Skip to content

Commit

Permalink
WIP Fix of 100% RF for aave#718
Browse files Browse the repository at this point in the history
- Test on pool-simple-flashloan failing
  • Loading branch information
eboadom committed Oct 31, 2022
1 parent f3e037b commit ddbf22b
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 46 deletions.
39 changes: 24 additions & 15 deletions contracts/protocol/libraries/logic/ReserveLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ library ReserveLogic {
DataTypes.ReserveData storage reserve,
DataTypes.ReserveCache memory reserveCache
) internal {
// If time didn't pass since last stored timestamp, skip state update
//solium-disable-next-line
if (reserveCache.reserveLastUpdateTimestamp == uint40(block.timestamp)) {
return;
}

_updateIndexes(reserve, reserveCache);
_accrueToTreasury(reserve, reserveCache);
}
Expand Down Expand Up @@ -286,7 +292,9 @@ library ReserveLogic {
reserveCache.nextLiquidityIndex = reserveCache.currLiquidityIndex;
reserveCache.nextVariableBorrowIndex = reserveCache.currVariableBorrowIndex;

//only cumulating if there is any income being produced
// Only cumulating on the supply side if there is any income being produced
// The case of Reserve Factor 100% is not a problem (currentLiquidityRate == 0),
// as liquidity index should not be updated
if (reserveCache.currLiquidityRate != 0) {
uint256 cumulatedLiquidityInterest = MathUtils.calculateLinearInterest(
reserveCache.currLiquidityRate,
Expand All @@ -296,19 +304,18 @@ library ReserveLogic {
reserveCache.currLiquidityIndex
);
reserve.liquidityIndex = reserveCache.nextLiquidityIndex.toUint128();
}

//as the liquidity rate might come only from stable rate loans, we need to ensure
//that there is actual variable debt before accumulating
if (reserveCache.currScaledVariableDebt != 0) {
uint256 cumulatedVariableBorrowInterest = MathUtils.calculateCompoundedInterest(
reserveCache.currVariableBorrowRate,
reserveCache.reserveLastUpdateTimestamp
);
reserveCache.nextVariableBorrowIndex = cumulatedVariableBorrowInterest.rayMul(
reserveCache.currVariableBorrowIndex
);
reserve.variableBorrowIndex = reserveCache.nextVariableBorrowIndex.toUint128();
}
// Variable borrow side only gets updated if there is any accrual of variable debt
if (reserveCache.currScaledVariableDebt != 0) {
uint256 cumulatedVariableBorrowInterest = MathUtils.calculateCompoundedInterest(
reserveCache.currVariableBorrowRate,
reserveCache.reserveLastUpdateTimestamp
);
reserveCache.nextVariableBorrowIndex = cumulatedVariableBorrowInterest.rayMul(
reserveCache.currVariableBorrowIndex
);
reserve.variableBorrowIndex = reserveCache.nextVariableBorrowIndex.toUint128();
}

//solium-disable-next-line
Expand Down Expand Up @@ -342,8 +349,10 @@ library ReserveLogic {
reserveCache.reserveLastUpdateTimestamp = reserve.lastUpdateTimestamp;

reserveCache.currScaledVariableDebt = reserveCache.nextScaledVariableDebt = IVariableDebtToken(
reserveCache.variableDebtTokenAddress
).scaledTotalSupply();
reserveCache
.variableDebtTokenAddress
)
.scaledTotalSupply();

(
reserveCache.currPrincipalStableDebt,
Expand Down
Loading

0 comments on commit ddbf22b

Please sign in to comment.