> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kimia.live/llms.txt
> Use this file to discover all available pages before exploring further.

# Funding Rate

> How Kimia computes, clamps, and settles funding payments.

<style>
  {`
    .katex-display {
      overflow-x: auto;
      overflow-y: hidden;
      scrollbar-width: none;
      -ms-overflow-style: none;
    }
    .katex-display::-webkit-scrollbar {
      display: none;
      height: 0;
      width: 0;
    }
    `}
</style>

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 $M$ be `mark_twap`, $O$ be `oracle_twap`, and $P$ the current oracle price.

$$
\text{spread} = (M - O) + \tfrac{O}{5000}
$$

$$
\text{clamped} = \text{clamp}\!\left(\text{spread},\; -\tfrac{P}{33},\; +\tfrac{P}{33}\right)
$$

$$
\text{funding\_rate} = \frac{\text{clamped} \times \text{FUNDING\_PRECISION}}{O \times \text{PERIODS\_PER\_DAY}}
$$

The $O/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/33$)
caps the worst-case per-period payment.

Breaking it down:

<AccordionGroup>
  <Accordion title="Why a carry offset?">
    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.
  </Accordion>

  <Accordion title="Why TWAP, not spot?">
    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.
  </Accordion>

  <Accordion title="Why the ±3% clamp?">
    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.
  </Accordion>
</AccordionGroup>

## TWAP update

$$
\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:

$$
\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.

## Read next

<CardGroup cols={2}>
  <Card title="kimia-perp reference" icon="book" href="/programs/kimia-perp">
    Funding instructions and accounts.
  </Card>

  <Card title="Run a keeper" icon="robot" href="/guides/run-a-keeper">
    Script to crank funding + liquidations.
  </Card>
</CardGroup>
