DEV Community

BaniPool
BaniPool

Posted on Edited on Originally published at banipool.com AI-assisted

Impermanent loss for Uniswap v3 ranges, in 25 lines of JavaScript

Most impermanent loss (IL) calculators only handle the classic full-range pool (Uniswap v2, x·y=k). But most liquidity today sits in concentrated ranges — Uniswap v3/v4, Orca Whirlpools, Meteora DLMM — where IL behaves very differently.

This post derives IL for a symmetric range, gives you a ~25-line function you can paste into a console, and shows a table of real numbers.

Full range: the formula you already know

With r = new price / starting price, a 50/50 full-range position is worth √r (normalised to 1 at the start), while simply holding the two tokens is worth (1 + r) / 2:

IL = 2·√r / (1 + r) − 1
Enter fullscreen mode Exit fullscreen mode

A 2× price move (r = 2) gives −5.72%. Small, which is why full-range LPs often shrug it off.

Concentrated range: where it gets interesting

In Uniswap v3 you provide liquidity L only between prices pa and pb. Inside the range, with s = √price:

token X = L · (1/s − 1/√pb)
token Y = L · (s − √pa)
Enter fullscreen mode Exit fullscreen mode

Below pa the position is 100% token X; above pb it is 100% token Y — and it earns no fees in either case.

Normalising the start price to 1 and the position to $1, we can solve for L and compare the position with holding the starting amounts.

The code

// Value of an LP position vs HODL after the price moves by `move` (e.g. -0.25 = -25%).
// width = half-width of a symmetric range (0.1 = ±10%); null = full range (x*y=k).
function ilVsHodl(move, width = null) {
  const r = 1 + move; // new price / starting price
  if (width === null) return Math.sqrt(r) / (0.5 * r + 0.5) - 1;

  const sa = Math.sqrt(1 - width), sb = Math.sqrt(1 + width), s = Math.sqrt(r);
  const L = 1 / ((1 - 1 / sb) + (1 - sa)); // liquidity for a $1 position at price 1
  const x0 = L * (1 - 1 / sb), y0 = L * (1 - sa); // starting token amounts

  let value;
  if (r <= 1 - width) value = L * (1 / sa - 1 / sb) * r; // below range: all token X
  else if (r >= 1 + width) value = L * (sb - sa); // above range: all token Y
  else value = L * (1 / s - 1 / sb) * r + L * (s - sa);

  return value / (x0 * r + y0) - 1;
}

// How many times more liquidity per dollar than a full-range position.
const concentration = (w) => 2 / (2 - Math.sqrt(1 - w) - 1 / Math.sqrt(1 + w));
Enter fullscreen mode Exit fullscreen mode

Run it:

for (const m of [-0.5, -0.25, -0.1, 0.1, 0.25, 0.5, 1]) {
  const row = [null, 0.25, 0.1].map((w) => (ilVsHodl(m, w) * 100).toFixed(2) + '%');
  console.log(`${(m * 100).toFixed(0)}%`.padStart(5), row.join('  '));
}
Enter fullscreen mode Exit fullscreen mode

The numbers

Position value vs holding, before fees. * = the price has left the range (no fees earned).

Price move Full range ±25% range ±10% range
−50% −5.72% −30.32%* −32.54%*
−25% −1.03% −8.42%* −12.46%*
−10% −0.14% −1.15% −2.83%*
+10% −0.11% −0.95% −2.32%*
+25% −0.62% −5.24%* −8.55%*
+50% −2.02% −13.79%* −17.34%*
+100% −5.72% −26.98%* −30.66%*

And the upside — capital concentration vs full range:

Range Liquidity per dollar
±25% ≈ 8.3×
±10% ≈ 20.4×
±5% ≈ 40.5×

What this means in practice

  • A ±10% range earns ~20× the fees of full range per dollar — but only while the price stays inside. A 10% move already costs 2–3% vs holding, and the position stops earning.
  • IL is roughly symmetric around the range, but once out of range the loss keeps growing while fees are zero. Out-of-range time is the real cost of a narrow range.
  • The useful question is therefore not "what is my IL?" but "what share of the time will the price stay in my range, and do the fees in that window beat the IL?"

That last question needs real data: the pool's fee APR and its recent price history. I built a free, no-wallet simulator that does exactly that for live pools on 9 EVM networks and Solana — pick a pool, a range and a duration, and it estimates fees from the pool's fee APR × the share of the last 7 days the price spent in your range, next to the IL table above: banipool.com/en/simulasi.

The full write-up (with FAQ) is on the BaniPool blog. Questions or corrections on the maths are very welcome in the comments.

Top comments (0)