Skip to content

Commit

Permalink
fix: bound base rate
Browse files Browse the repository at this point in the history
  • Loading branch information
MathisGD committed Oct 31, 2023
1 parent e061946 commit c68cb45
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/SpeedJumpIrm.sol
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ contract AdaptativeCurveIRM is IIrm {
// Safe "unchecked" cast because elapsed <= block.timestamp.
int256 linearVariation = speed * int256(elapsed);
uint256 variationMultiplier = MathLib.wExp(linearVariation);
uint256 newBaseRate = (baseRate[id] > 0) ? baseRate[id].wMulDown(variationMultiplier) : INITIAL_BASE_RATE;
// newBaseRate is bounded between MIN_BASE_RATE, MAX_BASE_RATE.
uint256 newBaseRate = (baseRate[id] > 0)
? baseRate[id].wMulDown(variationMultiplier).bound(MIN_BASE_RATE, MAX_BASE_RATE)
: INITIAL_BASE_RATE;
uint256 newBorrowRate = _curve(newBaseRate, err);

// Then we compute the average rate over the period (this is what Morpho needs to accrue the interest).
Expand All @@ -137,8 +140,7 @@ contract AdaptativeCurveIRM is IIrm {
uint256((int256(newBorrowRate) - int256(_curve(baseRate[id], err))).wDivDown(linearVariation));
}

// We bound both newBorrowRate and avgBorrowRate between MIN_RATE and MAX_RATE.
return (avgBorrowRate, newBaseRate.bound(MIN_BASE_RATE, MAX_BASE_RATE));
return (avgBorrowRate, newBaseRate);
}

function _curve(uint256 _baseRate, int256 err) internal view returns (uint256) {
Expand Down
5 changes: 2 additions & 3 deletions test/SpeedJumpIrmTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ contract AdaptativeCurveIRMTest is Test {
int256 speed = int256(ADJUSTMENT_SPEED).wMulDown(err);
uint256 elapsed = (baseRate > 0) ? block.timestamp - market.lastUpdate : 0;
int256 linearVariation = speed * int256(elapsed);
uint256 variationMultiplier = MathLib.wExp(linearVariation);
uint256 newBaseRate = (baseRate > 0) ? baseRate.wMulDown(variationMultiplier) : INITIAL_BASE_RATE;
uint256 newBaseRate = _expectedBaseRate(id, market);
uint256 newBorrowRate = _curve(newBaseRate, err);

uint256 avgBorrowRate;
Expand All @@ -164,7 +163,7 @@ contract AdaptativeCurveIRMTest is Test {
return avgBorrowRate;
}

function _curve(uint256 baseRate, int256 err) internal view returns (uint256) {
function _curve(uint256 baseRate, int256 err) internal pure returns (uint256) {
// Safe "unchecked" cast because err >= -1 (in WAD).
if (err < 0) {
return uint256((WAD_INT - WAD_INT.wDivDown(int256(CURVE_STEEPNESS))).wMulDown(err) + WAD_INT).wMulDown(
Expand Down

0 comments on commit c68cb45

Please sign in to comment.