Skip to content

Commit

Permalink
fix: if borrow rate is zero, only update the ilk last updated
Browse files Browse the repository at this point in the history
  • Loading branch information
junkim012 committed May 11, 2024
1 parent 0f78d89 commit 24b1cd6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/IonPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -490,14 +490,14 @@ contract IonPool is PausableUpgradeable, RewardToken {
(uint256 borrowRate, uint256 reserveFactor) =
$.interestRateModule.calculateInterestRate(ilkIndex, totalDebt, totalEthSupply);

if (borrowRate == 0) return (0, 0, 0, 0, 0);

// Calculates borrowRate ^ (time) and returns the result with RAY precision
uint256 borrowRateExpT = _rpow(borrowRate + RAY, block.timestamp - ilk.lastRateUpdate, RAY);

// Unsafe cast OK
timestampIncrease = uint48(block.timestamp) - ilk.lastRateUpdate;

if (borrowRate == 0) return (0, 0, 0, 0, timestampIncrease);

// Calculates borrowRate ^ (time) and returns the result with RAY precision
uint256 borrowRateExpT = _rpow(borrowRate + RAY, uint256(timestampIncrease), RAY);

// Debt distribution
// This form of rate accrual is much safer than distributing the new
// debt increase to the total debt since low debt amounts won't cause
Expand Down
46 changes: 46 additions & 0 deletions test/unit/concrete/IonPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,52 @@ contract IonPool_InterestTest is IonPoolSharedSetup, IIonPoolEvents {
}
}

// If zero borrow rate, only the last updated timestamp should update
function test_CalculateRewardAndDebtDistributionZeroBorrowRate() external {
// update interest rate module to have zero rates.
IlkData[] memory ilkConfigs = new IlkData[](3);
uint16[] memory distributionFactors = new uint16[](3);
distributionFactors[0] = 0.2e4;
distributionFactors[1] = 0.4e4;
distributionFactors[2] = 0.4e4;

for (uint8 i; i != 3; ++i) {
IlkData memory ilkConfig = IlkData({
adjustedProfitMargin: 0,
minimumKinkRate: 0,
reserveFactor: 0,
adjustedBaseRate: 0,
minimumBaseRate: 0,
optimalUtilizationRate: 9000,
distributionFactor: distributionFactors[i],
adjustedAboveKinkSlope: 0,
minimumAboveKinkSlope: 0
});
ilkConfigs[i] = ilkConfig;
}

interestRateModule = new InterestRate(ilkConfigs, apyOracle);
ionPool.updateInterestRateModule(interestRateModule);

vm.warp(block.timestamp + 1 days);

(
uint256 totalSupplyFactorIncrease,
,
uint104[] memory rateIncreases,
uint256 totalDebtIncrease,
uint48[] memory timestampIncreases
) = ionPool.calculateRewardAndDebtDistribution();

assertEq(totalSupplyFactorIncrease, 0, "total supply factor");
assertEq(totalDebtIncrease, 0, "total debt increase");

for (uint8 i; i != 3; ++i) {
assertEq(rateIncreases[i], 0, "rate");
assertEq(timestampIncreases[i], 1 days, "timestamp increase");
}
}

function test_AccrueInterest() public {
uint256 collateralDepositAmount = 10e18;
uint256 normalizedBorrowAmount = 5e18;
Expand Down

0 comments on commit 24b1cd6

Please sign in to comment.