core.test_uniswap

def test_calc_sqrt_prices():

Test Square Root Price Calculation

Uniswap V3 uses square root prices ($$\sqrt{P}$$) internally for all calculations. This test verifies the conversion from raw price to sqrt price.

Formulas

$$s_0 = \sqrt{P_0}$$ $$s_a = \sqrt{P_{min}}$$ $$s_b = \sqrt{P_{max}}$$

Test Data

Inputs:

  • Current Price ($P_0$): 3050
  • Min Price ($P_{min}$): 2200
  • Max Price ($P_{max}$): 3650

Expected Output:

  • $s_0 = \sqrt{3050} \approx 55.226805$
  • $s_a = \sqrt{2200} \approx 46.904157$
  • $s_b = \sqrt{3650} \approx 60.415229$
def test_calc_coefficients():

Test Liquidity Coefficients

These coefficients ($a_0, a_1$) determine the relationship between Investment amount and Liquidity ($L$).

Formulas

Coefficient for Token 0 (Volatile/ETH): $$a_0 = \frac{s_b - s_0}{s_0 s_b}$$

Coefficient for Token 1 (Stable/USDT): $$a_1 = s_0 - s_a$$

Test Data

Inputs:

  • $s_0 = 55.226805...$
  • $s_a = 46.904157...$
  • $s_b = 60.415229...$

Expected Output:

  • $a_0 \approx 0.001555$
  • $a_1 \approx 8.3226$
def test_calc_liquidity():

Test Liquidity Calculation

Calculates the amount of Liquidity ($L$) minted for a given investment amount.

Formula

$$L = \frac{I}{a_0 P_0 + a_1}$$

Where $I$ is the total investment in USD terms (if $P_0$ is USD price). (Note: This formula assumes splitting investment optimally to cover the range)

Test Data

Inputs:

  • Investment ($I$): 100,000
  • $a_0$: 0.001555...
  • $a_1$: 8.3226...
  • Price ($P_0$): 3050

Calculation: Denominator = $0.001555 \times 3050 + 8.3226 \approx 4.742 + 8.322 = 13.064$

$L = 100,000 / 13.064 \approx 7653.75$

Expected Output:

  • $L \approx 7653.75$
def test_token_amounts_at_price():

Test Token Amounts (Delta)

Calculates the token composition of the LP position at a specific price $P$. This verifies the "Gamma" or rate of change of the position.

Formulas (In Range: $P_{min} < P < P_{max}$)

Volatile Token (ETH): $$x = L \cdot \frac{s_b - \sqrt{P}}{\sqrt{P} s_b}$$

Stable Token (USDT): $$y = L \cdot (\sqrt{P} - s_a)$$

Test Data

Inputs:

  • Price ($P$): 3000
  • Liquidity ($L$): 7653.75
  • $\sqrt{P} = 54.77225$
  • $s_a = 46.90415$
  • $s_b = 60.41522$

Expected Output:

  • Volatile ($x$): ~13.05 ETH
  • Stable ($y$): ~60,220.44 USDT
def test_il_calculations():

Test Impermanent Loss (IL)

Verifies the calculation of IL relative to HODL and relative to Initial Deposit.

Formulas

IL vs HODL: $$IL_{\%} = \frac{V_{LP}}{V_{HODL}} - 1$$

IL vs Deposit: $$IL_{\%} = \frac{V_{LP}}{I_{initial}} - 1$$

Test Data

Inputs:

  • LP Value ($V_{LP}$): 89,667.86
  • HODL Value ($V_{HODL}$): 93,454.00
  • Initial Investment ($I$): 100,000.00

Expected Output:

  • IL vs HODL: $89667.86 / 93454.00 - 1 \approx -0.0405$ (-4.05%)
  • IL vs Deposit: $89667.86 / 100000 - 1 \approx -0.1033$ (-10.33%)