> For the complete documentation index, see [llms.txt](https://turboswap.gitbook.io/en/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://turboswap.gitbook.io/en/design-notes/applying-interest.md).

# Applying Interest

This page describes how borrow and deposit interest rates are applied to the positions

## The Challenge

The TurboSwap protocol continuously applies compound interest on all positive (long) and negative (short) positions.  Implementing this in a naive way is unfeasible on-chain, as the number of positions could be arbitrarily high and iterating through all of them each time would be too expensive.

Thus, the protocol needs to use some tricks to efficiently apply interest to a large number of positions.

## Applying Interest on Aggregated Positions

For a particular asset, the protocol stores the following aggregated values:

$$\tau^+\_i$$: the time when the sum of all the long positions in the$$i\text{-th}$$asset was last updated\
$$V^+\_i (\tau^+\_i)$$: the sum of all the long positions in the$$i\text{-th}$$asset as it was at the time moment$$\tau^+\_i$$\
&#x20;$$u^+\_i (\tau^+\_i)$$: the hypothetical value at the time moment$$\tau^+\_i$$of a one unit long positions opened in the the$$i\text{-th}$$asset at the time the$$i\text{-th}$$asset was just listed with the protocol\
&#x20;$$\tau^-\_i$$: the time when the sum of all the short positions in the$$i\text{-th}$$asset was last updated\
&#x20;$$V^-\_i (\tau^-\_i)$$: the sum of all the short positions in the$$i\text{-th}$$asset as it was at the time moment$$\tau^-\_i$$\
&#x20;$$u^-\_i (\tau^-\_i)$$: the hypothetical value at the time moment$$\tau^+\_i$$of a one unit short positions opened in the the$$i\text{-th}$$asset at the time the$$i\text{-th}$$asset was just listed with the protocol

Knowing these values, the protocol can calculate$$V^+\_i (t)$$and$$u^+\_i (t)$$for any time moment$$t \geqslant \tau^+\_i$$, and can calculate$$V^-\_i (t)$$ and$$u^-\_i (t)$$for any time moment$$t \geqslant \tau^-\_i$$.  See the “Main Concepts” sections for the calculation formulas.

The stored values for an asset ought to be updated only when the borrow interest rate for the asset is about to be changed, or some position in this asset is changed due to a user action.  Continuous accumulation of interest doesn't require the stored values to be updated&#x20;

## Applying Interest on Individual Positions

For a particular position, the protocol stored the following values:

$$\tau\_{ij}$$: the time the position of the$$j\text{-th}$$account in the$$i\text{-th}$$asset was last updated\
&#x20;$$v\_{ij} (\tau\_{ij})$$: the position value of the$$j\text{-th}$$account in the$$i\text{-th}$$asset, as it was at the time moment$$\tau\_{ij}$$\
&#x20;$$u\_{ij} (\tau\_{ij})$$: the value$$u^+*i (\tau*{ij})$$if the position is long, or the value $$u^-*i (\tau*{ij})$$ if the position is short

Knowing these values, the protocol can calculate$$v\_{ij} (t)$$and$$u\_{ij}{} (t)$$for any time moment$$t \geqslant \max (\tau\_{ij}, \tau^+\_i, \tau^-\_i)$$:

$$
\begin{array}{rcl}
u\_{ij} (t) & = &
\begin{cases}
u^+*i (t) & \text{, if } v*{ij} (\tau\_{ij}) \geqslant 0 \\\[1em]
u^-*i (t) & \text{, otherwise}
\end{cases} \\\[2em]
v*{ij} (t) & = & v\_{ij} (\tau\_{ij}) \frac{u\_{ij} (t)}{u\_{ij} (\tau\_{ij})}
\end{array}
$$

The stored values for a position outage to be updated only then the position is changed due to a user action.  Continuous accumulation of interest doesn't require the stored values to be updated.
