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

balancer · weighted & boosted pools

Liquidity LuVSThe Sandwich Bot
Liquidity Lu saysBalancer pools generalize the constant product to N tokens with arbitrary weights: the invariant ∏(B_i^w_i) replaces 50/50 with any allocation that sums to 1.

Balancer generalizes Uniswap to N tokens with arbitrary weights. The invariant is V = ∏ B_i^(w_i) with weights w_i summing to 1. A classic 80/20 pool gives an LP 80% exposure to one asset and 20% to another. Spot price between two tokens is (B_b/w_b) / (B_a/w_a).

This lets a single pool act as a self-balancing index fund — perfect for governance tokens that want exposure to ETH without selling. The demo computes spot price and the invariant for an 800/200 pool with 80/20 weights.

Power-ups you unlock

The Sandwich Bot attacks — common mistakes

Boss battleFor a (800, 200) pool with weights (0.8, 0.2), compute the spot price and the invariant V.

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 Ba = 800, Bb = 200, wa = 0.8, wb = 0.2;
const spot = (Bb/wb) / (Ba/wa);
const V = Math.pow(Ba, wa) * Math.pow(Bb, wb);
document.getElementById('o').textContent = [
  'Balancer 80/20 pool:  B_a=' + Ba + ', B_b=' + Bb + ', w_a=' + wa + ', w_b=' + wb,
  '  weights sum: ' + (wa+wb),
  '  spot price B_b per B_a: (B_b/w_b)/(B_a/w_a) = (' + (Bb/wb) + ')/(' + (Ba/wa) + ') = ' + spot,
  '  invariant V = B_a^w_a · B_b^w_b = ' + V.toFixed(4),
  '',
  '80/20 pool gives the LP 80% A-exposure with built-in rebalancing'
].join('\n');
</script></body></html>
▶ Open the interactive comic issue
‹ Curve · The Stableswap InvariantImpermanent Loss · The Math ›