Skip to main content
Funding keeps the perp price tracking the spot price. If perp > spot, longs pay shorts; if perp < spot, shorts pay longs. Kimia’s formula is Drift-inspired with an explicit cost-of-carry offset.

Formula

Let MM be mark_twap, OO be oracle_twap, and PP the current oracle price. spread=(MO)+O5000\text{spread} = (M - O) + \tfrac{O}{5000} clamped=clamp ⁣(spread,  P33,  +P33)\text{clamped} = \text{clamp}\!\left(\text{spread},\; -\tfrac{P}{33},\; +\tfrac{P}{33}\right) funding_rate=clamped×FUNDING_PRECISIONO×PERIODS_PER_DAY\text{funding\_rate} = \frac{\text{clamped} \times \text{FUNDING\_PRECISION}}{O \times \text{PERIODS\_PER\_DAY}} The O/5000O/5000 term (≈ 0.02% per period) is a cost-of-carry offset that nudges longs to pay shorts even when mark equals oracle. The ±3.03% clamp (via P/33P/33) caps the worst-case per-period payment. Breaking it down:
Even when mark == oracle exactly, perpetuals structurally benefit longs (they get price exposure without owning the asset). The offset charges longs ≈0.02% per period (≈1.75% annualized) to compensate shorts for providing the counterparty liquidity.
Using raw prices lets a single fill near the funding crank time swing the rate. TWAPs smooth that out. Both mark and oracle are tracked as 1-hour exponential moving averages.
Caps the worst-case per-period payment. A 3.03% clamp per hour is ≈26.5% annualized, enough to keep the peg alive but not enough for a single manipulated hour to nuke open interest.

TWAP update

twap_new=twap_old+(pricetwap_old)×elapsedperiod\text{twap\_new} = \text{twap\_old} + \frac{(\text{price} - \text{twap\_old}) \times \text{elapsed}}{\text{period}} Clamped so that if elapsed ≥ period, twap_new = price exactly (the memory has washed out).
  • Period: 3600s
  • Funding period: 3600s (minimum interval between update_funding_rate calls)

Settlement

Funding is accumulated globally (cumulative_funding_rate_long, _short) and applied lazily per user: payment=(cumulative_nowuser_last_cumulative)×base_amountFUNDING_PRECISION×1000\text{payment} = \frac{(\text{cumulative\_now} - \text{user\_last\_cumulative}) \times \text{base\_amount}}{\text{FUNDING\_PRECISION} \times 1000} If payment > 0 and the user is long, collateral decreases (they pay). If the user is short, collateral increases (they earn). Signs flip when payment < 0. The divisor 1000 bridges the BASE_PRECISION / QUOTE_PRECISION ratio. Settlement happens automatically on every user trade, and on-demand via settle_funding (permissionless; anyone can crank any user account).

Who runs the crank?

update_funding_rate is permissionless. In practice:
  • Protocol itself: delta-vault calls settle_funding when claiming yield.
  • Keeper bots: Any third party can call update_funding_rate once per period to bank the current rate. No reward is hard-coded in the protocol, but integrators can layer MEV or incentive schemes on top.

kimia-perp reference

Funding instructions and accounts.

Run a keeper

Script to crank funding + liquidations.