Curve's StableSwap Invariant — Math Deep Dive
The StableSwap invariant is what makes Curve the canonical venue for stablecoin swaps. By blending constant-sum and constant-product curves through an amplification parameter A, Curve achieves slippage 10–100× lower than Uniswap for pegged assets. Let's open the hood.
📐 The Hybrid Invariant — Two Curves, One Formula
The StableSwap invariant for N assets with amplification A is:
But trivially drainable — one asset can be completely bought out.
Safe at depeg but expensive slippage for pegged assets.
Amplification A controls the transition point
🎚️ Interactive: How A Shapes the Curve
Move the A slider to see how amplification changes the curve shape. Higher A = flatter near the peg = less slippage. Notice how the curve "bends away" from the diagonal at low A, and hugs it tightly at high A (until you go too far and it becomes drainable).
📊 Slippage Comparison: Curve vs Uniswap
For a $1M stablecoin→stablecoin swap, Curve's StableSwap is 10–100× cheaper than Uniswap. For volatile assets (ETH/USDC), the advantage disappears because both ends should trade at market.
| Pool Type | $100K Swap | $1M Swap | $10M Swap | $100M Swap |
|---|---|---|---|---|
| Curve 3pool (A=2000) | 0.001% | 0.01% | 0.10% | 1.5% |
| Curve ETH/mBTC (A=400) | 0.003% | 0.03% | 0.35% | 4.1% |
| Uniswap V2 (constant-product) | 0.10% | 1.00% | 10.0% | 100%+ |
| Uniswap V3 (concentrated, mid) | 0.02% | 0.25% | 2.5% | 25%+ |
🧮 Computing D — Step by Step
D is solved iteratively. Start with a guess, then refine until the change is negligible. For a 2-asset pool with x = 10M USDC, y = 10M USDT, A = 100:
⚠️ What Happens When a Stablecoin Depigs
Curve pools are designed to degrade gracefully. When one stablecoin depegs, arbitrageurs trade against the pool: buy the discounted asset (pushing it back toward $1), while selling the expensive one. The pool's invariant shifts from flat to curved, acting like a constant-product AMM.
A pool running A=2000 can have the majority of its liquidity drained in a single block if the depeg is severe. This is because the curve is nearly flat — a small price deviation moves the invariant a long way. Lower-A pools (A=50–100) lose less to arbitrage but also have worse slippage during normal operations.
- What does the amplification parameter A actually control?
- A interpolates the StableSwap curve between constant-sum (zero slippage, easy to drain) and constant-product (Uniswap-style). Higher A flattens the curve near the peg, making trades feel almost frictionless when reserves are balanced. But higher A also means a larger share of the pool can be drained if one asset depegs. Curve's most popular stablecoin pools run A between 100 and 2000 depending on collateral confidence.
- Why is slippage near-zero on Curve for stablecoins?
- Because near the peg (when token ratios are within ~1%), the StableSwap invariant behaves almost like x + y = k (constant-sum), which has no price movement regardless of trade size. Uniswap's x·y=k means every trade moves the price proportionally. For a $1M stablecoin swap, Curve might charge 0.01% slippage vs Uniswap's ~0.3%.
- What happens to a Curve pool when one stablecoin depegs?
- The curve gracefully degrades from flat (constant-sum) toward curved (constant-product). If USDC depegs to $0.95, the pool starts treating it like a volatile asset — the invariant becomes x·y=k-style, arbitrageurs drain the discounted leg, and the pool naturally corrects the peg. The danger is acute for high-A pools: a large depeg can drain a bigger share of pool depth than a lower-A pool would allow.
- How is D (the invariant) actually computed?
- D is solved iteratively using Newton's method. Starting from an initial guess of the sum of reserves, each iteration refines D until convergence: D_{new} = D - (A·n^n·sum + D - D^{n+1}/(n^n·product)) / (A·n^n - D^{n+2}/(n^n·D·product)). For a 2-coin pool, this converges in 1–2 iterations in practice. The formula An^n·sum(x_i) + An^n·product/D - D = 0 defines the invariant surface.
- Why do different Curve pools have different A values?
- A is a risk parameter. High-confidence stablecoins like USDC/USDT can run A=2000 because the peg is extremely tight — the flat region is safe. Newer or riskier stablecoins need lower A to limit drainability at depeg. Pool creators tune A based on how much they trust each asset's peg stability, similar to how lending protocols set different collateral factors.
- How does CryptoSwap (V2) differ mathematically from StableSwap (V1)?
- CryptoSwap adds an exponential moving average (EMA) repegging mechanism. The pool tracks its internal price as an EMA, and the invariant is adjusted to target that EMA rather than the spot ratio of reserves. This lets non-pegged assets like ETH/WBTC trade in a single pool with concentrated, self-rebalancing liquidity — something StableSwap can't handle because a constant-sum for ETH/USDC would be trivially drainable.