Skip to content

Commit

Permalink
Merge pull request #75 from morpho-labs/refactor/curve-function
Browse files Browse the repository at this point in the history
Refactor curve function
  • Loading branch information
MathisGD authored Nov 13, 2023
2 parents b209956 + 8b66f45 commit ea7a95e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
6 changes: 2 additions & 4 deletions src/SpeedJumpIrm.sol
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,9 @@ contract AdaptativeCurveIrm is IIrm {
/// r = ((1-1/C)*err + 1) * rateAtTarget if err < 0
/// ((C-1)*err + 1) * rateAtTarget else.
function _curve(uint256 _rateAtTarget, int256 err) private view returns (uint256) {
// Safe "unchecked" cast of _rateAtTarget because _rateAtTarget <= MAX_RATE_AT_TARGET.
int256 steeringCoeff = (err < 0 ? WAD_INT - WAD_INT.wDivDown(CURVE_STEEPNESS) : CURVE_STEEPNESS - WAD_INT)
.wMulDown(int256(_rateAtTarget));
int256 coeff = err < 0 ? WAD_INT - WAD_INT.wDivDown(CURVE_STEEPNESS) : CURVE_STEEPNESS - WAD_INT;
// Safe "unchecked" cast of _rateAtTarget because _rateAtTarget <= MAX_RATE_AT_TARGET.
// Safe "unchecked" cast of the result because r >= 0.
return uint256(steeringCoeff.wMulDown(err) + int256(_rateAtTarget));
return uint256((coeff.wMulDown(err) + WAD_INT).wMulDown(int256(_rateAtTarget)));
}
}
8 changes: 2 additions & 6 deletions test/SpeedJumpIrmTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,9 @@ contract AdaptativeCurveIrmTest is Test {
function _curve(uint256 rateAtTarget, int256 err) internal pure returns (uint256) {
// Safe "unchecked" cast because err >= -1 (in WAD).
if (err < 0) {
return uint256(
(WAD_INT - WAD_INT.wDivDown(CURVE_STEEPNESS)).wMulDown(int256(rateAtTarget)).wMulDown(err)
+ int256(rateAtTarget)
);
return uint256((WAD_INT - WAD_INT.wDivDown(CURVE_STEEPNESS)).wMulDown(err) + WAD_INT).wMulDown(rateAtTarget);
} else {
return
uint256((CURVE_STEEPNESS - WAD_INT).wMulDown(int256(rateAtTarget)).wMulDown(err) + int256(rateAtTarget));
return uint256((CURVE_STEEPNESS - WAD_INT).wMulDown(err) + WAD_INT).wMulDown(rateAtTarget);
}
}

Expand Down

0 comments on commit ea7a95e

Please sign in to comment.