.cache
TLDR
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
ReserveDataintoReserveCachestructReserveDataexists in storage,ReserveCacheexist in memoryRead/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 correspondingReserveData._reservesis defined on PoolStorage.sol which is inherited by Pool.sol
// Map of reserves and their data (underlyingAssetOfReserve => reserveData)
mapping(address => DataTypes.ReserveData) internal _reserves;Caching reserve into reserveCache
.cache serves to map the data held within reserve onto reserveCache, by unpacking and through some calculations.

// reservesData is PoolStorage::_reserves
DataTypes.ReserveData storage reserve = reservesData[params.asset];
DataTypes.ReserveCache memory reserveCache = reserve.cache();Code

1. Get ReserveConfigurationMap reserveConfiguration
reserveCache.reserveConfiguration = reserve.configuration;simply copies
reserveConfigurationMapfrom storage to memory

2. Get reserveFactor
The value of reserveFactor is obtained by passing reserveConfiguration, obtained earlier, into function getReserveFactor.
reserveCache.reserveFactor = reserveCache.reserveConfiguration.getReserveFactor();
The reserveFactor, amongst other data, is stored within ReserveConfigurationMap, which is a bitmap. See getReserveFactor for in-depth explanation.
3 - 4. Get indexes: current and next
currLiquidityIndexandnextLiquidityIndex
reserveCache.currLiquidityIndex = reserveCache.nextLiquidityIndex = reserve.liquidityIndex;currVariableBorrowIndexandnextVariableBorrowIndex
reserveCache.currVariableBorrowIndex = reserveCache.nextVariableBorrowIndex
= reserve.variableBorrowIndex;reserve.liquidityIndex is the value of the supply index, as per the last time updateState was ran previously. Similarly, reserve.variableBorrowIndex.
nextLiquidityIndex and nextVariableBorrowIndexwill be updated in updateState, to reflect interest accruel till current block.timestamp.
5 - 6. Get rates: currLiquidityRate & currVariableBorrowRate
currLiquidityRate & currVariableBorrowRatereserveCache.currLiquidityRate = reserve.currentLiquidityRate;
reserveCache.currVariableBorrowRate = reserve.currentVariableBorrowRate;currentLiquidityRate: current supply ratecurrentVariableBorrowRate: current variable borrow rate
7-10. Token Addresses
reserveCache.aTokenAddress = reserve.aTokenAddress;
reserveCache.stableDebtTokenAddress = reserve.stableDebtTokenAddress;
reserveCache.variableDebtTokenAddress = reserve.variableDebtTokenAddress;
reserveCache.reserveLastUpdateTimestamp = reserve.lastUpdateTimestamp;Values are obtained as follows:
aTokenAddress
aTokenAddress
stableDebtTokenAddress
stableDebtTokenAddress
variableDebtTokenAddress
variableDebtTokenAddress
lastUpdateTimeStamp
reserveLastUpdateTimeStamp
11. Get currScaledVariableDebt & nextScaledVariableDebt
currScaledVariableDebt & nextScaledVariableDebtreserveCache.currScaledVariableDebt = reserveCache.nextScaledVariableDebt = IVariableDebtToken(reserveCache.variableDebtTokenAddress).scaledTotalSupply();returns scaled variable debt
actual variable debt can be obtained by:
scaledBalance * variableBorrowIndex
12. Get currPrincipalStableDebt, currAvgStableBorrowRate, currTotalStableDebt
currPrincipalStableDebt, currAvgStableBorrowRate, currTotalStableDebt (
reserveCache.currPrincipalStableDebt,
reserveCache.currTotalStableDebt,
reserveCache.currAvgStableBorrowRate,
reserveCache.stableDebtLastUpdateTimestamp
) = IStableDebtToken(reserveCache.stableDebtTokenAddress).getSupplyData();
getSupplyData()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
13. Get nextAvgStableBorrowRate, nextTotalStableDebt
AvgStableBorrowRate, nextTotalStableDebtreserveCache.nextTotalStableDebt = reserveCache.currTotalStableDebt;
reserveCache.nextAvgStableBorrowRate = reserveCache.currAvgStableBorrowRate;See getSupplyData for detailed explanation
Last updated
Was this helpful?
