The Mathematics of Constant Product Markets
Learning Objectives
Derive the constant product formula and explain why it produces liquidity
Calculate spot price and execution price for any trade size
Quantify slippage as a function of trade size and pool depth
Prove the relationship between price impact and trade size mathematically
Apply these formulas to real XRPL AMM pools
Many AMM users treat the constant product formula as a black box. They deposit tokens, make trades, and accept whatever price the protocol offers. This works until it doesn'tβuntil slippage exceeds expectations, or impermanent loss confuses, or a trade executes at a price that seems wrong.
- Verify whether a quoted price is correct
- Calculate expected slippage before executing
- Understand exactly why impermanent loss occurs
- Evaluate whether a pool is efficient for your needs
This lesson builds that understanding from the ground up. If you follow the derivations, you'll not only understand how AMMs work but why they must work this way given their design constraints.
THE FUNDAMENTAL EQUATION
x Γ y = k
Where:
βββ x = Quantity of Token A in the pool
βββ y = Quantity of Token B in the pool
βββ k = The constant product (invariant)
βββ After any trade: x_new Γ y_new = k (unchanged)
This single equation defines:
βββ The price at any moment
βββ The price after any trade
βββ The slippage for any trade size
βββ The relationship between all variables
βββ Everything about AMM behavior
```
The term "constant product" comes from the constraint that the product of the two token quantities must remain constant through all trades.
INVARIANT DEMONSTRATION
Initial pool state:
βββ x = 10,000 XRP
βββ y = 25,000 RLUSD
βββ k = 10,000 Γ 25,000 = 250,000,000
After a trade (buying XRP with RLUSD):
βββ x decreases (XRP removed)
βββ y increases (RLUSD added)
βββ But x_new Γ y_new must still equal 250,000,000
βββ This constraint determines the price
Why this matters:
βββ No external oracle needed
βββ Price emerges from pool state
βββ Self-balancing system
βββ Arbitrageurs correct mispricings
βββ Simple but powerful
```
Spot Price is the instantaneous price for an infinitesimally small trade.
SPOT PRICE DERIVATION
Given: x Γ y = k
Price of A in terms of B = dy/dx at the current state
Taking the differential of both sides:
x Γ dy + y Γ dx = 0 (since dk = 0, k is constant)
Rearranging:
dy/dx = -y/x
The negative sign indicates:
βββ As x increases, y must decrease
βββ Buying A requires giving up B
βββ This is the exchange relationship
Spot Price of A in terms of B:
P_A = y/x
Spot Price of B in terms of A:
P_B = x/y
Example:
βββ Pool: 10,000 XRP, 25,000 RLUSD
βββ Price of XRP = 25,000/10,000 = 2.5 RLUSD
βββ Price of RLUSD = 10,000/25,000 = 0.4 XRP
βββ Reciprocal relationship always holds
```
The constant product formula describes a rectangular hyperbola. This geometric interpretation helps visualize AMM behavior.
GEOMETRIC INTERPRETATION
The equation x Γ y = k describes a hyperbola in the x-y plane
Properties:
βββ Curve never touches either axis
βββ Infinite liquidity at extreme prices
βββ Liquidity decreases as you move toward axes
βββ Slope at any point = -y/x (the price)
Visual intuition:
βββ Near origin: Both balances small, price volatile
βββ Far from origin: Large balances, stable prices
βββ Trade moves you along the curve
βββ Large trades = large movement = large price change
Why hyperbola matters:
βββ Guarantees liquidity exists at ANY price
βββ But liquidity thins at extreme prices
βββ Trade-off between coverage and efficiency
βββ Concentrated liquidity (Uniswap v3) addresses this
```
When you make a trade, you don't get the spot priceβyou get the execution price, which accounts for the price moving as you trade.
EXECUTION PRICE DERIVATION
Setup:
βββ Pool: x tokens of A, y tokens of B
βββ Trader wants to buy Ξx tokens of A
βββ Trader must provide Ξy tokens of B
βββ After trade: (x - Ξx)(y + Ξy) = k = x Γ y
Solving for Ξy:
(x - Ξx)(y + Ξy) = xy
xy + xΞy - yΞx - ΞxΞy = xy
xΞy - yΞx - ΞxΞy = 0
xΞy = yΞx + ΞxΞy
xΞy = Ξx(y + Ξy)
Ξy = Ξx(y + Ξy)/x
Rearranging:
Ξy = (y Γ Ξx)/(x - Ξx)
This is the amount of B required to buy Ξx of A
Execution price:
P_exec = Ξy/Ξx = y/(x - Ξx)
Key insight:
βββ As Ξx approaches 0: P_exec β y/x (spot price)
βββ As Ξx increases: P_exec increases
βββ Denominator (x - Ξx) decreases as you buy more
βββ This is why large trades have worse prices
```
EXECUTION PRICE EXAMPLE
Pool state:
βββ x = 100,000 XRP
βββ y = 250,000 RLUSD
βββ k = 25,000,000,000
βββ Spot price = 250,000/100,000 = 2.50 RLUSD/XRP
Trade 1: Buy 100 XRP (0.1% of pool)
βββ Ξy = (250,000 Γ 100)/(100,000 - 100)
βββ Ξy = 25,000,000/99,900
βββ Ξy = 250.25 RLUSD
βββ Execution price = 250.25/100 = 2.5025 RLUSD/XRP
βββ Price impact = (2.5025 - 2.50)/2.50 = 0.1%
βββ Minimal slippage
Trade 2: Buy 1,000 XRP (1% of pool)
βββ Ξy = (250,000 Γ 1,000)/(100,000 - 1,000)
βββ Ξy = 250,000,000/99,000
βββ Ξy = 2,525.25 RLUSD
βββ Execution price = 2,525.25/1,000 = 2.5253 RLUSD/XRP
βββ Price impact = (2.5253 - 2.50)/2.50 = 1.01%
βββ Noticeable slippage
Trade 3: Buy 10,000 XRP (10% of pool)
βββ Ξy = (250,000 Γ 10,000)/(100,000 - 10,000)
βββ Ξy = 2,500,000,000/90,000
βββ Ξy = 27,777.78 RLUSD
βββ Execution price = 27,777.78/10,000 = 2.7778 RLUSD/XRP
βββ Price impact = (2.7778 - 2.50)/2.50 = 11.11%
βββ Significant slippage
Pattern: Price impact scales roughly with trade size as % of pool
```
SLIPPAGE DERIVATION
Slippage = (Execution Price - Spot Price) / Spot Price
Spot price: P_spot = y/x
Execution price: P_exec = y/(x - Ξx)
Slippage = [y/(x - Ξx) - y/x] / [y/x]
= [yΒ·x - yΒ·(x - Ξx)] / [(x - Ξx)Β·y]
= [yx - yx + yΞx] / [y(x - Ξx)]
= Ξx / (x - Ξx)
Simplified slippage formula:
Slippage = Ξx / (x - Ξx)
Where Ξx/x is the trade size as fraction of pool
For small trades (Ξx << x):
Slippage β Ξx/x
This means:
βββ 1% of pool β ~1% slippage
βββ 5% of pool β ~5.3% slippage
βββ 10% of pool β ~11.1% slippage
βββ 20% of pool β ~25% slippage
βββ Slippage accelerates for larger trades
```
SLIPPAGE REFERENCE TABLE
| Trade Size (% of pool) | Slippage |
|---|
0.1% | 0.10%
0.5% | 0.50%
1.0% | 1.01%
2.0% | 2.04%
3.0% | 3.09%
5.0% | 5.26%
10.0% | 11.11%
15.0% | 17.65%
20.0% | 25.00%
25.0% | 33.33%
50.0% | 100.00%
Key observations:
βββ Slippage is roughly linear for small trades
βββ Accelerates significantly above 10%
βββ At 50% of pool, you pay 2x spot price
βββ Buying entire pool: Approaches infinity
βββ This is why pool size matters!
```
After a trade executes, the pool composition changes, and so does the spot price.
POST-TRADE PRICE
Before trade:
βββ Pool: xβ = 100,000 XRP, yβ = 250,000 RLUSD
βββ k = 25,000,000,000
βββ Price = 2.50 RLUSD/XRP
Buy 10,000 XRP:
βββ Pay Ξy = 27,777.78 RLUSD (calculated earlier)
βββ New pool: xβ = 90,000 XRP, yβ = 277,777.78 RLUSD
βββ Verify: 90,000 Γ 277,777.78 = 25,000,000,020 β k β
βββ New price = 277,777.78/90,000 = 3.0864 RLUSD/XRP
Price impact:
βββ Started at 2.50 RLUSD/XRP
βββ Ended at 3.0864 RLUSD/XRP
βββ Price moved 23.46%
βββ Your trade moved the market
This creates arbitrage opportunity:
βββ If external market price is still 2.50
βββ Arbitrageur can sell XRP to pool at 3.0864
βββ Buy XRP externally at 2.50
βββ Profit on difference
βββ This restores the price
```
LIQUIDITY-SLIPPAGE RELATIONSHIP
The same dollar trade has different slippage in different pools:
$10,000 trade in small pool:
βββ Pool: 10,000 XRP + 25,000 RLUSD ($50,000 total)
βββ Trade: Buy ~4,000 XRP (40% of XRP reserves)
βββ Slippage = 0.40/(1-0.40) = 66.7%
βββ Execution is terrible
$10,000 trade in medium pool:
βββ Pool: 100,000 XRP + 250,000 RLUSD ($500,000 total)
βββ Trade: Buy ~4,000 XRP (4% of reserves)
βββ Slippage = 0.04/(1-0.04) = 4.17%
βββ Execution is acceptable
$10,000 trade in large pool:
βββ Pool: 1,000,000 XRP + 2,500,000 RLUSD ($5,000,000 total)
βββ Trade: Buy ~4,000 XRP (0.4% of reserves)
βββ Slippage = 0.004/(1-0.004) = 0.40%
βββ Execution is excellent
Key insight:
βββ 10Γ liquidity β ~10Γ less slippage
βββ Pool depth determines execution quality
βββ This is why TVL matters
βββ And why XRPL's small pools are a limitation
```
EFFECTIVE LIQUIDITY CONCEPT
Not all TVL is equal for execution quality:
Constant product (Uniswap v2, XRPL):
βββ Liquidity spread across infinite price range
βββ Most liquidity is "far" from current price
βββ Only fraction is "active" near market
βββ Capital inefficient
Example:
βββ Pool TVL: $1,000,000
βββ Effective liquidity at Β±2% of current price: ~$40,000
βββ 96% of liquidity sits unused at extreme prices
βββ This is the "capital efficiency problem"
Concentrated liquidity (Uniswap v3):
βββ LPs specify price range
βββ All capital active within that range
βββ Much better execution
βββ But: Requires active management, more IL risk
XRPL AMM uses constant product:
βββ Same capital efficiency limitations
βββ Simpler for LPs (no range management)
βββ Trade-off accepted for simplicity
βββ May evolve in future amendments
---
In practice, AMMs charge fees. The fee reduces the amount you receive.
INCORPORATING FEES
Standard model (without fees):
Ξy = (y Γ Ξx)/(x - Ξx)
With fee (f = 0.3% = 0.003):
βββ Fee is taken from input
βββ Effective input: Ξx Γ (1 - f)
βββ This increases cost to trader
Adjusted formula:
Ξy = (y Γ Ξx Γ (1-f))/(x - Ξx Γ (1-f))
Example with 0.3% fee:
βββ Pool: 100,000 XRP, 250,000 RLUSD
βββ Buy 1,000 XRP
βββ Without fee: Pay 2,525.25 RLUSD
βββ With fee: Pay 2,525.25/(1-0.003) = 2,532.84 RLUSD
βββ Additional cost: 7.59 RLUSD (0.3%)
βββ Fee goes to LPs
Alternative interpretation:
βββ You're buying 1,000 XRP with RLUSD
βββ Pool uses your RLUSD as if 0.3% less
βββ Results in same execution but higher cost
βββ Fee is always paid by trader
```
Sometimes you know how much you want to spend, not how much you want to buy.
SPENDING A FIXED AMOUNT
Question: How much XRP can I buy with 5,000 RLUSD?
Setup:
βββ Pool: x = 100,000 XRP, y = 250,000 RLUSD
βββ k = 25,000,000,000
βββ Spending: Ξy = 5,000 RLUSD
βββ Find: Ξx = ?
Using constant product:
(x - Ξx)(y + Ξy) = xy
Solving for Ξx:
(x - Ξx) = xy/(y + Ξy)
Ξx = x - xy/(y + Ξy)
Ξx = x(y + Ξy - y)/(y + Ξy)
Ξx = x Γ Ξy/(y + Ξy)
Calculation:
Ξx = 100,000 Γ 5,000/(250,000 + 5,000)
Ξx = 500,000,000/255,000
Ξx = 1,960.78 XRP
Verify:
βββ New pool: 98,039.22 XRP, 255,000 RLUSD
βββ Product: 98,039.22 Γ 255,000 = 25,000,001,100 β k β
βββ Effective price: 5,000/1,960.78 = 2.55 RLUSD/XRP
βββ vs spot price 2.50 = 2% slippage
```
FULL TRADE ANALYSIS WORKFLOW
Step 1: Gather pool data
βββ Token A balance: x
βββ Token B balance: y
βββ Fee rate: f
βββ Calculate k = x Γ y
Step 2: Determine trade direction
βββ Buying A with B: You provide Ξy, receive Ξx
βββ Selling A for B: You provide Ξx, receive Ξy
βββ Formulas differ by direction
Step 3: Calculate without fees
βββ Buying Ξx of A: Ξy = yΓΞx/(x-Ξx)
βββ Spending Ξy for A: Ξx = xΓΞy/(y+Ξy)
βββ Use appropriate formula
Step 4: Apply fee
βββ Multiply cost by 1/(1-f) if paying B
βββ Or multiply received by (1-f) if receiving B
βββ Fee always reduces trader value
Step 5: Calculate metrics
βββ Execution price = Ξy/Ξx
βββ Spot price = y/x
βββ Slippage = (Exec - Spot)/Spot
βββ Price impact = change in spot price after trade
βββ Document all for analysis
Step 6: Compare alternatives
βββ vs other AMM pools
βββ vs order book DEX
βββ vs centralized exchange
βββ Choose best execution
```
POOL DRAINAGE IMPOSSIBILITY
Intuition: Why can't someone buy all the XRP in a pool?
Mathematical answer:
βββ To buy x tokens (all XRP), you need infinite y tokens
βββ As Ξx β x, the formula Ξy = yΓΞx/(x-Ξx) β β
βββ The last token costs infinite amount
βββ Pool can never be fully drained
Numerical demonstration:
βββ Pool: 100,000 XRP, 250,000 RLUSD
βββ Buy 99,000 XRP (99% of pool)
βββ Ξy = 250,000 Γ 99,000/(100,000 - 99,000)
βββ Ξy = 24,750,000,000/1,000 = 24,750,000 RLUSD
βββ That's 99Γ the original RLUSD in pool!
βββ And you still can't get the last 1,000 XRP
This property means:
βββ Liquidity exists at any price (theoretically)
βββ Extreme prices are extremely expensive
βββ Pool always has some of each token
βββ A core feature of constant product design
```
TRADE SYMMETRY
Property: The cost to buy X tokens equals the
amount received when selling X tokens (ignoring fees)
Proof:
βββ Buy Ξx of A, pay Ξy of B
βββ Ξy = yΓΞx/(x-Ξx)
βββ Now sell Ξx of A, starting from new state:
βββ New pool: (x-Ξx), (y+Ξy)
βββ Selling Ξx returns: (y+Ξy)ΓΞx/((x-Ξx)+Ξx)
βββ = (y+Ξy)ΓΞx/x
βββ Simplifying with Ξy = yΓΞx/(x-Ξx):
βββ = (y + yΓΞx/(x-Ξx))ΓΞx/x
βββ = yΓΞx/x + yΓ(Ξx)Β²/(x(x-Ξx))
βββ This equals Ξy β
What this means:
βββ Round-trip (buy then sell) returns to original
βββ No arbitrage from same-pool round trips
βββ Pool is self-consistent
βββ Important for mathematical soundness
```
MARGINAL VS AVERAGE PRICE
Marginal price: Price of next infinitesimal unit
Average price: Total cost / total units purchased
For constant product:
βββ Marginal price varies continuously during trade
βββ Starts at spot price, increases throughout
βββ Average price is always between start and end marginal
Mathematical relationship:
βββ Spot (start): Pβ = y/x
βββ Spot (end): Pβ = (y+Ξy)/(x-Ξx)
βββ Average: P_avg = Ξy/Ξx
Property: Pβ < P_avg < Pβ (when buying)
This is why slippage exists:
βββ You pay average price, not spot price
βββ Average includes all the marginal price increases
βββ Larger trades = higher average
βββ Cannot avoid without smaller orders
```
XRPL-SPECIFIC CONSIDERATIONS
Same formula:
βββ XRPL uses constant product (x Γ y = k)
βββ Mathematics identical to Uniswap v2
βββ All derivations in this lesson apply
βββ No XRPL-specific formula changes
Fee structure:
βββ Pool creator sets fee tier (0.0% to 1.0%)
βββ Fee goes to LP token holders
βββ Continuous auction may provide additional LP revenue
βββ Total LP revenue = Trading fees + Auction proceeds
Practical differences:
βββ Much smaller pools (~$20-50M total TVL)
βββ Higher relative slippage for same $ trade
βββ Less arbitrageur activity to correct prices
βββ Integration with order book provides alternatives
βββ Must compare AMM vs order book execution
```
XRPL AMM CALCULATION EXAMPLE
Hypothetical XRPL pool (realistic scale):
βββ Pool: 500,000 XRP + 1,250,000 RLUSD
βββ Total value: ~$2,500,000 (assuming XRP = $2.50)
βββ Fee: 0.5%
βββ k = 625,000,000,000
Trade: Buy 10,000 XRP
Step 1: Calculate without fees
βββ Ξy = (1,250,000 Γ 10,000)/(500,000 - 10,000)
βββ Ξy = 12,500,000,000/490,000
βββ Ξy = 25,510.20 RLUSD
Step 2: Apply 0.5% fee
βββ Actual cost = 25,510.20/(1-0.005)
βββ Actual cost = 25,638.39 RLUSD
Step 3: Calculate metrics
βββ Spot price: 1,250,000/500,000 = 2.50 RLUSD/XRP
βββ Execution price: 25,638.39/10,000 = 2.564 RLUSD/XRP
βββ Slippage: (2.564 - 2.50)/2.50 = 2.56%
βββ Price impact: 10,000/490,000 = 2.04%
βββ Fee component: 0.5%
Step 4: Compare to order book
βββ Check XRPL DEX for 10,000 XRP order
βββ If order book has 10,000 XRP at 2.51 = better
βββ If order book has only 2,000 XRP at 2.51 = maybe split
βββ This is why dual venue is valuable
βββ XRPL pathfinding considers both
```
WHY XRPL POOL SIZE MATTERS
Current XRPL AMM reality:
βββ Largest pools: ~$1-5M TVL
βββ Many pools: <$100K TVL
βββ Compare to Uniswap pools: $10M-500M TVL
βββ 10-100Γ smaller typical pool
Slippage implications:
βββ $10K trade in $100K pool: ~10% slippage
βββ $10K trade in $1M pool: ~1% slippage
βββ $10K trade in $10M pool: ~0.1% slippage
βββ Smaller pools = worse execution
Practical impact:
βββ Large trades nearly impossible via AMM
βββ Order book may be better for size
βββ Arbitrageurs less active (less profit opportunity)
βββ Prices may diverge from external markets longer
βββ LPs face adverse selection with fewer arbitrageurs
```
β The constant product formula is mathematically sound. It produces consistent, predictable behavior that has been battle-tested across billions in volume.
β Slippage is deterministic. Given pool size and trade size, slippage can be calculated exactly before execution.
β The formula creates guaranteed liquidity. Any trade can execute at some price, regardless of order book depth.
β οΈ Optimal fee levels for XRPL pools. The trade-off between LP compensation and trader costs is empirical.
β οΈ Whether constant product is optimal for XRPL. Concentrated liquidity could improve capital efficiency but adds complexity.
β οΈ How arbitrageurs will behave with small pools. Less profit opportunity may mean less price accuracy.
π Ignoring slippage calculations. Large trades in small pools can have catastrophic execution.
π Assuming all pools are equal. A $100K pool and a $10M pool have vastly different execution quality.
π Not comparing to order book. XRPL's order book DEX may offer better execution for many trades.
The mathematics of constant product AMMs is elegant, proven, and deterministic. Understanding these formulas lets you calculate exact costs before trading. However, mathematical soundness doesn't overcome economic limitationsβXRPL's small pool sizes mean slippage will often be higher than on larger platforms. Always calculate before executing.
Assignment: Complete 10 calculation exercises demonstrating mastery of AMM mathematics.
Requirements:
Exercise Set A: Basic Calculations (4 problems)
Spot Price Calculation
Simple Trade Execution
Trade with Fees
Spending Fixed Amount
Exercise Set B: Analysis Problems (4 problems)
Pool Comparison
Slippage Table Construction
Post-Trade Price Analysis
Arbitrage Opportunity
Exercise Set C: Advanced Problems (2 problems)
Fee Break-Even
Multi-Hop Trade
Show all work for each problem
Include formulas used
Verify answers where possible (e.g., check k stays constant)
Provide brief interpretation of each result
Mathematical accuracy (40%)
Work shown and methodology (30%)
Formula application (20%)
Interpretation quality (10%)
Time Investment: 3-4 hours
Knowledge Check
Question 1 of 3In a pool with 80,000 XRP and 200,000 RLUSD, what is the spot price of XRP?
- Uniswap v2 whitepaper (formal proofs)
- "An analysis of Uniswap markets" (Angeris et al.)
- "Improved Price Oracles: Constant Function Market Makers" (Angeris & Chitra)
- Uniswap documentation (mathematical appendix)
- Paradigm research on AMM mathematics
- Various DeFi educational courses
- AMM slippage calculators
- Pool analysis dashboards
- Trading simulators
For Next Lesson:
With the basic mathematics understood, Lesson 3 will tackle the most misunderstood topic in AMM economics: impermanent loss. We'll derive the IL formula from first principles and understand exactly why LPs sometimes lose money even when prices return to entry levels.
End of Lesson 2
Total words: ~6,200
Estimated completion time: 65 minutes reading + 3-4 hours for deliverable
Key Takeaways
x Γ y = k is the complete model.
Every aspect of AMM behaviorβprice, slippage, executionβderives from this single equation.
Spot price = y/x, always.
The ratio of token quantities is the instantaneous price. Everything else builds from this.
Slippage β Trade Size / Pool Size.
For small trades, slippage is roughly proportional to your trade as a percentage of the pool. Large trades face accelerating slippage.
Pool depth determines execution quality.
A 10Γ larger pool has roughly 10Γ less slippage for the same dollar trade. XRPL's smaller pools mean higher slippage.
Always calculate before trading.
The formulas let you know exact execution cost before submitting. Use them. ---