Mathematical Proof: Engineering Multi-Pool Arbitrage in Uniswap V2

Contents

  1. Definitions and Initial Conditions
  2. First Swap Mathematical Framework
  3. Second Swap Mathematical Framework
  4. Profitability Condition
  5. Optimal Flash Loan Amount
  6. Generalized Formula
  7. Practical Constraints
  8. Example Application
  9. Convergence to Market Efficiency

1. Definitions and Initial Conditions

Let's define the key variables:

\[ \begin{aligned} R_A^1, R_B^1 &: \text{Initial reserves of tokens A and B in Pool 1} \\ R_B^2, R_C^2 &: \text{Initial reserves of tokens B and C in Pool 2} \\ Δx &: \text{Amount of token A borrowed via flash loan} \\ γ &: \text{Uniswap fee coefficient (0.997 for 0.3% fee)} \\ k_1 &: \text{Constant product for Pool 1} = R_A^1 \cdot R_B^1 \\ k_2 &: \text{Constant product for Pool 2} = R_B^2 \cdot R_C^2 \end{aligned} \]

2. First Swap (A → B) Mathematical Framework

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} \]

3. Second Swap (B → C) Mathematical Framework

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}\]

4. Profitability Condition

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\]

5. Optimal Flash Loan Amount

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.

6. Generalized Formula for n Pools

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.

7. Practical Constraints

  1. Slippage Constraint:
    For each swap i: \(Δy_i ≥ minimum\_output_i\)
  2. Reserve Constraint:
    For all pools: \(R_{token}^i > 0\) after swaps
  3. Gas Cost Constraint:
    \(Profit > gas\_price \cdot gas\_used\)

8. Example Application

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\]

9. Convergence to Market Efficiency

After arbitrage:

  1. Price of B relative to A in Pool 1 decreases
  2. Price of C relative to B in Pool 2 increases
  3. System tends toward equilibrium where: \[\frac{R_A^1}{R_B^1} ≈ \frac{R_B^2}{R_C^2}\]