Deposit & Borrow Interest

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.

Deposit Interest

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.

  • Simple Interest=P(r∗T)Simple\, Interest = P(r * T)

  • Total=P(1+r∗T)Total = P(1 + r * T)

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:

Index=1∗(1+r1t1)∗(1+r2t2)∗(1+r3t3)...Index = 1 * (1 + r_{1}t_{1})* (1+ r_{2}t_{2}) * (1+ r_{3}t_{3})...

Notice the similarities with Total=P(1+r∗T)Total = P(1 + r * T). Also, notice that the multiplicative series indicates compounding. The interplay between simple interest calculation and compounding is explained further down.

Example: calculating the next index

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.

Compounding frequency

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.

  • This is a simple interest calculation: (1+rt)(1 + rt)

  • However, the new index is obtained by multiplying the prior index with (1+rt)(1 + rt), which means that incoming interest is applied upon both the principal and previously accrued interest.

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

Borrow interest compounds every second. This is achiever by an approximation of binomial expansion to the third term.

Binomal Expansion:(1+x)n=1+nx+n2(n−1)x2+n6(n−1)(n−2)x3+...Binomal\, Expansion: (1+x)^n = 1+nx+\frac{n}{2}(n-1)x^2+\frac{n}{6}(n-1)(n-2)x^3+...

Where n = t, number of periods and x = r, rate per second

Interest:(1+r)t≈1+rt+t2(t−1)r2+t6(t−1)(t−2)r3Interest:(1+r)^t \approx 1+rt+\frac{t}{2}(t-1)r^2+\frac{t}{6}(t-1)(t-2)r^3

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.

Last updated