Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trapezoidal (n=2) #91

Merged
merged 7 commits into from
Nov 16, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 17 additions & 21 deletions src/SpeedJumpIrm.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import {MarketParamsLib} from "../lib/morpho-blue/src/libraries/MarketParamsLib.
import {Id, MarketParams, Market} from "../lib/morpho-blue/src/interfaces/IMorpho.sol";
import {MathLib as MorphoMathLib} from "../lib/morpho-blue/src/libraries/MathLib.sol";

/// @dev Number of steps used in the Riemann sum.
/// @dev 4 steps allows to have a relative error below 30% for 15 days at err=1 or err=-1.
int256 constant N_STEPS = 4;

/// @title AdaptiveCurveIrm
/// @author Morpho Labs
/// @custom:contact security@morpho.org
Expand Down Expand Up @@ -147,24 +143,24 @@ contract AdaptiveCurveIrm is IIrm {
} else {
// Formula of the average rate that should be returned to Morpho Blue:
// avg = 1/T ∫_0^T curve(startRateAtTarget * exp(speed * x), err) dx
// The integral is approximated with a Riemann sum (steps of length T/N). To underestimate the rate, a
// left Riemann (a=0, b=N-1) is done when the rate goes up (err>0) and a right Riemann (a=1, b=N) is
// done when the rate goes down (err<0).
// avg ~= 1/T Σ_i=a^b curve(startRateAtTarget * exp(speed * T/N * i), err) * T / N
// ~= Σ_i=a^b curve(startRateAtTarget * exp(linearAdaptation/N * i), err) / N
// curve is linear in startRateAtTarget, so:
// ~= curve(Σ_i=a^b startRateAtTarget * exp(linearAdaptation/N * i), err) / N
// ~= curve(Σ_i=a^b startRateAtTarget * exp(linearAdaptation/N * i) / N, err)
int256 sumRateAtTarget;
int256 step = linearAdaptation / N_STEPS;
// Compute the terms 1 to N_STEPS - 1.
for (int256 k = 1; k < N_STEPS; k++) {
sumRateAtTarget += _newRateAtTarget(startRateAtTarget, step * k);
}
// The integral is approximated with the trapezoidal rule:
// avg ~= 1/T Σ_i=1^N (f((i-1) * T/N) + f(i * T/N)) / 2 * T/N
// Where f(x) = curve(startRateAtTarget * exp(speed * x), err).
// avg ~= 1/N * ( (f(0)+f(T))/2 + Σ_i=1^(N-1) f(i*T/N) )
// avg ~= 1/N * ( (f(startRateAtTarget)+f(endRateAtTarget))/2 + Σ_i=1^(N-1) curve(startRateAtTarget *
MathisGD marked this conversation as resolved.
Show resolved Hide resolved
// exp(speed * i*T/N), err))
// As curve is linear in rateAtTarget:
// avg ~= 1/N * curve((startRateAtTarget+endRateAtTarget)/2 + Σ_i=1^(N-1) startRateAtTarget * exp(speed
// * i*T/N), err)
// avg ~= curve((startRateAtTarget+endRateAtTarget)/(2*N) + Σ_i=1^(N-1) startRateAtTarget * exp(speed *
// i*T/N) / N, err)
// With N=2:
// avg ~= curve((startRateAtTarget+endRateAtTarget)/4 + startRateAtTarget * exp(speed * T/2) / 2, err)
// avg ~= curve((startRateAtTarget + endRateAtTarget + 2 * startRateAtTarget * exp(speed * T/2)) / 4,
// err)
endRateAtTarget = _newRateAtTarget(startRateAtTarget, linearAdaptation);
// Add the term 0 for a left Riemann or the term N_STEPS for a right Riemann.
sumRateAtTarget += err < 0 ? endRateAtTarget : startRateAtTarget;
avgRateAtTarget = sumRateAtTarget / N_STEPS;
int256 midRateAtTarget = _newRateAtTarget(startRateAtTarget, linearAdaptation / 2);
avgRateAtTarget = (startRateAtTarget + 2 * midRateAtTarget + endRateAtTarget) / 4;
}
}

Expand Down