From c346251df03b192897215c6185870e0aa982a134 Mon Sep 17 00:00:00 2001 From: The3D Date: Mon, 21 Sep 2020 18:51:51 +0200 Subject: [PATCH] Added comments --- contracts/libraries/logic/ReserveLogic.sol | 52 ++++++++++++------- contracts/tokenization/StableDebtToken.sol | 6 +-- .../interfaces/IStableDebtToken.sol | 2 +- 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/contracts/libraries/logic/ReserveLogic.sol b/contracts/libraries/logic/ReserveLogic.sol index e5eac57a..8f6069ab 100644 --- a/contracts/libraries/logic/ReserveLogic.sol +++ b/contracts/libraries/logic/ReserveLogic.sol @@ -148,28 +148,23 @@ library ReserveLogic { * @param reserve the reserve object **/ function updateState(ReserveData storage reserve) external { - address stableDebtToken = reserve.stableDebtTokenAddress; address variableDebtToken = reserve.variableDebtTokenAddress; uint256 previousVariableBorrowIndex = reserve.variableBorrowIndex; uint256 previousLiquidityIndex = reserve.liquidityIndex; - uint40 timestamp = reserve.lastUpdateTimestamp; (uint256 newLiquidityIndex, uint256 newVariableBorrowIndex) = _updateIndexes( reserve, variableDebtToken, previousLiquidityIndex, - previousVariableBorrowIndex, - timestamp + previousVariableBorrowIndex ); _mintToTreasury( reserve, - stableDebtToken, variableDebtToken, previousVariableBorrowIndex, newLiquidityIndex, - newVariableBorrowIndex, - timestamp + newVariableBorrowIndex ); } @@ -299,16 +294,24 @@ library ReserveLogic { uint256 totalDebtAccrued; uint256 amountToMint; 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( ReserveData storage reserve, - address stableDebtToken, address variableDebtToken, uint256 previousVariableBorrowIndex, uint256 newLiquidityIndex, - uint256 newVariableBorrowIndex, - uint40 previousTimestamp + uint256 newVariableBorrowIndex ) internal { MintToTreasuryLocalVars memory vars; @@ -322,10 +325,12 @@ library ReserveLogic { vars.scaledVariableDebt = IVariableDebtToken(variableDebtToken).scaledTotalSupply(); //fetching the principal, total stable debt and the avg stable rate - (vars.principalStableDebt, vars.currentStableDebt, vars.avgStableRate) = IStableDebtToken( - stableDebtToken - ) - .getSupplyData(); + ( + vars.principalStableDebt, + vars.currentStableDebt, + vars.avgStableRate, + vars.stableSupplyUpdatedTimestamp + ) = IStableDebtToken(reserve.stableDebtTokenAddress).getSupplyData(); //calculate the last principal variable debt vars.previousVariableDebt = vars.scaledVariableDebt.rayMul(previousVariableBorrowIndex); @@ -336,7 +341,7 @@ library ReserveLogic { //calculate the stable debt until the last timestamp update vars.cumulatedStableInterest = MathUtils.calculateCompoundedInterest( vars.avgStableRate, - previousTimestamp + vars.stableSupplyUpdatedTimestamp ); vars.previousStableDebt = vars.principalStableDebt.rayMul(vars.cumulatedStableInterest); @@ -353,13 +358,22 @@ library ReserveLogic { 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( ReserveData storage reserve, address variableDebtToken, uint256 liquidityIndex, - uint256 variableBorrowIndex, - uint40 lastUpdateTimestamp + uint256 variableBorrowIndex ) internal returns (uint256, uint256) { + + uint40 timestamp = reserve.lastUpdateTimestamp; + uint256 currentLiquidityRate = reserve.currentLiquidityRate; uint256 newLiquidityIndex = liquidityIndex; @@ -369,7 +383,7 @@ library ReserveLogic { if (currentLiquidityRate > 0) { uint256 cumulatedLiquidityInterest = MathUtils.calculateLinearInterest( currentLiquidityRate, - lastUpdateTimestamp + timestamp ); newLiquidityIndex = cumulatedLiquidityInterest.rayMul(liquidityIndex); require(newLiquidityIndex < (1 << 128), Errors.LIQUIDITY_INDEX_OVERFLOW); @@ -381,7 +395,7 @@ library ReserveLogic { if (IERC20(variableDebtToken).totalSupply() > 0) { uint256 cumulatedVariableBorrowInterest = MathUtils.calculateCompoundedInterest( reserve.currentVariableBorrowRate, - lastUpdateTimestamp + timestamp ); newVariableBorrowIndex = cumulatedVariableBorrowInterest.rayMul(variableBorrowIndex); require(newVariableBorrowIndex < (1 << 128), Errors.VARIABLE_BORROW_INDEX_OVERFLOW); diff --git a/contracts/tokenization/StableDebtToken.sol b/contracts/tokenization/StableDebtToken.sol index fe352b32..1974d51e 100644 --- a/contracts/tokenization/StableDebtToken.sol +++ b/contracts/tokenization/StableDebtToken.sol @@ -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; - return (super.totalSupply(), _calcTotalSupply(avgRate), avgRate); + return (super.totalSupply(), _calcTotalSupply(avgRate), avgRate, _totalSupplyTimestamp); } /** diff --git a/contracts/tokenization/interfaces/IStableDebtToken.sol b/contracts/tokenization/interfaces/IStableDebtToken.sol index 0e75bae4..901a1178 100644 --- a/contracts/tokenization/interfaces/IStableDebtToken.sol +++ b/contracts/tokenization/interfaces/IStableDebtToken.sol @@ -88,7 +88,7 @@ interface IStableDebtToken { /** * @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