AMM liquidity provision mathematics including constant-product, concentrated liquidity, price impact, and LP share calculations
Automated Market Makers (AMMs) replace traditional orderbooks with liquidity pools. Instead of matching buyers and sellers, a mathematical formula determines prices based on reserve ratios. Liquidity providers (LPs) deposit both assets into a pool and earn fees from every trade.
Understanding the math behind AMMs is essential for:
Related skills: See impermanent-loss for IL calculations, yield-analysis for LP yield modeling, liquidity-analysis for pool depth assessment.
The foundational AMM model used by Raydium V4 and most Solana DEXes.
x * y = k
Where:
x = reserve amount of token X (e.g., SOL)y = reserve amount of token Y (e.g., USDC)k = constant product (increases over time from fees)P = x / y (price of Y in terms of X)
P = y / x (price of X in terms of Y)
For a pool with 100 SOL and 10,000 USDC: price of SOL = 10,000 / 100 = 100 USDC.
When a trader swaps Δx of token X into the pool:
# Output amount (before fees)
delta_y = y * delta_x / (x + delta_x)
# With fee (e.g., 0.3%)
delta_y_after_fee = delta_y * (1 - fee_rate)
# New reserves
x_new = x + delta_x
y_new = y - delta_y_after_fee
The key insight: larger trades get worse prices because each unit moves the ratio further.
To get a specific output amount Δy, the required input is:
delta_x = x * delta_y / (y - delta_y)
price_new = y_new / x_new
Pool: 100 SOL / 10,000 USDC (k = 1,000,000), fee = 0.3%
Buy 5 SOL worth of USDC:
10,000 * 5 / (100 + 5) = 476.19 USDC476.19 * 0.003 = 1.43 USDC474.76 USDC474.76 / 5 = 94.95 USDC/SOL (vs spot 100)(100 - 94.95) / 100 = 5.05%105 * 9,525.24 = 1,000,150.2 (k increased from fees)See references/amm_formulas.md for complete derivations.
Used by Orca Whirlpool, Raydium CLMM, and Meteora DLMM. Liquidity is only active within a chosen price range [P_lower, P_upper].
L = sqrt(x * y) # Liquidity within the active range
price_at_tick = 1.0001^tick # Tick-to-price conversion
Concentrating liquidity in a narrow range provides more depth per dollar:
# Capital efficiency ratio
efficiency = sqrt(P_upper / P_lower) / (sqrt(P_upper / P_lower) - 1)
# Example: ±5% range around $100 SOL
P_lower, P_upper = 95, 105
efficiency = sqrt(105/95) / (sqrt(105/95) - 1) # ≈ 20.5x
A ±5% range is ~20x more capital-efficient than full-range, but the position goes 100% into one asset if price moves outside the range.
For a CLMM position with liquidity L in range [P_lower, P_upper] at current price P:
if P <= P_lower:
# All in token X (below range)
value_x = L * (1/sqrt(P_lower) - 1/sqrt(P_upper))
value_y = 0
elif P >= P_upper:
# All in token Y (above range)
value_x = 0
value_y = L * (sqrt(P_upper) - sqrt(P_lower))