💹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:

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

Knowing these values, the protocol can calculateVi+(t)V^+_i (t)andui+(t)u^+_i (t)for any time momenttτi+t \geqslant \tau^+_i, and can calculateVi(t)V^-_i (t) andui(t)u^-_i (t)for any time momenttτit \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

Applying Interest on Individual Positions

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

τij\tau_{ij}: the time the position of thej-thj\text{-th}account in thei-thi\text{-th}asset was last updated vij(τij)v_{ij} (\tau_{ij}): the position value of thej-thj\text{-th}account in thei-thi\text{-th}asset, as it was at the time momentτij\tau_{ij} uij(τij)u_{ij} (\tau_{ij}): the valueui+(τij)u^+_i (\tau_{ij})if the position is long, or the value ui(τij)u^-_i (\tau_{ij}) if the position is short

Knowing these values, the protocol can calculatevij(t)v_{ij} (t)anduij(t)u_{ij}{} (t)for any time momenttmax(τij,τi+,τi)t \geqslant \max (\tau_{ij}, \tau^+_i, \tau^-_i):

uij(t)={ui+(t), if vij(τij)0ui(t), otherwisevij(t)=vij(τij)uij(t)uij(τij)\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.

Last updated