Added comments

This commit is contained in:
The3D 2020-09-21 18:51:51 +02:00
parent c278832e5a
commit c346251df0
3 changed files with 37 additions and 23 deletions

View File

@ -148,28 +148,23 @@ library ReserveLogic {
* @param reserve the reserve object * @param reserve the reserve object
**/ **/
function updateState(ReserveData storage reserve) external { function updateState(ReserveData storage reserve) external {
address stableDebtToken = reserve.stableDebtTokenAddress;
address variableDebtToken = reserve.variableDebtTokenAddress; address variableDebtToken = reserve.variableDebtTokenAddress;
uint256 previousVariableBorrowIndex = reserve.variableBorrowIndex; uint256 previousVariableBorrowIndex = reserve.variableBorrowIndex;
uint256 previousLiquidityIndex = reserve.liquidityIndex; uint256 previousLiquidityIndex = reserve.liquidityIndex;
uint40 timestamp = reserve.lastUpdateTimestamp;
(uint256 newLiquidityIndex, uint256 newVariableBorrowIndex) = _updateIndexes( (uint256 newLiquidityIndex, uint256 newVariableBorrowIndex) = _updateIndexes(
reserve, reserve,
variableDebtToken, variableDebtToken,
previousLiquidityIndex, previousLiquidityIndex,
previousVariableBorrowIndex, previousVariableBorrowIndex
timestamp
); );
_mintToTreasury( _mintToTreasury(
reserve, reserve,
stableDebtToken,
variableDebtToken, variableDebtToken,
previousVariableBorrowIndex, previousVariableBorrowIndex,
newLiquidityIndex, newLiquidityIndex,
newVariableBorrowIndex, newVariableBorrowIndex
timestamp
); );
} }
@ -299,16 +294,24 @@ library ReserveLogic {
uint256 totalDebtAccrued; uint256 totalDebtAccrued;
uint256 amountToMint; uint256 amountToMint;
uint256 reserveFactor; uint256 reserveFactor;
uint40 stableSupplyUpdatedTimestamp;
} }
/**
* @dev mints part of the repaid interest to the reserve treasury, depending on the reserveFactor for the
* specific asset.
* @param reserve the reserve reserve to be updated
* @param variableDebtToken the debt token address
* @param previousVariableBorrowIndex the variable borrow index before the last accumulation of the interest
* @param newLiquidityIndex the new liquidity index
* @param newVariableBorrowIndex the variable borrow index after the last accumulation of the interest
**/
function _mintToTreasury( function _mintToTreasury(
ReserveData storage reserve, ReserveData storage reserve,
address stableDebtToken,
address variableDebtToken, address variableDebtToken,
uint256 previousVariableBorrowIndex, uint256 previousVariableBorrowIndex,
uint256 newLiquidityIndex, uint256 newLiquidityIndex,
uint256 newVariableBorrowIndex, uint256 newVariableBorrowIndex
uint40 previousTimestamp
) internal { ) internal {
MintToTreasuryLocalVars memory vars; MintToTreasuryLocalVars memory vars;
@ -322,10 +325,12 @@ library ReserveLogic {
vars.scaledVariableDebt = IVariableDebtToken(variableDebtToken).scaledTotalSupply(); vars.scaledVariableDebt = IVariableDebtToken(variableDebtToken).scaledTotalSupply();
//fetching the principal, total stable debt and the avg stable rate //fetching the principal, total stable debt and the avg stable rate
(vars.principalStableDebt, vars.currentStableDebt, vars.avgStableRate) = IStableDebtToken( (
stableDebtToken vars.principalStableDebt,
) vars.currentStableDebt,
.getSupplyData(); vars.avgStableRate,
vars.stableSupplyUpdatedTimestamp
) = IStableDebtToken(reserve.stableDebtTokenAddress).getSupplyData();
//calculate the last principal variable debt //calculate the last principal variable debt
vars.previousVariableDebt = vars.scaledVariableDebt.rayMul(previousVariableBorrowIndex); vars.previousVariableDebt = vars.scaledVariableDebt.rayMul(previousVariableBorrowIndex);
@ -336,7 +341,7 @@ library ReserveLogic {
//calculate the stable debt until the last timestamp update //calculate the stable debt until the last timestamp update
vars.cumulatedStableInterest = MathUtils.calculateCompoundedInterest( vars.cumulatedStableInterest = MathUtils.calculateCompoundedInterest(
vars.avgStableRate, vars.avgStableRate,
previousTimestamp vars.stableSupplyUpdatedTimestamp
); );
vars.previousStableDebt = vars.principalStableDebt.rayMul(vars.cumulatedStableInterest); vars.previousStableDebt = vars.principalStableDebt.rayMul(vars.cumulatedStableInterest);
@ -353,13 +358,22 @@ library ReserveLogic {
IAToken(reserve.aTokenAddress).mintToTreasury(vars.amountToMint, newLiquidityIndex); IAToken(reserve.aTokenAddress).mintToTreasury(vars.amountToMint, newLiquidityIndex);
} }
/**
* @dev updates the reserve indexes and the timestamp of the update
* @param reserve the reserve reserve to be updated
* @param variableDebtToken the debt token address
* @param liquidityIndex the last stored liquidity index
* @param variableBorrowIndex the last stored variable borrow index
**/
function _updateIndexes( function _updateIndexes(
ReserveData storage reserve, ReserveData storage reserve,
address variableDebtToken, address variableDebtToken,
uint256 liquidityIndex, uint256 liquidityIndex,
uint256 variableBorrowIndex, uint256 variableBorrowIndex
uint40 lastUpdateTimestamp
) internal returns (uint256, uint256) { ) internal returns (uint256, uint256) {
uint40 timestamp = reserve.lastUpdateTimestamp;
uint256 currentLiquidityRate = reserve.currentLiquidityRate; uint256 currentLiquidityRate = reserve.currentLiquidityRate;
uint256 newLiquidityIndex = liquidityIndex; uint256 newLiquidityIndex = liquidityIndex;
@ -369,7 +383,7 @@ library ReserveLogic {
if (currentLiquidityRate > 0) { if (currentLiquidityRate > 0) {
uint256 cumulatedLiquidityInterest = MathUtils.calculateLinearInterest( uint256 cumulatedLiquidityInterest = MathUtils.calculateLinearInterest(
currentLiquidityRate, currentLiquidityRate,
lastUpdateTimestamp timestamp
); );
newLiquidityIndex = cumulatedLiquidityInterest.rayMul(liquidityIndex); newLiquidityIndex = cumulatedLiquidityInterest.rayMul(liquidityIndex);
require(newLiquidityIndex < (1 << 128), Errors.LIQUIDITY_INDEX_OVERFLOW); require(newLiquidityIndex < (1 << 128), Errors.LIQUIDITY_INDEX_OVERFLOW);
@ -381,7 +395,7 @@ library ReserveLogic {
if (IERC20(variableDebtToken).totalSupply() > 0) { if (IERC20(variableDebtToken).totalSupply() > 0) {
uint256 cumulatedVariableBorrowInterest = MathUtils.calculateCompoundedInterest( uint256 cumulatedVariableBorrowInterest = MathUtils.calculateCompoundedInterest(
reserve.currentVariableBorrowRate, reserve.currentVariableBorrowRate,
lastUpdateTimestamp timestamp
); );
newVariableBorrowIndex = cumulatedVariableBorrowInterest.rayMul(variableBorrowIndex); newVariableBorrowIndex = cumulatedVariableBorrowInterest.rayMul(variableBorrowIndex);
require(newVariableBorrowIndex < (1 << 128), Errors.VARIABLE_BORROW_INDEX_OVERFLOW); require(newVariableBorrowIndex < (1 << 128), Errors.VARIABLE_BORROW_INDEX_OVERFLOW);

View File

@ -237,11 +237,11 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
} }
/** /**
* @dev returns the principal, total supply and the average borrow rate * @dev returns the principal and total supply, the average borrow rate and the last supply update timestamp
**/ **/
function getSupplyData() public override view returns (uint256, uint256, uint256) { function getSupplyData() public override view returns (uint256, uint256, uint256,uint40) {
uint256 avgRate = _avgStableRate; uint256 avgRate = _avgStableRate;
return (super.totalSupply(), _calcTotalSupply(avgRate), avgRate); return (super.totalSupply(), _calcTotalSupply(avgRate), avgRate, _totalSupplyTimestamp);
} }
/** /**

View File

@ -88,7 +88,7 @@ interface IStableDebtToken {
/** /**
* @dev returns the principal, the total supply and the average stable rate * @dev returns the principal, the total supply and the average stable rate
**/ **/
function getSupplyData() external view returns (uint256, uint256, uint256); function getSupplyData() external view returns (uint256, uint256, uint256, uint40);
/** /**
* @dev returns the timestamp of the last update of the total supply * @dev returns the timestamp of the last update of the total supply