Loading...
Loading...
AMM liquidity provision mathematics including constant-product, concentrated liquidity, price impact, and LP share calculations
npx skill4agent add agiprolabs/claude-trading-skills lp-mathimpermanent-lossyield-analysisliquidity-analysisx * y = kxykP = x / y (price of Y in terms of X)
P = y / x (price of X in terms of Y)# 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_feedelta_x = x * delta_y / (y - delta_y)price_new = y_new / x_new10,000 * 5 / (100 + 5) = 476.19 USDC476.19 * 0.003 = 1.43 USDC474.76 USDC474.76 / 5 = 94.95 USDC/SOL(100 - 94.95) / 100 = 5.05%105 * 9,525.24 = 1,000,150.2references/amm_formulas.mdL = sqrt(x * y) # Liquidity within the active range
price_at_tick = 1.0001^tick # Tick-to-price conversion# 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.5xif 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))
else:
# In range — holds both tokens
value_x = L * (1/sqrt(P) - 1/sqrt(P_upper))
value_y = L * (sqrt(P) - sqrt(P_lower))| Range | Efficiency | IL Risk | Fee Capture | Best For |
|---|---|---|---|---|
| ±2% | ~50x | Very high | High if in range | Stablecoins, tight pegs |
| ±5% | ~20x | High | Good for trending | Active management |
| ±25% | ~4x | Moderate | Consistent | Semi-passive |
| ±100% | ~2x | Low | Lower per $ | Passive, volatile pairs |
| Full range | 1x | Baseline | Always earning | Set and forget |
references/amm_formulas.md# Price impact as a fraction
price_impact = delta_x / (x + delta_x)
# As percentage of pool
pool_fraction = trade_value / pool_tvl
# Rule of thumb: impact ≈ 2 * pool_fraction for constant productdef multi_hop_impact(hops: list[dict]) -> float:
"""Calculate total price impact across route legs.
Args:
hops: List of {reserve_in, trade_amount} for each leg.
Returns:
Total price impact as a fraction.
"""
remaining = 1.0
for hop in hops:
leg_impact = hop["trade_amount"] / (hop["reserve_in"] + hop["trade_amount"])
remaining *= (1 - leg_impact)
return 1 - remaining| Impact | Assessment | Action |
|---|---|---|
| < 0.1% | Negligible | Proceed normally |
| 0.1–0.5% | Low | Acceptable for most trades |
| 0.5–2% | Moderate | Consider splitting across pools |
| 2–5% | High | Split trade, use TWAP |
| > 5% | Severe | Reduce size or find deeper pools |
shares = sqrt(x_deposited * y_deposited)shares_minted = min(
x_added / x_reserve,
y_added / y_reserve
) * total_sharesx_out = (shares_burned / total_shares) * x_reserve
y_out = (shares_burned / total_shares) * y_reserveshare_value = pool_tvl / total_shares
your_value = your_shares * share_value# Before trade: k = x * y
# After trade with fee:
# k_new = (x + delta_x) * (y - delta_y_net) > k
# The difference is the fee retained in the pool
# Fee APR estimation
daily_volume = 500_000 # USD
fee_rate = 0.003 # 0.3%
daily_fees = daily_volume * fee_rate # $1,500
tvl = 2_000_000 # $2M pool
fee_apr = (daily_fees * 365) / tvl # 27.4%# CLMM fee estimation
your_liquidity = 50_000 # Your L
total_liquidity = 1_000_000 # Total L in your tick range
your_share = your_liquidity / total_liquidity # 5%
your_daily_fees = daily_fees * your_share # $75references/pool_mechanics.md1. Calculate expected fee APR
2. Estimate impermanent loss for expected price movement
3. Net return = fee APR - IL
4. Compare to simply holding the assetsStablecoin pair → CLMM with tight range (±0.5%)
Major pair (SOL/USDC) → CLMM with moderate range (±10-25%)
New/volatile token → Constant product (full range)
Active management → Meteora DLMM with dynamic rebalancing# Never LP more than you can afford to lose to IL
max_lp_allocation = portfolio_value * 0.20 # 20% max in any single pool
# For volatile pairs, reduce further
volatility_adjustment = 1 - (annualized_vol / 2) # Scale down for vol
adjusted_allocation = max_lp_allocation * max(0.1, volatility_adjustment)references/amm_formulas.mdreferences/pool_mechanics.mdscripts/amm_calculator.pyscripts/clmm_calculator.py| Formula | Expression |
|---|---|
| Constant product | |
| Spot price | |
| Trade output | |
| Required input | |
| Price impact | |
| Initial LP shares | |
| Subsequent shares | |
| Fee APR | |
| CLMM efficiency | |
| Tick to price | |