Skip to content
Finance
Skill

/lp-math

AMM liquidity provision mathematics including constant-product, concentrated liquidity, price impact, and LP share calculations

From plugin
trading-skills
26767 skills
Install
$ npx -y skills add agiprolabs/claude-trading-skills --skill lp-math --agent claude-code

How it fires

How this skill gets triggered: by you, by Claude, or both.

  • Fires itselfAuto-invocation. Claude auto-loads it when your prompt matches the work.Auto-invocation is when the right skill fires by itself at the right moment, driven by a FLOW.md router and a hook, instead of you invoking it by name. It is the difference between a skill being installed and a skill actually getting used.Read the full definition →
  • You can call itInvoke it directly when you want it.
  • Slash command/lp-math

Context preview

The summary Claude sees to decide when to auto-load this skill.

AMM liquidity provision mathematics including constant-product, concentrated liquidity, price impact, and LP share calculations

SKILL.md

lp-math.SKILL.md
name: lp-math
description: AMM liquidity provision mathematics including constant-product, concentrated liquidity, price impact, and LP share calculations

LP Math — AMM Liquidity Provision Mathematics

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:

  • Evaluating whether providing liquidity is profitable after impermanent loss
  • Estimating price impact before executing large trades
  • Comparing capital efficiency across pool types (constant product vs concentrated)
  • Calculating expected fee revenue for a given pool position

**Related skills**: See `impermanent-loss` for IL calculations, `yield-analysis` for LP yield modeling, `liquidity-analysis` for pool depth assessment.

---

1. Constant Product AMM (xy = k)

The foundational AMM model used by Raydium V4 and most Solana DEXes.

Core Invariant

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)

Spot Price

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.

Trade Execution

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.

Inverse Calculation

To get a specific output amount Δy, the required input is:

delta_x = x * delta_y / (y - delta_y)

Price After Trade

price_new = y_new / x_new

Worked Example

Pool: 100 SOL / 10,000 USDC (k = 1,000,000), fee = 0.3%

Buy 5 SOL worth of USDC: 1. Gross output: `10,000 * 5 / (100 + 5) = 476.19 USDC` 2. Fee: `476.19 * 0.003 = 1.43 USDC` 3. Net output: `474.76 USDC` 4. Effective price: `474.76 / 5 = 94.95 USDC/SOL` (vs spot 100) 5. Price impact: `(100 - 94.95) / 100 = 5.05%` 6. New reserves: 105 SOL / 9,525.24 USDC 7. New k: `105 * 9,525.24 = 1,000,150.2` (k increased from fees)

See `references/amm_formulas.md` for complete derivations.

---

2. Concentrated Liquidity (CLMM)

Used by Orca Whirlpool, Raydium CLMM, and Meteora DLMM. Liquidity is only active within a chosen price range [P_lower, P_upper].

Key Concepts

L = sqrt(x * y)           # Liquidity within the active range
price_at_tick = 1.0001^tick  # Tick-to-price conversion

Capital Efficiency

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.

Position Value

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))
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 Strategy Comparison

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

See `references/amm_formulas.md` for full CLMM derivations.

---

3. Price Impact

Constant Product Impact

# 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 product

Multi-Hop Impact

For a route through multiple pools, compound the impacts:

def 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 Thresholds

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

---

4. LP Share Calculations

Initial Deposit (Empty Pool)

shares = sqrt(x_deposited * y_deposited)

The first depositor sets the ratio and receives shares equal to the geometric mean.

Subsequent Deposits

shares_minted = min(
    x_added / x_reserve,
    y_added / y_reserve
) * total_shares

Deposits must be proportional to the current reserve ratio. Any excess of one token is not used (or returned, depending on implementation).

Withdrawal

x_out
Read more
Ships withtrading-skills

A comprehensive collection of 67 ready-to-use trading, DeFi, and quantitative finance Agent Skills. Works with Claude Code, Cursor, Codex, Gemini CLI, and 30+ other tools.

Get the whole plugin
Stats
312
Stars
62
Forks
Maintained
Maintenance
Python
Language
MIT
License
1mo ago
Last commit
5mo ago
Created

Repo: agiprolabs/claude-trading-skills