freecoding.school100% FREE · NO SIGNUP
Yield OceanISSUE #5 of 34

curve · the stableswap invariant

Liquidity LuVSThe Sandwich Bot
Liquidity Lu saysCurve’s stableswap invariant blends constant-sum (flat near peg) with constant-product (steep at the edges) — high amplification keeps two stables trading near 1:1 with tiny slippage.

Curve's stableswap invariant interpolates between constant-sum (flat) and constant-product (curved) depending on an amplification coefficient A. Near the peg, the curve is nearly flat — trades happen with almost no slippage. As reserves drift away from equal weight, it bends sharply, raising slippage and discouraging trading away from peg.

Real Curve solves for D iteratively each swap. The demo uses a stylized model showing slippage staying near zero while balanced and ramping as reserves skew.

Power-ups you unlock

The Sandwich Bot attacks — common mistakes

Boss battleShow stableswap slippage staying small near 50/50 balance and rising as the pool skews to 90/10.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre id="o"></pre>
<script>
// stylized: real Curve solves D iteratively. this shows the qualitative behaviour.
function slippage(A, x, y){
  const r = x / (x + y);
  const skew = Math.abs(r - 0.5);
  return (Math.pow(skew * 2, 3) / A);
}
const A = 100;
const rows = [];
for(const x of [5000, 4500, 4000, 3000, 2000, 1000]){
  const y = 10000 - x;
  const ratio = x / (x + y);
  rows.push('  ratio x/(x+y)=' + ratio.toFixed(2) + '  slippage ≈ ' + (slippage(A, x, y) * 100).toFixed(4) + '%');
}
document.getElementById('o').textContent = [
  'stableswap (amp=' + A + ', stylized):',
  ...rows,
  '',
  'near 50/50 → near-zero slippage; toward 90/10 → slippage ramps fast'
].join('\n');
</script></body></html>
▶ Open the interactive comic issue
‹ Uniswap V4 · Hooks · The SingletonBalancer · Weighted & Boosted Pools ›