.cache
Last updated
Last updated
The cache function creates a cached copy of struct ReservesData
in memory, as reserveCache
to avoid repeated calls to storage which are expensive.
Essentially, reserveCache
will serve as the working copy within which data will be modified and updated through the course of execution.
Caches ReserveData
into ReserveCache
struct
ReserveData
exists in storage, ReserveCache
exist in memory
Read/Write calls to memory are cheaper than storage
Each reserve (asset) in Aave has its own unique configuration, rates, indexes, and token contracts (aToken and other debt tokens).
For each reserve, this data is captured within a struct ReserveData
. These structs are accessed via the mapping _reserves
, which maps the asset address to its corresponding ReserveData
.
_reserves
is defined on PoolStorage.sol which is inherited by Pool.sol
.cache serves to map the data held within reserve
onto reserveCache
, by unpacking and through some calculations.
ReserveData variables NOT cached:
id
aTokenAddress
interestRateStrategyAddress
accruedToTreasury
unbacked
isolationModeTotalDebt
simply copies reserveConfigurationMap
from storage to memory
The value of reserveFactor
is obtained by passing reserveConfiguration
, obtained earlier, into function getReserveFactor
.
The reserveFactor
, amongst other data, is stored within ReserveConfigurationMap
, which is a bitmap. See getReserveFactor for in-depth explanation.
The reserve factor is a percentage of protocol interest which goes to the Aave Ecosystem Reserve. The rest of the borrow interest goes to the suppliers.
currLiquidityIndex
and nextLiquidityIndex
currVariableBorrowIndex
and nextVariableBorrowIndex
reserve.liquidityIndex is the value of the supply index, as per the last time updateState was ran previously. Similarly, reserve.variableBorrowIndex.
nextLiquidityIndex and nextVariableBorrowIndex
will be updated in updateState, to reflect interest accruel till current block.timestamp.
Having both next and current indexes allows us to compare interest deltas and the like.
currLiquidityRate
& currVariableBorrowRate
currentLiquidityRate
: current supply rate
currentVariableBorrowRate
: current variable borrow rate
Both reserve.currentLiquidityRate and reserve.currentVariableBorrowRate are updated each time .updateInterestRates is executed.
Values are obtained as follows:
ReserveData | ReserveCache |
---|---|
aTokenAddress | aTokenAddress |
stableDebtTokenAddress | stableDebtTokenAddress |
variableDebtTokenAddress | variableDebtTokenAddress |
lastUpdateTimeStamp | reserveLastUpdateTimeStamp |
reserve.lastUpdateTimestamp is updated each time .updateState is executed.
currScaledVariableDebt
& nextScaledVariableDebt
returns scaled variable debt
actual variable debt can be obtained by: scaledBalance * variableBorrowIndex
currPrincipalStableDebt
, currAvgStableBorrowRate
, currTotalStableDebt
currPrincipalStableDebt = super.totalSupply() = _totalSupply
Reflects total stable debt, accounting for interest accrued from inception till _totalSupplyTimestamp.
Does not account for interest accrued from _totalSupplyTimestamp to now.
currTotalStableDebt = _calcTotalSupply(avgRate)
Calculates total stable debt, accounting for interest accrued to date.
currAvgStableBorrowRate = _avgStableRate
weighted global average rate, calculated across all stable borrows
stableDebtLastUpdateTimestamp = _totalSupplyTimestamp
Timestamp of the last update of _totalSupply
See getSupplyData for detailed explanations
AvgStableBorrowRate, nextTotalStableDebt
See getSupplyData for detailed explanation