The goal of this page is to be a self-contained explanation of the big mathematical concepts involved in building a lending protocol.
For a given lending market, a reserve holds all deposits of a coin type for a given lending market. For example, the Suilend Main Market will have exactly 1 SUI reserve and 1 USDC reserve.
If a user deposits/repays SUI, the SUI reserve will increase in supply.
If a user borrows/withdraws SUI, the SUI reserve will decrease in supply.
Where:
-
$U_{r}$ is reserve utilization.$0 < U_{reserve} < 1$ -
$B_r$ is the amount of tokens lent to borrowers from reserve$r$ . -
$A_r$ is the amount of tokens available in reserve$r$ . These are tokens that are have been deposited into the reserve but not borrowed yet. -
$T_r$ is the total supply of tokens in reserve$r$ .
Example: Say I (ripleys) deposit 100 USDC into Suilend, and Soju (our bd guy) deposit 100 SUI and borrows 50 USDC.
The reserve utilization on the USDC reserve is
When a user deposits SUI into Suilend, they will mint (ie get back) CSUI. This CSUI entitles the user to obtain their deposit from Suilend + additional interest. The interest is obtained by lending out the tokens to borrowers.
The CToken ratio denotes the exchange rate between the CToken and its underlying asset. Formally, the ctoken ratio is calculated by:
Where:
-
$C_r$ is the ctoken ratio for reserve$r$ -
$B_r$ is the amount of tokens lent to borrowers for reserve$r$ (including interest) -
$A_r$ is the amount of available tokens (ie not lent out) for reserve$r$ -
$T_{C_r}$ is the total supply of ctokens in reserve$r$ .
Notes:
-
$C_r$ starts at 1 when the reserve is initialized, and grows over time. The CToken ratio never decreases. - a user cannot always exchange their CSUI back to SUI. In a worst case scenario, all deposited SUI could be lent out, so the protocol won't have any left for redemption. However, in this scenario, the interest rates will skyrocket, incentivizing new depositors and also incentivizing borrowers to pay back their debts.
- the ctoken ratio captures the interest earned by a deposit.
An obligation tracks a user's deposits and borrows in a given lending market.
The USD value of a user's borrows can never exceed the USD value of a user's deposits. Otherwise, the protocol can pick up bad debt!
An obligation O is healthy if:
Where:
-
$M$ is the lending market -
$r$ is a reserve in$M$ -
$B(O, r)$ is the USD value of obligation O's borrows from reserve$r$ -
$D(O, r)$ is the USD value of obligation O's deposits from reserve$r$ -
$LTV_{open}(r)$ is the open LTV for reserve$r$ . ($0 <= LTV_{open}(r) < 1$ )
An obligation O is unhealthy and eligible for liquidation if:
Where:
-
$LTV_{close}(r)$ is the close LTV for reserve$r$ . ($0 <= LTV_{close}(r) < 1$ )
An obligation O is underwater if:
In this situation, the protocol has picked up bad debt.
In Suilend, debt is compounded every second.
Compounded debt is tracked per obligation and per reserve. Debt needs to be tracked per reserve because it affects the ctoken ratio. Debt needs to be tracked per obligation because otherwise users won't pay back their debt!
This section is a bit complicated and only relevant if you want to understand the source code of the protocol.
An APR is a representation of the yearly interest paid on your debt, without accounting for compounding.
In Suilend, the APR is a function of reserve utilization. The exact function is subject to change.
Note that reserve utilization only changes on a borrow or repay action.
The formula below describes how to compound debt on a reserve:
$$B(t=1)_r = B(t=0)r * (1 + APR(U_r) / Y{seconds})^1$$
Where:
-
$B(t)_r$ is the total amount of tokens borrowed in reserve$r$ at time$t$ . -
$APR(U_r)$ is the APR for a given utilization value. -
$Y_{seconds}$ is the number of seconds in a year.
Note that even if no additional users borrow tokens after
This is tricky to do efficiently since the APR can change every second and our borrowed amount can change on every borrow/repay action. Let's work through a simple example first.
Lets say the owner of obligation
What is the compounded debt at
-
$B$ is the amount of tokens borrowed from reserve$r$ at$t=0$ . -
$APR_r(t)$ is the variable APR for reserve$r$ at time$t$ .
Lets define a new variable I that accumulates the products.
Now, simplifying our first expression:
Note that the term
Each obligation would "snapshot" the latest value of
At any time T and for any reserve, the following expression is true.
-
$B_r$ is the total amount of tokens borrowed in reserve$r$ at time$T$ . -
$B_r(o)$ is the amount of tokens borrowed from reserve$r$ by obligation$O$ . -
$I_r(T)$ is the latest cumulative borrow rate product for reserve$r$ . -
$T'(o)$ is the last time the obligation's debt was compounded.
In other words, the global borrowed amount equals the sum of all borrowed tokens per obligation after compounding.
The goal of liquidations is to force repay an unhealthy obligation's debt before it goes underwater. Recall that an obligation O is unhealthy and eligible for liquidation if:
Where:
-
$M$ is the lending market -
$r$ is a reserve in$M$ -
$B(O, r)$ is the USD value of obligation O's borrows from reserve$r$ -
$D(O, r)$ is the USD value of obligation O's deposits from reserve$r$ -
$LTV_{close}(r)$ is the close LTV for reserve$r$ . ($0 <= LTV_{close}(r) < 1$ )
Say the total value of a user's borrowed (deposited) amount is
Where:
-
$CF$ is the close factor (see parameters section) -
$LB$ is the liquidation bonus (see parameters section)
Notes:
- when an obligation is unhealthy but not underwater, the LTV decreases after a liquidation. This is good.
- the liquidation bonus (LB) is what makes the liquidation profitable for a liquidator.
Open LTV is a percentage that limits how much can be initially borrowed against a deposit.
Open LTV is less than or equal to 1, and is defined per reserve. This is because some tokens are more risky than others. For example, using USDC as collateral is much safer than using DOGE.
Close LTV is a percentage that represents the maximum amount that can be borrowed against a deposit.
For a given reserve, Close LTV > Open LTV.
The Close Factor determines the percentage of an obligation's borrow that can be repaid on liquidation.
Bounds:
The liquidation bonus determines the bonus a liquidator gets when they liquidate an obligation. This bonus value is what makes a liquidation profitable for the liquidator.
Bounds: