Implementation explained
Last updated
Last updated
There is a global stable rate determined by a model; each user accrues interest based on the stable rate they locked-in. How does this work?
Each asset in Aave has its configuration data contained within a struct ReserveData
. This can be accessed via an internal mapping _reserves
.
Held in storage, each ReserveData
, contains a uint128 variable currentStableBorrowRate
.
This is the stable rate enjoyed by incoming stable borrows, regardless of user.
This global value is determined by the Utilization model we described above.
Held in storage, the ReserveData struct contains variable: currentStableBorrowRate
Upon taking up a stable loan, the rate enjoyed by the user is stored in _userState[address].additionalData
, as defined on the stableDebtToken contract.
.additionalData
is his weighted average stable rate, based on all his previous borrows.
This is the rate at which interest compounds for the user.
Each time a new stable borrow is taken, this is incremented:
(oldAmount * oldRate) + (newAmount * newRate) / totalAmount
_userState[address].additionalData
is updated within the
mint
function of stableDebtToken.
An internal storage variable on the stable debt token contract, it is the weighted average rate across all the stable borrows taken to date, system-wide.
Incremented like so:
_avgStableRate
= (currrentAvgStableRate * previousSupply) + (newRate * newAmount) / (_totalSupply() + newAmount)
Difference between _avgStableRate
and
currentStableBorrowRate
_avgStableRate
reflects the average rate at which interest is being accrued by stable borrowers
currentStableBorrowRate
is the rate for the next incoming stable borrow as determined by the Utilization model.
borrow & mint
mint
returns the following two values which are stored in reserveCache
:
reserveCache.nextAvgStableBorrowRate
= vars.currentAvgStableRate
reserveCache.nextTotalStableDebt
= vars.nextSupply
They are used later in calculateInterestRates
to update system-wide rates.
After a loan is taken, calculateInterestRates
is ran to update the system-wide rates:
Liquidity rate
Stable borrow rate
Variable borrow rate
totalStableDebt
used to calculate totalDebt
and averageStableBorrowRate
used to calculate overallBorrowRate
, and consequently, currentLiquidityRate
.
Stable borrow rate is updated, accounting for increase in stable loans taken; as are the other rates.
The updated stable rate is stored in reserve.currentStableBorrowRate - in storage.