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

perps · funding rates · gmx · synthetix

Liquidity LuVSThe Sandwich Bot
Liquidity Lu saysPerpetual futures use a funding rate to drive the mark price toward the index: when mark > index, longs pay shorts; when mark < index, shorts pay longs — every funding period.

Perpetuals have no expiry, so they need a mechanism to anchor them to the spot index. The funding rate does it: it is computed every funding period (8h typical) as roughly (mark − index) / index / periodsPerDay. Longs pay shorts that amount per unit position when mark exceeds index; shorts pay longs when mark falls below.

This aligns incentives without expiry: traders pay (or receive) to stay open, pushing mark back toward index. GMX, Synthetix, dYdX, and Hyperliquid all use variants. The demo shows funding across three mark/index scenarios.

Power-ups you unlock

The Sandwich Bot attacks — common mistakes

Boss battleCompute the 8-hour funding rate for mark/index pairs (2050/2000), (1980/2000), and (2000/2000).

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>
function funding(mark, index, periodH){
  const premium = (mark - index) / index;
  return premium / (24 / periodH);
}
const cases = [[2050, 2000], [1980, 2000], [2000, 2000]];
const rows = cases.map(([m, i]) => {
  const f = funding(m, i, 8);
  const dir = f > 0 ? 'longs pay shorts' : f < 0 ? 'shorts pay longs' : 'no funding (anchored)';
  return '  mark=' + m + ' index=' + i + '  → funding ' + (f*100).toFixed(4) + '% / 8h   (' + dir + ')';
});
document.getElementById('o').textContent = [
  'perp funding rate per 8h period:',
  '',
  ...rows,
  '',
  'positive funding pulls mark down toward index; negative pulls it up'
].join('\n');
</script></body></html>
▶ Open the interactive comic issue
‹ Makerdao · Dai · The Peg Stability ModuleToken Engineering · Supply & Demand Sinks ›