Deposit & Borrow Interest
Last updated
Last updated
This section will cover interest rate calculations for both deposits and loans. The prerequisite is an understanding of simple(linear) interest and compound interest.
Simple and compound interest: Refresher
calculateLinearInterest
updates liquidity index => determines how deposit interest accumulates.
calculateCompoundInterest
updates variableBorrowIndex => determines how borrow interest accumulates.
Supply interest in Aave accrues via linear interest (simple interest), as described in the function calculateLinearInterest
.
This function is executed within updateState
, which is called at the start of any state-changing function.
The order of operations might be misleading; with a little re-arranging, it should be obvious:
This should not be surprising to the reader; this is how simple interest is calculated.
However, the reader may wonder why return 1e27 + result
- why add the ray at all?
This has to do with the calculation for nextLiquidityIndex
:
Given that nextLiquidityIndex
is calculated as seen above, we can conclude that the liquidity index is a multiplicative series, where the index begins with the value of 1:
Given the details below, what would be the index at t1?
At t0:
liquidity index = 1.0
supply interest rate, per second = 0.04
The liquidity index at t1 would be 1.2 as a result of the accumulated interest over 5 seconds where the supply interest rate (per second) was 0.04.
Let's connect the dots between interest and indexes by how they are calculated:
Given the manner in which deposit interest is calculated, it would seem that depositors on Aave do not enjoy the compounding of their interest.
This is not true. Deposit interest does compound, but only when the market is touched.
When the market is touched?
Indexes are updated when state-changing transactions are made: supply, borrow, repay, withdraw, etc.
Each time the index is updated, interest is calculated for the interval fromlastUpdatedTimestamp
till now.
Every time the index is updated, a new entry is created in the multiplicative series which contributes to compounding. However, the t in (1 + rt), for each period is irregular.
Let me illustrate with a 2-period example
User deposits 100 DAI at t0, when the Index is 1.0
What is his balance at t2, when the Index has increased to 2.4
From the breakdown on how Total is calculated it should be made apparent that due to the indexes being multiplied, interest is earned on both the principal and prior interest -> compound interest.
This is why earlier we mentioned that deposit interest compounds only when the market is touched -> index is updated, creating a new compounding period.
Compound interest: each period interest is generated on both the principal and the interest generated thus far.
Borrow interest compounds every second. This is achiever by an approximation of binomial expansion to the third term.
Where n = t, number of periods and x = r, rate per second
For obvious reasons, the level of approximation has to be chosen such that the trade-off between accuracy and gas-savings is optimized.
From Aave-v2-whitepaper:
The function calculateCompoundedInterest, (MathUtils.sol line 46) implements the firrst three expansions which gives a good approximation of the compounded interest for up to a 5 year loan duration.
This results in a slight underpayment offset by the gas optimisation benefits.
It's important to note that this behaves a little differently for variable and stable borrowing:
For variable borrows, interests are accrued on any action of any borrower;
For stable borrows, interests are accrued only when a specific borrower performs an action, increasing the impact of the approximation. Still, the difference seems reasonable given the savings in the cost of the transaction.
Deposit interest compounds only when the market is touched while borrow interest compounds every second due to an additional calculation performed in the contract.
Notice the similarities with . Also, notice that the multiplicative series indicates compounding. The interplay between simple interest calculation and compounding is explained further down.
This is a simple interest calculation:
However, the new index is obtained by multiplying the prior index with , which means that incoming interest is applied upon both the principal and previously accrued interest.