From c68cb45d013715e0b81ec30428e3c0ee7205f4aa Mon Sep 17 00:00:00 2001 From: MathisGD Date: Tue, 31 Oct 2023 11:23:10 +0100 Subject: [PATCH] fix: bound base rate --- src/SpeedJumpIrm.sol | 8 +++++--- test/SpeedJumpIrmTest.sol | 5 ++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/SpeedJumpIrm.sol b/src/SpeedJumpIrm.sol index 6597f5b5..15a20628 100644 --- a/src/SpeedJumpIrm.sol +++ b/src/SpeedJumpIrm.sol @@ -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). @@ -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) { diff --git a/test/SpeedJumpIrmTest.sol b/test/SpeedJumpIrmTest.sol index d2aa65d3..4fa3fc44 100644 --- a/test/SpeedJumpIrmTest.sol +++ b/test/SpeedJumpIrmTest.sol @@ -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; @@ -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(