Skip to content

Commit

Permalink
docs: minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
MathisGD committed Nov 10, 2023
1 parent bd0b5cd commit 222b04e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
28 changes: 14 additions & 14 deletions src/SpeedJumpIrm.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ contract AdaptativeCurveIrm is IIrm {
/// @notice Target utilization (scaled by WAD).
/// @dev Verified to be strictly between 0 and 1 at construction.
int256 public immutable TARGET_UTILIZATION;
/// @notice Initial rate at target (scaled by WAD).
/// @notice Initial rate at target per second (scaled by WAD).
uint256 public immutable INITIAL_RATE_AT_TARGET;

/* STORAGE */
Expand Down Expand Up @@ -102,16 +102,16 @@ contract AdaptativeCurveIrm is IIrm {

Id id = marketParams.id();

(uint256 avgBorrowRate, uint256 newRateAtTarget) = _borrowRate(id, market);
(uint256 avgBorrowRate, uint256 endRateAtTarget) = _borrowRate(id, market);

rateAtTarget[id] = newRateAtTarget;
rateAtTarget[id] = endRateAtTarget;

emit BorrowRateUpdate(id, avgBorrowRate, newRateAtTarget);
emit BorrowRateUpdate(id, avgBorrowRate, endRateAtTarget);

return avgBorrowRate;
}

/// @dev Returns avgBorrowRate and newRateAtTarget.
/// @dev Returns avgBorrowRate and endRateAtTarget.
/// @dev Assumes that the inputs `marketParams` and `id` match.
function _borrowRate(Id id, Market memory market) private view returns (uint256, uint256) {
// Safe "unchecked" cast because the utilization is smaller than 1 (scaled by WAD).
Expand All @@ -134,28 +134,28 @@ contract AdaptativeCurveIrm is IIrm {
// market.lastUpdate != 0 because it is not the first interaction with this market.
uint256 elapsed = block.timestamp - market.lastUpdate;
// Safe "unchecked" cast because elapsed <= block.timestamp.
int256 linearVariation = speed * int256(elapsed);
uint256 variationMultiplier = MathLib.wExp(linearVariation);
int256 linearAdaptation = speed * int256(elapsed);
uint256 adaptationMultiplier = MathLib.wExp(linearAdaptation);
// endRateAtTarget is bounded between MIN_RATE_AT_TARGET and MAX_RATE_AT_TARGET.
uint256 endRateAtTarget =
startRateAtTarget.wMulDown(variationMultiplier).bound(MIN_RATE_AT_TARGET, MAX_RATE_AT_TARGET);
startRateAtTarget.wMulDown(adaptationMultiplier).bound(MIN_RATE_AT_TARGET, MAX_RATE_AT_TARGET);
uint256 endBorrowRate = _curve(endRateAtTarget, err);

// Then we compute the average rate over the period.
// Note that startBorrowRate is defined in the computations below.
// avgBorrowRate = 1 / elapsed * ∫ startBorrowRate * exp(speed * t) dt between 0 and elapsed
// = startBorrowRate * (exp(linearVariation) - 1) / linearVariation
// = (endBorrowRate - startBorrowRate) / linearVariation
// And avgBorrowRate ~ startBorrowRate = endBorrowRate for linearVariation around zero.
// = startBorrowRate * (exp(linearAdaptation) - 1) / linearAdaptation
// = (endBorrowRate - startBorrowRate) / linearAdaptation
// And for linearAdaptation around zero: avgBorrowRate ~ startBorrowRate = endBorrowRate.
// Also, when it is the first interaction (rateAtTarget = 0).
uint256 avgBorrowRate;
if (linearVariation == 0) {
if (linearAdaptation == 0) {
avgBorrowRate = endBorrowRate;
} else {
uint256 startBorrowRate = _curve(startRateAtTarget, err);
// Safe "unchecked" cast to uint256 because linearVariation < 0 <=> variationMultiplier <= 1.
// Safe "unchecked" cast to uint256 because linearAdaptation < 0 <=> adaptationMultiplier <= 1.
// <=> endBorrowRate <= startBorrowRate.
avgBorrowRate = uint256((int256(endBorrowRate) - int256(startBorrowRate)).wDivDown(linearVariation));
avgBorrowRate = uint256((int256(endBorrowRate) - int256(startBorrowRate)).wDivDown(linearAdaptation));
}

return (avgBorrowRate, endRateAtTarget);
Expand Down
18 changes: 9 additions & 9 deletions test/SpeedJumpIrmTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ contract AdaptativeCurveIrmTest is Test {
uint256 rateAtTarget = irm.rateAtTarget(id);
int256 speed = ADJUSTMENT_SPEED.wMulDown(_err(market));
uint256 elapsed = (rateAtTarget > 0) ? block.timestamp - market.lastUpdate : 0;
int256 linearVariation = speed * int256(elapsed);
uint256 variationMultiplier = MathLib.wExp(linearVariation);
int256 linearAdaptation = speed * int256(elapsed);
uint256 adaptationMultiplier = MathLib.wExp(linearAdaptation);
return (rateAtTarget > 0)
? rateAtTarget.wMulDown(variationMultiplier).bound(irm.MIN_RATE_AT_TARGET(), irm.MAX_RATE_AT_TARGET())
? rateAtTarget.wMulDown(adaptationMultiplier).bound(irm.MIN_RATE_AT_TARGET(), irm.MAX_RATE_AT_TARGET())
: INITIAL_RATE_AT_TARGET;
}

Expand All @@ -206,17 +206,17 @@ contract AdaptativeCurveIrmTest is Test {
int256 err = _err(market);
int256 speed = ADJUSTMENT_SPEED.wMulDown(err);
uint256 elapsed = (rateAtTarget > 0) ? block.timestamp - market.lastUpdate : 0;
int256 linearVariation = speed * int256(elapsed);
uint256 newRateAtTarget = _expectedRateAtTarget(id, market);
uint256 newBorrowRate = _curve(newRateAtTarget, err);
int256 linearAdaptation = speed * int256(elapsed);
uint256 endRateAtTarget = _expectedRateAtTarget(id, market);
uint256 newBorrowRate = _curve(endRateAtTarget, err);

uint256 avgBorrowRate;
if (linearVariation == 0 || rateAtTarget == 0) {
if (linearAdaptation == 0 || rateAtTarget == 0) {
avgBorrowRate = newBorrowRate;
} else {
// Safe "unchecked" cast to uint256 because linearVariation < 0 <=> newBorrowRate <= borrowRateAfterJump.
// Safe "unchecked" cast to uint256 because linearAdaptation < 0 <=> newBorrowRate <= borrowRateAfterJump.
avgBorrowRate =
uint256((int256(newBorrowRate) - int256(_curve(rateAtTarget, err))).wDivDown(linearVariation));
uint256((int256(newBorrowRate) - int256(_curve(rateAtTarget, err))).wDivDown(linearAdaptation));
}
return avgBorrowRate;
}
Expand Down

0 comments on commit 222b04e

Please sign in to comment.