# Funding Curves
Purpose of the note is to explore different options for open interest (OI) imbalance drawdown, given current funding rates are rather extreme for small sizes.
Also, below generalizes all the prior math in [Section II.B of WP](https://planckcat.mypinata.cloud/ipfs/QmVMX7DH8Kh22kxMyDFGUJcw1a3irNPvyZBtAogkyJYJEv), showing the protocol's main degree of freedom in choosing different funding curves is choice of time evolution of OI imbalance.
## Context
The protocol takes on the profit liability associated with an imbalance in open interest
$$
\mathrm{OI}_{imb}(t) = \mathrm{OI}_o(t) - \mathrm{OI}_u(t)
$$
where $\mathrm{OI}_o$, $\mathrm{OI}_u$ are the aggregate open interest amounts (for all outstanding positions) on the overweight and underweight sides of the market.
To account for the risk this outstanding liability creates, the protocol assesses a funding rate based on the magnitude of the imbalance, incentivizing longs to balance shorts. The funding payment at each time $t$ is split based on the liablity assumed. A pro-rata share of the funding payment is "burned" based on the liability the protocol assumes through the open interest imbalance, with the remaining share transferred from the overweight to the underweight side.
This "payment" is in units of open interest, which means funding affects collateral *and* position size.
## Infinitesimal Evolution
Assume funding payments happen continuously. How much funding should be paid by the overweight to the underweight side and burned by the protocol at any time $t$ can be determined by setting behavior for infinitesimal time changes.
Given a state for the open interest on the overweight and underweight sides at $t$, the state at time $t+dt$ for infinitesimally small $dt$ will be
$$
\begin{align}
d\mathrm{OI}_o(t) &= -d\mathrm{FP}(t) \\
d\mathrm{OI}_u(t) &= [1-b_r(t)] \cdot d\mathrm{FP}(t) \\
d\mathrm{OI}_{b_r}(t) &= b_r(t) \cdot d\mathrm{FP}(t)
\end{align}
$$
where $d\mathrm{FP}(t)$ is the funding payment paid by the overweight open interest side.
Transforming infinitesimal equations in terms of open interest imbalance and total open interest
$$
\mathrm{OI}_{tot}(t) \equiv \mathrm{OI}_o(t) + \mathrm{OI}_u(t),
$$
one has time evolution
$$
\begin{align}
d\mathrm{OI}_{tot}(t) & = -b_r(t) \cdot d\mathrm{FP}(t) \\
d\mathrm{OI}_{imb}(t) & = -[2-b_r(t)] \cdot d\mathrm{FP}(t)
\end{align}
$$
The pro-rata share of the overweight contracts the protocol is liable for and therefore the share of the funding payment "burned" to compensate the protocol for the risk it assumes is
$$
b_r(t) = \frac{\mathrm{OI}_{imb}(t)}{\mathrm{OI}_o(t)}
$$
$\mathrm{OI}_{b_r}$ is the cumulative amount of open interest removed/"burned" from the system, easily expressed as
$$
\mathrm{OI}_{b_r}(t+\tau) = \mathrm{OI}_{tot}(t) - \mathrm{OI}_{tot}(t+\tau)
$$
## Invariants
There are some invariants that can be derived given the time evolution of funding and pro-rata burn rate assumed, regardless of whatever choice of funding payment is assumed.
Since
$$
\begin{align}
\frac{d\mathrm{OI}_{tot}}{dt} & = - \frac{\mathrm{OI}_{imb}}{\mathrm{OI}_o} \cdot \frac{\mathrm{d\mathrm{FP}}}{dt} \\
\frac{d\mathrm{OI}_{imb}}{dt} & = - \frac{\mathrm{OI}_{tot}}{\mathrm{OI}_o} \cdot \frac{d\mathrm{FP}}{dt}
\end{align}
$$
the evolution in total open interest and open interest imbalance must obey
$$
\frac{d}{dt}\mathrm{OI}_{tot}^2 = \frac{d}{dt}\mathrm{OI}_{imb}^2 .
$$
This implies that the difference in squares betweeen the total open interest and the imbalance in open interest must always be a constant:
$$
C_{o \cdot u} = \frac{1}{4} [\mathrm{OI}^2_{tot} - \mathrm{OI}^2_{imb}]
$$
Given the manner in which $\mathrm{OI}_{tot}$ and $\mathrm{OI}_{imb}$ are defined, the overweight and underweight sides at any time $t$ are determined by
$$
\begin{align}
\mathrm{OI}_o(t) & = \frac{1}{2} \cdot \bigg[ \mathrm{OI}_{tot}(t) + \mathrm{OI}_{imb}(t) \bigg] \\
\mathrm{OI}_u(t) & = \frac{1}{2} \cdot \bigg[ \mathrm{OI}_{tot}(t) - \mathrm{OI}_{imb}(t) \bigg]
\end{align}
$$
which shows that this difference in squares constant is simply the product in open interest between the overweight and underweight sides
$$
\mathrm{OI}_o \cdot \mathrm{OI}_u = C_{o \cdot u}
$$
Our invariant looks analogous to the Uniswap $x\cdot y = k$ invariant, but for open interest over time.
A choice for the time evolution of $\mathrm{OI}_{imb}$ (or $d\mathrm{FP}(t)$ more generally) then completely determines the full evolution of the system through funding. $\mathrm{OI}_{tot}$ will be given by
$$
\mathrm{OI}_{tot}(t+\tau) = \sqrt{ \mathrm{OI}^2_{tot}(t) + \mathrm{OI}^2_{imb}(t+\tau) - \mathrm{OI}^2_{imb}(t) }
$$
for some time in the future $t+\tau$ given an initial start state at $t$. The funding rate experienced by traders (paid and received) will be
$$
f(t) = - \frac{1}{\mathrm{OI}_o} \frac{d\mathrm{OI}_o}{dt} = \frac{1}{\mathrm{OI}_o} \cdot \frac{d\mathrm{FP}}{dt}
$$
One can check the rates paid by the overweight side and received by the underweight side are the same.
## Curves
It's pretty easy now to see that our degree of freedom with this system is in our choice for the funding rate $f$ and all other quantities will follow from there.
### Option 1
Currently, market contracts are implemented with the instantaneous funding rate being given by
$$
f_{1}(t) = 2k \cdot \frac{\mathrm{OI}_{imb}(t)}{\mathrm{OI}_{tot}(t)}
$$
where $k$ is a governance risk parameter. This leads to the imbalance being drawn down exponentially:
$$
\mathrm{OI}_{imb}(t+\tau) = \mathrm{OI}_{imb}(t) \cdot e^{-2k\tau}
$$
- Pros with this approach for the protocol are that the imbalance gets drawn down quickly.
- Cons with this approach would be that the funding rate reaches its most extreme form of $2k$ when $\mathrm{OI}_{imb} = \mathrm{OI}_{tot}$ regardless of the total size of the aggregate open interest on the market. This means smaller traders that may not cause a significant open interest imbalance relative to the OI cap $C_{\mathrm{OI}}$ are still paying the worst-case rate.
To mimic more realistic funding rates -- analogous to those that incentivize tracking of price on CEXs -- we should likely modify the funding rate to increase/decrease in magnitude based on the imbalance amount relative to the open interest cap set.
### Option 2
One alternative to consider could be
$$
f_2 (t) = \frac{2k}{C_{\mathrm{OI}}} \cdot \frac{\mathrm{OI}^2_{imb}(t)}{\mathrm{OI}_{tot}(t)}
$$
where $f_2 = f_1 \cdot \frac{\mathrm{OI}_{imb}}{C_{\mathrm{OI}}}$, scaling our prior funding rate expression with respect to the fraction of the OI cap the imbalance represents. The time evolution of the imbalance drawdown also gets scaled by the same fraction, resulting in
$$
\frac{d\mathrm{OI}_{imb}}{dt} = - \frac{2k}{C_{\mathrm{OI}}} \cdot \mathrm{OI}^2_{imb}(t)
$$
which can be easily solved through integration for a time evolution of the imbalance, giving
$$
\mathrm{OI}_{imb}(t+\tau) = \frac{\mathrm{OI}_{imb}(t)}{1+2k \cdot q(t) \cdot \tau}
$$
where
$$
q(t) \equiv \frac{\mathrm{OI}_{imb}(t)}{C_{\mathrm{OI}}}.
$$
Notice, for small $\tau$ this is similar to an exponential drawdown
$$
\lim_{\tau \to 0} \frac{\mathrm{OI}_{imb}(t+\tau)}{\mathrm{OI}_{imb}(t)} \sim e^{-2k'\tau}
$$
with an effective funding parameter of
$$
k' = k \cdot q(t).
$$
### Option 3
There's a bit more complexity with this alternative, but still interesting particularly given decay is more rapid than option 2.
Take the instantaneous funding rate to remain linear in imbalance, like option 1, but scale relative to the OI cap:
$$
f_3(t) = 2k \cdot \frac{\mathrm{OI}_{imb}(t)}{C_{\mathrm{OI}}}
$$
Effectively, option 1 but scaled by $f_3 = f_1 \cdot \frac{\mathrm{OI}_{tot}}{C_{\mathrm{OI}}}$ this time. It leads to a set of coupled differential equations
$$
\begin{align}
\frac{d\mathrm{OI}_{imb}}{dt} & = - \frac{2k}{C_{\mathrm{OI}}} \cdot \mathrm{OI}_{tot}(t) \cdot \mathrm{OI}_{imb}(t) \\
\frac{d\mathrm{OI}_{tot}}{dt} & = - \frac{2k}{C_{\mathrm{OI}}} \cdot \mathrm{OI}^2_{imb}(t)
\end{align}
$$
for the time evolution of the imbalance and total open interest. To solve, we can use the invariant $C_{o \cdot u}$ from the prior section. The imbalance evolution becomes
$$
\frac{d\mathrm{OI}_{imb}}{dt} = - \frac{2k}{C_{\mathrm{OI}}} \cdot \mathrm{OI}_{imb} \cdot \sqrt{4C_{o \cdot u} + \mathrm{OI}_{imb}^2}
$$
which has solution
$$
\mathrm{OI}_{imb}(t+\tau) = 2 \cdot \sqrt{C_{o \cdot u}} \cdot \mathrm{csch} \bigg[ \frac{4k}{C_{\mathrm{OI}}} \cdot \sqrt{C_{o \cdot u}} \cdot \tau + \mathrm{arcsch} \bigg( \frac{\mathrm{OI}_{imb}(t)}{2 \cdot \sqrt{C_{o \cdot u}}} \bigg) \bigg]
$$
where $\mathrm{csch}$ is the hyperbolic cosecant and $\mathrm{arcsch}$ its inverse. In practice, computing hyperbolic and inverse hyperbolic functions on-chain may be overly complex, although we do have easy access to $\log$ and $\exp$ functions in our [`FixedPoint` library](https://github.com/overlay-market/v1-core/blob/main/contracts/libraries/FixedPoint.sol#L162). The small open interest imbalance limit (i.e. $\mathrm{OI}_{imb}(t) \to 0$) seems particularly worrisome when computing the $\mathrm{arcsch}$ of the $t$ boundary condition on-chain, as it blows up with these expressions. Same for $C_{o \cdot u} \to 0$, when the imbalance is the same as the total open interest (all OI is to one side of the market).
Notice, for large $\tau$ this is similar to an exponential drawdown
$$
\lim_{\tau \to 0} \frac{\mathrm{OI}_{imb}(t+\tau)}{\mathrm{OI}_{imb}(t)} \sim e^{-2k''\tau}
$$
with an effective funding parameter of
$$
k'' = k \cdot \sqrt{a(t)^2 - q(t)^2},
$$
where
$$
a(t) \equiv \frac{\mathrm{OI}_{tot}(t)}{C_{\mathrm{OI}}}.
$$
From a risk perspective, this is what you want given feed prices exhibit exponential behavior (i.e. $\frac{P(t+\tau)}{P(t)} \sim e^{X_\tau}$). The price risk, in the long-run, is much easier to mitigate through funding.
### Plots
Plots of the imbalance drawdowns that result from funding rate choices are in the Desmos link below
<iframe src="https://www.desmos.com/calculator/iju3y4b6y9?embed" width="500" height="500" style="border: 1px solid #ccc" frameborder=0></iframe>
where $f_1(t)$ produces the red plot $f_2(t)$ produces the green plot, and $f_3(t)$ produces the purple plot. Changing $q$, shows the green plot decays less initially while the red plot continues to decay at the same rate.
In general, as the imbalance draws down, the rate of decay decreases for the green plot vs the red. Meaning the protocol continues to be exposed to imbalance risk for a longer period of time, albeit at small magnitudes relative to the OI cap. Larger $k$ values set by governance could mitigate this risk a bit, as the "effective" funding constant becomes more severe, to the detriment of larger traders.
$f_3$ seems to be somewhere between our current funding $f_1$ and option 2 with $f_2$, based on how much larger $a(t) = \frac{\mathrm{OI}_{tot}(t)}{C_{\mathrm{OI}}}$ is in comparison to $q(t) = \frac{\mathrm{OI}_{imb}(t)}{C_{\mathrm{OI}}}$. For imbalance levels representing the majority of outstanding open interest (i.e. $a \approx q$), purple approaches the shape of green but with faster decay over longer periods of time. If imbalance is much smaller than the outstanding open interest, we see purple approaching red (more extreme decay over shorter time periods).
## Conclusion
For usability (traders actually wanting to trade), we should likely change to either $f_2(t)$ or $f_3(t)$ funding curves.
On-chain expressions for $f_3(t)$ will be difficult to work with, but it does appear at first glance to be the better of the two options in terms of mitigating the protocol's profit liability exposure over longer time horizons.
From the perspective of safety with smart contracts, however, $f_2(t)$ does seem to be the better of the two options. The time evolution of the imbalance over time is very easy to calculate with no real risk of underflow/overflow.
Further, $f_3$ reduces to $f_2$ as $\mathrm{OI}_{imb} \to \mathrm{\mathrm{OI}_{tot}}$, which is the most extreme case we care about for profit liability exposure. Given this limit, it seems smarter to use $f_2$ to avoid smart contract issues as $f_3$ provides minimal upside for market risk drawdown when imbalance is all to one side.
To mitigate the significantly larger risk of long-term profit liability exposure when choosing $f_2$, you likely need to have a more extreme $k$ value. More extreme $k$ values then make it less likely that the market has extreme imbalances relative to the open interest cap.