From d137c9c5e4b0809bc31e5edf2243998ac1eb0bbe Mon Sep 17 00:00:00 2001 From: eboado Date: Thu, 13 Oct 2022 13:24:49 +0200 Subject: [PATCH] Fix on ReserveLogic for #315. - Changed condition for when indexes should be updated on _updateIndexes(). - Added condition to not do any update on updateState() if time has not passed since the previous update. - Moved update location of reserve.lastUpdateTimestamp. --- .../protocol/libraries/logic/ReserveLogic.sol | 53 +++++++++++-------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/contracts/protocol/libraries/logic/ReserveLogic.sol b/contracts/protocol/libraries/logic/ReserveLogic.sol index 2b5b2cf4b..17e58c551 100644 --- a/contracts/protocol/libraries/logic/ReserveLogic.sol +++ b/contracts/protocol/libraries/logic/ReserveLogic.sol @@ -108,29 +108,38 @@ library ReserveLogic { * @param reserve the reserve object **/ function updateState(DataTypes.ReserveData storage reserve) internal { - uint256 scaledVariableDebt = - IVariableDebtToken(reserve.variableDebtTokenAddress).scaledTotalSupply(); - uint256 previousVariableBorrowIndex = reserve.variableBorrowIndex; - uint256 previousLiquidityIndex = reserve.liquidityIndex; - uint40 lastUpdatedTimestamp = reserve.lastUpdateTimestamp; - - (uint256 newLiquidityIndex, uint256 newVariableBorrowIndex) = - _updateIndexes( + //solium-disable-next-line + if (reserve.lastUpdateTimestamp != uint40(block.timestamp)) { + uint256 scaledVariableDebt = + IVariableDebtToken(reserve.variableDebtTokenAddress).scaledTotalSupply(); + uint256 previousVariableBorrowIndex = reserve.variableBorrowIndex; + uint256 previousLiquidityIndex = reserve.liquidityIndex; + uint40 lastUpdatedTimestamp = reserve.lastUpdateTimestamp; + uint256 avgStableRate = + IStableDebtToken(reserve.stableDebtTokenAddress).getAverageStableRate(); + + (uint256 newLiquidityIndex, uint256 newVariableBorrowIndex) = + _updateIndexes( + reserve, + scaledVariableDebt, + previousLiquidityIndex, + previousVariableBorrowIndex, + lastUpdatedTimestamp, + avgStableRate + ); + + _mintToTreasury( reserve, scaledVariableDebt, - previousLiquidityIndex, previousVariableBorrowIndex, + newLiquidityIndex, + newVariableBorrowIndex, lastUpdatedTimestamp ); - _mintToTreasury( - reserve, - scaledVariableDebt, - previousVariableBorrowIndex, - newLiquidityIndex, - newVariableBorrowIndex, - lastUpdatedTimestamp - ); + //solium-disable-next-line + reserve.lastUpdateTimestamp = uint40(block.timestamp); + } } /** @@ -336,15 +345,17 @@ library ReserveLogic { uint256 scaledVariableDebt, uint256 liquidityIndex, uint256 variableBorrowIndex, - uint40 timestamp + uint40 timestamp, + uint256 avgStableRate ) internal returns (uint256, uint256) { uint256 currentLiquidityRate = reserve.currentLiquidityRate; + uint256 currentVariableBorrowRate = reserve.currentVariableBorrowRate; uint256 newLiquidityIndex = liquidityIndex; uint256 newVariableBorrowIndex = variableBorrowIndex; - //only cumulating if there is any income being produced - if (currentLiquidityRate > 0) { + // Indexes should be updated only if there is any debt component getting accrued + if ((scaledVariableDebt != 0 && currentVariableBorrowRate != 0) || avgStableRate != 0) { uint256 cumulatedLiquidityInterest = MathUtils.calculateLinearInterest(currentLiquidityRate, timestamp); newLiquidityIndex = cumulatedLiquidityInterest.rayMul(liquidityIndex); @@ -366,8 +377,6 @@ library ReserveLogic { } } - //solium-disable-next-line - reserve.lastUpdateTimestamp = uint40(block.timestamp); return (newLiquidityIndex, newVariableBorrowIndex); } }