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

impermanent loss · the math

Liquidity LuVSThe Sandwich Bot
Liquidity Lu saysImpermanent loss is the gap between holding and providing to a v2-style AMM: IL = 2√r/(1+r) − 1 where r = priceNew/priceOld — symmetric, monotone, brutal at extreme moves.

Impermanent loss (IL) is the underperformance of being an AMM LP versus just holding the two tokens. The closed form for v2-style pools is IL = 2√r / (1+r) − 1 where r is the price ratio at exit vs entry. At r=1: IL=0. At r=2: ≈ −5.7%. At r=5: ≈ −25%. At r=100: ≈ −80%.

It is "impermanent" only if the price reverts; otherwise the loss is realized at withdrawal. Fees earned must exceed IL for LPing to pay. The demo computes IL across a range of price ratios.

Power-ups you unlock

The Sandwich Bot attacks — common mistakes

Boss battleCompute IL at price ratios 1x, 2x, 5x, and 10x and confirm symmetry between r and 1/r.

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>
const il = (r) => 2 * Math.sqrt(r) / (1 + r) - 1;
const ratios = [1, 1.25, 1.5, 2, 5, 10, 100];
const rows = ratios.map(r => '  price ratio ' + String(r).padStart(4) + 'x → IL = ' + (il(r)*100).toFixed(2) + '%');
const sym = [[2, 0.5], [5, 0.2], [10, 0.1]].map(([a,b]) => '  IL(' + a + ') = ' + (il(a)*100).toFixed(4) + '%   IL(' + b + ') = ' + (il(b)*100).toFixed(4) + '%   symmetric? ' + (Math.abs(il(a) - il(b)) < 1e-9));
document.getElementById('o').textContent = [
  'impermanent loss (v2-style pools): IL = 2√r/(1+r) − 1',
  '',
  ...rows,
  '',
  'symmetry check:',
  ...sym
].join('\n');
</script></body></html>
▶ Open the interactive comic issue
‹ Balancer · Weighted & Boosted PoolsTwap Oracles · Manipulation Resistance ›