Skip to content

Commit

Permalink
Fix on ReserveLogic for aave#315.
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
eboadom committed Oct 13, 2022
1 parent 0829f97 commit d137c9c
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions contracts/protocol/libraries/logic/ReserveLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down Expand Up @@ -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);
Expand All @@ -366,8 +377,6 @@ library ReserveLogic {
}
}

//solium-disable-next-line
reserve.lastUpdateTimestamp = uint40(block.timestamp);
return (newLiquidityIndex, newVariableBorrowIndex);
}
}

0 comments on commit d137c9c

Please sign in to comment.