5.6 StableSwap and Trading Similar Assets
The $9 Billion Problem
In early 2020, if you wanted to swap $1 million USDC for DAI on Uniswap, you'd receive approximately $909,090 DAI—losing over $90,000 despite both assets being worth exactly $1. This wasn't a bug. It was a fundamental limitation of the constant product formula xy = k when applied to assets that should trade at 1:1.
This inefficiency represented a massive market opportunity. Stablecoin trading volume exceeded $100 billion annually, with each trade suffering from unnecessary slippage. The solution came from Curve Finance (originally called StableSwap), which introduced an elegant mathematical innovation: a bonding curve that combines the best properties of constant sum and constant product formulas.
Today, Curve dominates stablecoin trading with:
- 70-80% market share of stablecoin DEX volume
- $9+ billion in total value locked (2024)
- 100x better capital efficiency than Uniswap for stable pairs
- <0.01% slippage on multi-million dollar trades
This lesson explores the mathematics, mechanics, and applications of StableSwap—arguably the most important innovation in AMM design since Uniswap's constant product formula.
The Fundamental Problem with Constant Product
Why xy = k Fails for Stablecoins
Let's start by understanding exactly why the constant product formula is inefficient for assets that should maintain a fixed ratio.
Consider a USDC/DAI Uniswap pool:
- Both tokens worth $1
- Should trade at 1:1 ratio
- But constant product creates artificial scarcity
Mathematical Analysis: The Slippage Problem
Pool setup:
- 1,000,000 USDC (x)
- 1,000,000 DAI (y)
- Constant product: k = 1,000,000² = 10¹²
Trade: Swap 100,000 USDC for DAI
Using the constant product formula (from Lesson 5.2):
Δy = y₀ × Δx / (x₀ + Δx)
Δy_DAI = 1,000,000 × 100,000 / (1,000,000 + 100,000)
= 100,000,000,000 / 1,100,000
= 90,909 DAI
Result:
- Input: 100,000 USDC (worth $100,000)
- Output: 90,909 DAI (worth $90,909)
- Loss: $9,091 (9.1%!)
For a trade between two $1 assets, you lost almost 10% to slippage. This is absurd.
Where Does the Liquidity Go?
The constant product curve distributes liquidity across all possible price points:
DAI Reserve (millions)
4 │
│●
3 │ ●
│ ●
2 │ ●
│ ●_
1 │ ●___
│ ●●●●●___
0 │ ●●●●____
└────┴────┴────┴────┴────┴────> USDC Reserve (millions)
0 1 2 3 4 5
Most liquidity here ↑
(prices far from 1:1)
The problem:
- Stablecoins trade at ~1:1 forever
- But most liquidity prepares for USDC = $10 or DAI = $10
- This liquidity is wasted
Quantifying the waste:
In a balanced 1M/1M pool:
- ~90% of liquidity serves prices from $0.10 to $10
- ~10% of liquidity serves prices from $0.90 to $1.10
- But stablecoins trade $0.99-$1.01 (99.9% of the time)
Effective liquidity utilization: ~1%
Comparison with Ideal Stablecoin AMM
Ideal behavior for stablecoins:
Trade size Ideal slippage Uniswap slippage Waste
10k (1%) 0.001% 0.10% 100x
100k (10%) 0.01% 9.09% 909x
500k (50%) 0.05% 100% 2000x
The larger the trade, the worse constant product performs relative to what's possible.
The Capital Efficiency Disaster
To achieve 1% slippage on a 100k trade with Uniswap, you'd need:
Target: 1% slippage on 100k trade
Required reserves: ~10,000,000 USDC/DAI
Capital required: $20 million
To serve: $100k trade efficiently
Capital efficiency: 0.5%
This means you need $200 of capital to efficiently serve $1 of trading volume. Terrible efficiency.
For comparison:
- Traditional exchanges: $0.001 capital per $1 volume (100,000x better)
- Curve (as we'll see): $2 capital per $1 volume (100x better than Uniswap)
The Two Extremes: Constant Sum vs Constant Product
Before introducing StableSwap, let's understand the two formulas it combines.
Constant Sum: x + y = D
Formula: The sum of reserves stays constant.
x + y = D (constant)
For stablecoins:
- Perfect! Trading 100k USDC for DAI:
Before: 1M USDC + 1M DAI = 2M
After: 1.1M USDC + 0.9M DAI = 2M
Output: 100k DAI exactly
Slippage: 0%
Price calculation:
Taking the derivative:
dx + dy = 0
dy/dx = -1
Price is always 1:1. Perfect for stablecoins!
The fatal flaw: Depletion
You can fully drain one side:
Start: 1M USDC, 1M DAI
Trade: 1M USDC for 1M DAI
End: 2M USDC, 0 DAI
Pool is drained!
Now if anyone wants DAI, the pool is empty. The pool can be completely arbitraged to exhaustion.
Visual representation:
DAI
│
2M├─────●
│ ╱
│ ╱
1M│ ╱ Current state
│ ╱
│╱
0 └─────────> USDC
0 1M 2M
The line hits the axes (complete depletion possible)
Constant Product: xy = k
Formula: The product of reserves stays constant (Lesson 5.2).
xy = k (constant)
For stablecoins:
- Already analyzed: 9% slippage on 10% trade
- Terrible capital efficiency
- Most liquidity serves prices that never occur
Price calculation:
Price = y/x (reserve ratio)
Price moves with every trade, creating slippage.
The key advantage: Cannot deplete
lim(x→0) y = ∞
The curve is asymptotic—it approaches but never touches the axes. You can't fully drain the pool.
Visual representation:
DAI
│
│●
│ ●
│ ●
1M│ ●
│ ●___
│ ●●●___
│ ●●●●____
└────────────────────────> USDC
0 1M
Never touches axes (depletion impossible)
The Trade-off Table
| Property | Constant Sum | Constant Product | Ideal for Stables |
|---|---|---|---|
| Slippage at 1:1 | 0% ✓ | High ✗ | Low ✓ |
| Can deplete | Yes ✗ | No ✓ | No ✓ |
| Capital efficiency | Infinite* ✓ | Low ✗ | High ✓ |
| Price stability | Perfect ✓ | Poor ✗ | Good ✓ |
| Safety | None ✗ | High ✓ | High ✓ |
*But fails catastrophically when depleted
The insight: We need a curve that:
- Behaves like constant sum near equilibrium (low slippage)
- Behaves like constant product far from equilibrium (no depletion)
This is exactly what StableSwap achieves.
Curve's Innovation: The StableSwap Invariant
The Hybrid Formula
StableSwap combines constant sum and constant product via an amplification coefficient A:
StableSwap invariant (2 assets):
A × n² × (x + y) + D = A × D × n² + D^(n+1) / (n² × xy)
Where:
- x, y = token reserves
- D = invariant (like k in constant product)
- n = number of tokens (2 in this case)
- A = amplification coefficient
For n=2, this simplifies to:
4A(x + y) + D = 4AD + D³/(4xy)
Rearranging:
4A(x + y) + D = 4AD + D³/(4xy)
4A(x + y - D) = D³/(4xy) - D
4A(x + y - D) = D(D²/(4xy) - 1)
Understanding Through Limiting Behavior
As A → ∞:
4A(x + y - D) >> D(D²/(4xy) - 1)
x + y ≈ D
Behaves like constant sum!
As A → 0:
D³/(4xy) - D >> 4A(x + y - D)
D³/(4xy) ≈ D
D² ≈ 4xy
D ≈ 2√(xy)
Behaves like constant product!
For intermediate A:
- Near equilibrium (x ≈ y ≈ D/2): Constant sum dominates
- Far from equilibrium: Constant product takes over
Visual Understanding
y (DAI reserves)
│
│ A = ∞ (constant sum)
│ ╱
│ ╱ A = 100
│ ╱────
│╱─────── A = 10
│────────── A = 1
│──────────── A = 0 (constant product)
│
└─────────────────> x (USDC reserves)
Equilibrium (x=y) marked by vertical line
Key observations:
-
Near equilibrium (x ≈ y): All curves are similar, but higher A means flatter (less slippage)
-
Far from equilibrium: Higher A means sharper curve (more protection against depletion)
-
At extremes: All curves converge to constant product behavior (asymptotic)
The Mathematical Beauty
StableSwap elegantly interpolates between two extremes:
Near center: Flat like constant sum (efficient)
↓
StableSwap Curve
↓
At edges: Curved like constant product (safe)
The amplification coefficient A controls how much of each behavior you get.
The Amplification Coefficient: Deep Dive
The A parameter is the most important design choice in a StableSwap pool. Let's understand it thoroughly.
What A Represents
Intuition: A amplifies the constant sum behavior near equilibrium.
Mathematically:
When x ≈ y ≈ D/n, the StableSwap formula becomes:
Price deviation from 1:1 ∝ 1/A
Higher A → smaller price deviation → flatter curve → less slippage
Physical interpretation:
Think of A as "how hard the pool tries to maintain 1:1 ratio"
- A = 1: Barely tries (almost constant product)
- A = 100: Tries moderately hard
- A = 10,000: Tries extremely hard (almost constant sum)
Numerical Examples: Different A Values
Let's compare the same trade across different A values.
Setup:
- Pool: 10M USDC, 10M DAI
- Trade: Swap 1M USDC for DAI
- Trade size: 10% of pool
A = 1 (minimal amplification):
Output: ~909,090 DAI
Slippage: 9.09%
Behavior: Almost identical to constant product
A = 10:
Output: ~980,000 DAI
Slippage: 2.0%
Behavior: Somewhat flatter
A = 100:
Output: ~998,000 DAI
Slippage: 0.2%
Behavior: Much flatter
A = 1,000:
Output: ~999,800 DAI
Slippage: 0.02%
Behavior: Very flat
A = 10,000:
Output: ~999,980 DAI
Slippage: 0.002%
Behavior: Nearly constant sum
A = ∞ (constant sum):
Output: 1,000,000 DAI
Slippage: 0%
The Slippage vs A Curve
Slippage (%) on 10% trade
10│ A=1
│ ●
│
│
5│ A=10
│ ●
│
│ A=100
1│ ●
│ A=1000
│ ●
0 │ ●────● A=10000
└───┴────┴────┴────┴──── ┴────┴───> A value
1 10 100 1k 10k 100k
Key insight: Returns diminish. Going from A=100 to A=1000 helps more than A=1000 to A=10000.
Choosing the Optimal A
Factors to consider:
1. Asset correlation
Tightly correlated (USDC/DAI):
- Expected deviation: ±0.1%
- Can use high A (2000-5000)
- Maximum efficiency
Moderately correlated (WBTC/renBTC):
- Expected deviation: ±1%
- Use moderate A (100-500)
- Balanced approach
Loosely correlated (stETH/ETH):
- Expected deviation: ±5%
- Use low A (20-100)
- Prioritize safety
2. De-peg risk
High de-peg risk:
- Use lower A
- More protection when peg breaks
- Example: Algorithmic stablecoins
Low de-peg risk:
- Use higher A
- Maximize efficiency
- Example: USDC/USDT (both fiat-backed)
3. Historical data
Curve analyzes historical price spreads:
Max historical deviation × 2 → Suggested A range
For USDC/DAI:
- Max historical spread: ~0.5%
- Suggested A: 1000-2000
4. Risk tolerance
Conservative pool operators:
- Use lower A
- Accept higher slippage
- Prioritize safety
Aggressive pool operators:
- Use higher A
- Minimize slippage
- Accept de-peg risk
Real Curve Pools: A Parameter Examples
| Pool | Assets | A Value | Rationale |
|---|---|---|---|
| 3pool | DAI/USDC/USDT | 2000 | Core stables, fiat-backed |
| sUSD | sUSD/3pool | 2000 | Synthetix USD stable |
| FRAX | FRAX/3pool | 5000 | Highly correlated to 3pool |
| sBTC | WBTC/renBTC/sBTC | 100 | BTC variants, less tight |
| stETH | stETH/ETH | 50 | Staking derivative, can diverge |
| alUSD | alUSD/3pool | 2000 | Collateralized stable |
| MIM | MIM/3pool | 200 | Moderate de-peg risk |
Adjusting A: The Risks
Curve allows governance to adjust A, but it's extremely dangerous.
Why dangerous?
Changing A changes the bonding curve, which changes implied prices. If done suddenly, arbitrageurs can profit from the discontinuity.
Example attack:
1. Pool at A=100, price 1:1
2. Governance changes A→2000 instantly
3. Curve becomes much flatter
4. At old reserves, implied price shifts
5. Arbitrageur drains advantage before rebalancing
Curve's solution: Gradual ramping
A changes gradually over days:
Day 0: A = 100
Day 1: A = 110 (+10%)
Day 2: A = 121 (+10%)
...
Day 30: A = 1,744
This gives arbitrageurs time to rebalance the pool at each step, preventing exploitation.
The A Parameter Formula
For those interested, A appears in the price calculation.
Price in StableSwap:
p = dy/dx =
[An²(Σxᵢ - D) + D - D^(n+1)/(n^n × (∏xᵢ)/xⱼ²)] /
[An²(Σxᵢ - D) + D - D^(n+1)/(n^n × (∏xᵢ)/xᵢ²)]
This is complex because the invariant is implicit. In practice, prices are computed numerically.
Key intuition:
The price formula includes terms with A as a coefficient on the constant sum part, and terms with 1/A behavior from the constant product part. The balance determines the price.