# Funding Payments

Perpetual contracts rely on a scheduled payment between longs and shorts known as funding payments. Funding payments are meant to converge the price between the derivative contract (mark price) and its underlying (index price).

Funding payments on Nibi-Perps occur every half-hour. This setup is analogous to a traditional future that expires once a day. If a perp trades consistently at 2% above its underlying index price, the funding payments would amount to 2% of the position size after a full day.

If the funding rate is positive, mark price is greater than index price and longs pay shorts. Nibi-Perps automatically deducts the funding payment amount from the margin of the long positions.

Here, position size refers to amount of base asset represented by the derivative. I.e., a BTC:USD perp with 7 BTC of exposure would have a position size of 7.

# Price Divergence

When the mark price deviates from the index price, there is a price divergence calculated as

D:=Ξ”PPindex=Pmarkβˆ’PindexPindexD := \frac{\Delta P}{P_{\text{index}}} = \frac{ P_{\text{mark} } - P_{ \text{index}} }{ P_{\text{index}} }

where DD is the price divergence (unitless), PmP_m is the mark price (units of USDC/base), and PindexP_{\text{index}} is the index price (units of USDC/base). When people refer to the β€œfunding rate”, they are referring to the price divergence.

We call the term, Ξ”P=Pmarkβˆ’Pindex\Delta P = P_{\text{mark}} - P_{\text{index}}, the premium.

# Funding Payment

The funding payment a trader pays (or receives) is directly proportional to the price divergence and their position size at the time the funding payment is calculated (once every 30 minutes on the Nibiru Chain).

Variable Description
FpayF_{\text{pay}} funding payment in quote units (e.g. USD)
SS Size of all perp positions in base units
nn Payment frequency. The number of daily payments.

Fpay:=Sβ‹…Pindexβ‹…(Frate):=Sβˆ—Pindexβˆ—Dn\begin{aligned} & F_{\text{pay}} := S \cdot P_{\text{index}} \cdot \left( F_{\text{rate}} \right) \\ &\quad\quad := S * P_{\text{index}} * \frac{D}{n} \end{aligned}

Given that the Nibiru has 30 minute funding intervals, there are n=48 payments per day.

Substituting in D=Ξ”PPindexD = \frac{ \Delta P }{ P_{\text{index}} } and re-arranging terms, we get

Fpay=Sβˆ—Ξ”PnF_{\text{pay}} = S * \frac{\Delta P}{n}

The term Ξ”Pn\dfrac{\Delta P}{n} is called the premium fraction.

# It's better to store premium fraction than price divergence.

Recall from our funding payment equations above that the funding payment is proportional to the index price at the time of the funding rate interval.

Fpay=Sβˆ—Pindexβˆ—Dn=Sβˆ—Ξ”PnF_{\text{pay}} = S*\frac{P_{\text{index}} *D}{n} = S * \frac{\Delta P}{n}

Over several funding intervals, there will be new prices at which we compute the funding rate that needs to be applied to each position. There are two main approaches for performing this computation.

  1. Naive Approach: One way to account for each of these payments is to store a history of the index price and divergence at each funding time. The unrealized payments could then be calculated and applied to the positiion the next time it undergoes a size change.

  2. Cumulative Approach: Instead of keeping track of DD and PindexP_{\text{index}} separately, it’s more efficient to combine them together and store them as Dβˆ—Pindexn=Ξ”Pn\dfrac{D*P_{\text{index}}}{n} = \dfrac{\Delta P}{n}. Then, we only need to store the term Ξ”Pn\dfrac{\Delta P}{n} on-chain and multiply by SS when calculating the unrealized funding payment for a position.

Nibiru uses the cumulative approach and realizes funding payments lazily.

# Lazily Realized Funding Payments

A funding payment, FpayF_{\text{pay}} , occurs at a funding time tt. The net funding for the exchange is then Fpay,EXF_{\text{pay},\text{EX}}

Fpay,EX=βˆ‘u∈UsersFpay(u)=βˆ‘u∈UsersS(u)βˆ—Ξ”PnF_{\text{pay},\text{EX}} = \sum_{u\in\text{Users}} F_{\text{pay}}(u) = \sum_{u\in\text{Users}} S(u) * \frac{\Delta P}{n}

Or more correctly accounting for time, we’d write

Fpay,EX(t)=βˆ‘u∈UsersFpay(u,t)F_{\text{pay},\text{EX}}(t) = \sum_{u\in\text{Users}} F_{\text{pay}}(u, t)

At the end of a funding rate interval, the premium fraction, Ξ”P(t)n\dfrac{\Delta P (t)}{n}, is recomputed using the current mark and index TWAPs. Summing premium fractions over multiple funding intervals produces a cumulative premium fraction (CPF).

Nibiru stores the latest cumulative premium fraction on-chain to determine how much a trader should pay (or receive) in funding.

cumulativePremiumFraction=βˆ‘t∈TΞ”P(t)n=exchangeCPF\text{cumulativePremiumFraction} = \sum_{t\in T} \frac{\Delta P (t)}{n} = \text{exchangeCPF}

Every position keeps track of what the most recent CPF value when undergoing a state change. A state change for a position refers to the execution of any perp module message such as MsgOpenPosition, MsgClosePosition, MsgAddMargin, MsgRemoveMargin, or MsgLiquidate.

The next time that position changes, the difference between the exchange’s latest CPF and the position’s CPF is the unrealized premium fraction that needs to be accounted for on the trader’s position. This value is equivalent to the percentage of the trader’s size that needs to be payed or received in funding.

unrealizedPremiumFraction=exchangeCPF-positionCPF\text{unrealizedPremiumFraction = exchangeCPF - positionCPF}

This method of allowing positions to backpay for missed funding payments is often referred to as β€œcall by need” or β€œlazy evaluation” in the computing world.

Q: Why are funding payments realized lazily?

Funding payments are realized lazily to prevent a performance bottleneck. Realizing all funding payments at each funding interval would mean iterating over all of the positions for each market at each funding interval. As the number of positions and markets grows on Nibi-Perps, this computation becomes impractical.


# Perpetual Funding Rates on dYdX

The key difference between Nibi-Perps and dYdX funding rates is that Nibiru uses Frate=DnF_{\text{rate}} = \dfrac{D}{n}.

(Nibiru)Frate=Dn(dYdX)Frate=Dn+(Irateβˆ—Pindex) \begin{aligned} &(\text{Nibiru})\quad &F_{\text{rate}} &= \frac{D}{n} \\ & & \\ &(\text{dYdX})\quad &F_{\text{rate}} &= \frac{D}{n} + (I_{\text{rate}}*P_{\text{index}}) \end{aligned}

The resulting funding payment on dYdX is

Fpay=Sβ‹…Pindexβ‹…(Frate)=Sβ‹…Pindexβ‹…(Pmarkβˆ’PindexPindexβ‹…n+Irate).\begin{aligned} F_\text{pay} &= S \cdot P_{\text{index}} \cdot \left(F_{\text{rate}}\right) \\ &= S \cdot P_{\text{index}} \cdot \left(\dfrac{P_{\text{mark}} - P_{\text{index}}} {P_{\text{index}} \cdot n} + I_{\text{rate}}\right). \end{aligned}

# References