Let's define the key variables:
Constant Product Formula:
\[(R_A^1 + γΔx)(R_B^1 - Δy_1) = k_1\]Output Amount (Δy₁) of token B:
\[Δy_1 = \frac{γΔx \cdot R_B^1}{R_A^1 + γΔx}\]New Reserves After Swap:
\[ \begin{aligned} R_A^{1'} &= R_A^1 + Δx \\ R_B^{1'} &= R_B^1 - Δy_1 \end{aligned} \]Constant Product Formula:
\[(R_B^2 + γΔy_1)(R_C^2 - Δy_2) = k_2\]Output Amount (Δy₂) of token C:
\[Δy_2 = \frac{γΔy_1 \cdot R_C^2}{R_B^2 + γΔy_1}\]For the arbitrage to be profitable:
\[Δy_2 > Δx + costs\]Substituting the expressions:
\[\frac{γ(\frac{γΔx \cdot R_B^1}{R_A^1 + γΔx}) \cdot R_C^2}{R_B^2 + γ(\frac{γΔx \cdot R_B^1}{R_A^1 + γΔx})} > Δx + costs\]To find the optimal amount, differentiate profit with respect to Δx:
\[\frac{d}{dΔx}(Δy_2 - Δx - costs) = 0\]This yields a complex polynomial equation. The solution gives the optimal flash loan amount that maximizes profit.
For a chain of n pools:
\[Δy_i = \frac{γΔy_{i-1} \cdot R_{out}^i}{R_{in}^i + γΔy_{i-1}}\]where Δy₀ = Δx and i ranges from 1 to n.
Initial conditions:
\[ \begin{aligned} R_A^1 &= R_B^1 = 1000 \\ R_B^2 &= R_C^2 = 1000 \\ Δx &= 1000 \\ γ &= 0.997 \end{aligned} \]First swap output:
\[Δy_1 = \frac{0.997 \cdot 1000 \cdot 1000}{1000 + 0.997 \cdot 1000} ≈ 498.75\]Second swap output:
\[Δy_2 = \frac{0.997 \cdot 498.75 \cdot 1000}{1000 + 0.997 \cdot 498.75} ≈ 332.5\]After arbitrage: