Comment on page

🔬

# Implementation Details

Implementation details for developers

The Borrowing module has been coded with some implementation choices which should be known to developers to understand some on-chain data.

In a given vault, two elements are stored:

`collateralAmount`

: the amount of collateral put in the vault`normalizedDebt`

: a normalized value of the debt

In fact, when someone borrows stablecoins with the Angle Protocol, it's not this amount that is stored but this amount normalized by a quantity called the

`interestAccumulator`

which tracks the evolution of the interest rate in time.For instance, if at the start the value of the

`interestAccumulator`

is 1, and there's a 2% interest rate accruing to all vaults, then after a year, `interestAccumulator`

is 1.02.As long as the interest rate is positive, this

`interestAccumulator`

increases indefinitely.When someone borrows

`x`

of stablecoins and the value of the `interestAccumulator`

is $IR_{t_1}$

:

$\texttt{normalizedDebt} = \frac{x}{IR_{t_1}}$

At any given time, if the value of the

`interestAccumulator`

is $IR_{t_2}$

, the value of the debt of this vault is:

$\texttt{debt} = \texttt{normalizedDebt} \times IR_{t_2} = \frac{x\times IR_{t_2}}{IR_{t_1}}$

If

$IR_{t_1} = 1$

and $IR_{t_2} = 1.02$

, then the debt of this vault which borrowed `x`

stablecoins increased to $1.02 \times x$

.There is nothing obvious with power computation in Solidity and it can rapidly lead to overflows. As such compounding interest rates (like in the formula above) is not that easy.

While Maker uses its own library for interest rates, the solution adopted in Angle Borrowing Module, like Aave in their V2 and V3 is to rely on a third order binomial approximation which works assuming a small interest rate.

$(1+r)^{\Delta_t} \approx 1 + \Delta_tr+\frac{1}{2} \Delta_t(\Delta_t - 1)r^2+\frac{1}{6} \Delta_t(\Delta_t - 1)(\Delta_t - 2)r^3$

This approximation shows up to be quite accurate even for big time frames (like 10 years). It however slightly undercharges borrowers with the advantage of a great gas cost reduction. Interest rates could be set by the protocol with this in mind.

Last modified 1yr ago