Skip to content

Commit

Permalink
Merge pull request #724 from bgd-labs/fix/100-rf
Browse files Browse the repository at this point in the history
Fix indexes update with 100% reserve factor
  • Loading branch information
miguelmtzinf authored Nov 14, 2022
2 parents 02fbbd4 + 5d0a1aa commit cac49b4
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 46 deletions.
36 changes: 21 additions & 15 deletions contracts/protocol/libraries/logic/ReserveLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,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 +298,21 @@ 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 index only gets updated if there is any variable debt.
// reserveCache.currVariableBorrowRate != 0 is not a correct validation,
// because a positive base variable rate can be stored on
// reserveCache.currVariableBorrowRate, but the index should not increase
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 +346,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 cac49b4

Please sign in to comment.