agiprolabs/claude-trading-skills/lp-math
Claude Code and agent skill by agiprolabs/claude-trading-skills
- community
Checking that the SKILL.md file is still in the repository.
AMM liquidity provision mathematics including constant-product, concentrated liquidity, price impact, and LP share calculations
Install
npx vskill@latest install agiprolabs/claude-trading-skills/lp-mathSKILL.md
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
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
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:
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:
Price After Trade
Worked Example
Pool: 100 SOL / 10,000 USDC (k = 1,000,000), fee = 0.3%
Buy 5 SOL worth of USDC:
- Gross output:
10,000 * 5 / (100 + 5) = 476.19 USDC - Fee:
476.19 * 0.003 = 1.43 USDC - Net output:
474.76 USDC - Effective price:
474.76 / 5 = 94.95 USDC/SOL(vs spot 100) - Price impact:
(100 - 94.95) / 100 = 5.05% - New reserves: 105 SOL / 9,525.24 USDC
- 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
Capital Efficiency
Concentrating liquidity in a narrow range provides more depth per dollar:
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:
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
Multi-Hop Impact
For a route through multiple pools, compound the impacts:
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)
The first depositor sets the ratio and receives shares equal to the geometric mean.
Subsequent Deposits
Deposits must be proportional to the current reserve ratio. Any excess of one token is not used (or returned, depending on implementation).
Withdrawal
You always receive both tokens in the current ratio.
Share Value
5. Fee Accrual
Fees accumulate inside the pool, increasing k:
For CLMM positions, fee earnings depend on:
- Whether price stays within your range (out-of-range = no fees)
- Your share of active liquidity in that range
- Total volume routed through the pool
6. Solana Pool Types
Raydium V4 (Constant Product)
- Model: Standard xy = k
- Fee: 0.25% (0.22% to LP, 0.03% to RAY buyback)
- Best for: New token launches, volatile pairs
- Note: Integrated with OpenBook for limit order flow
Orca Whirlpool (Concentrated Liquidity)
- Model: Concentrated liquidity with tick spacing
- Fee tiers: 0.01%, 0.05%, 0.3%, 1%
- Position: Represented as NFT (each position is unique)
- Best for: Major pairs (SOL/USDC), stablecoin pairs
Raydium CLMM
- Model: Concentrated liquidity (similar to Uniswap V3)
- Tick spacing: 1, 10, 60, 200
- Fee tiers: 0.01%, 0.05%, 0.25%, 1%
- Best for: Pairs with predictable ranges
Meteora DLMM (Dynamic Liquidity Market Maker)
- Model: Discrete bins instead of continuous ticks
- Strategies: Spot (uniform), Curve (concentrated), Bid-Ask (around current price)
- Fees: Dynamic, adjusting based on volatility
- Best for: Active LPs who rebalance frequently
See references/pool_mechanics.md for detailed mechanics and comparison.
7. Practical Decision Framework
Should You LP?
Which Pool Type?
Position Sizing for LP
Files
References
references/amm_formulas.md— Complete mathematical derivations for constant product and concentrated liquidity AMMsreferences/pool_mechanics.md— Solana-specific pool mechanics for Raydium, Orca, and Meteora
Scripts
scripts/amm_calculator.py— Constant product AMM calculator with trade simulation, LP shares, and fee accrualscripts/clmm_calculator.py— Concentrated liquidity calculator with position valuation, capital efficiency, and range comparison
Quick Reference
| Formula | Expression | |
|---|---|---|
| Constant product | x * y = k | |
| Spot price | P = y / x | |
| Trade output | Δy = y * Δx / (x + Δx) | |
| Required input | Δx = x * Δy / (y - Δy) | |
| Price impact | Δx / (x + Δx) | |
| Initial LP shares | sqrt(x * y) | |
| Subsequent shares | min(Δx/x, Δy/y) * total | |
| Fee APR | (daily_fees * 365) / TVL | |
| CLMM efficiency | sqrt(P_u/P_l) / (sqrt(P_u/P_l) - 1) | |
| Tick to price | 1.0001^tick |
Security
- Security scanPassed, 95 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/lp-math/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/agiprolabs/claude-trading-skills/lp-math)