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

Sliced accrual: add double rate print #83

Closed
Closed
Changes from all 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
83 changes: 49 additions & 34 deletions test/IntegrationAdaptativeCurveIrmTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity ^0.8.0;

import "../src/SpeedJumpIrm.sol";
import "../lib/forge-std/src/Test.sol";
import {BaseTest,IrmMock,IMorpho,MorphoLib} from "../lib/morpho-blue/test/forge/BaseTest.sol";
import {BaseTest, IrmMock, IMorpho, MorphoLib} from "../lib/morpho-blue/test/forge/BaseTest.sol";

contract IntegrationAdaptativeCurveIrmTest is BaseTest {
using MarketParamsLib for MarketParams;
Expand All @@ -16,11 +16,13 @@ contract IntegrationAdaptativeCurveIrmTest is BaseTest {

AdaptativeCurveIrm aIrm;

function _freshMarket(uint supply, uint borrow) internal returns (MarketParams memory) {
function _freshMarket(uint256 supply, uint256 borrow) internal returns (MarketParams memory) {
aIrm =
new AdaptativeCurveIrm(address(morpho), uint256(CURVE_STEEPNESS), uint256(ADJUSTMENT_SPEED), uint256(TARGET_UTILIZATION), INITIAL_RATE_AT_TARGET);

marketParams = MarketParams(address(loanToken), address(collateralToken), address(oracle), address(aIrm), DEFAULT_TEST_LLTV);
marketParams = MarketParams(
address(loanToken), address(collateralToken), address(oracle), address(aIrm), DEFAULT_TEST_LLTV
);
id = marketParams.id();

vm.startPrank(OWNER);
Expand All @@ -30,52 +32,63 @@ contract IntegrationAdaptativeCurveIrmTest is BaseTest {

// Existing market

loanToken.setBalance(SUPPLIER,supply);
loanToken.setBalance(SUPPLIER, supply);
vm.prank(SUPPLIER);
morpho.supply(marketParams, supply, 0, SUPPLIER, hex"");


collateralToken.setBalance(BORROWER,1e30);
collateralToken.setBalance(BORROWER, 1e30);
vm.prank(BORROWER);
morpho.supplyCollateral(marketParams, 1e30, BORROWER, hex"");
vm.prank(BORROWER);
morpho.borrow(marketParams,borrow,0,BORROWER,BORROWER);
morpho.borrow(marketParams, borrow, 0, BORROWER, BORROWER);
return marketParams;
}

// Make stepped accrual fail
// Will revert due to overflow for a long enough duration
// Overflow occurs in _accrueInterest when interests are added do supply/borrow assets
// Overflow occurs in _accrueInterest when interests are added to supply/borrow assets
function testSteppedAccrual() public {
_freshMarket({supply: 1e18, borrow: 0.9e18});
uint duration = 13 weeks;
uint period = 1000 seconds;
for (uint i=0;i<duration/period;i++) {
uint256 duration = 1000 weeks;
uint256 period = 1000 seconds;
uint256 borrow = morpho.totalBorrowAssets(id);
uint256 refRate = INITIAL_RATE_AT_TARGET;
uint256 refI;
for (uint256 i = 0; i < duration / period; i++) {
_forward(period);
morpho.accrueInterest(marketParams);
uint256 nextStepBorrow = morpho.totalBorrowAssets(id);
uint256 rate = (nextStepBorrow - borrow) / 1000;
borrow = nextStepBorrow;
if (rate > 2 * refRate) {
// `(i - refI) * 1000` is the time elapsed to double the rate
console.log(((i - refI) * 1000 * 100) / (1 days));
refRate = rate;
refI = i;
}
}
}

// Compare stepped accrual (1 accrual per period over duration) vs. leap accrual (1 accrual at end of duration)
function testCompareAccrualMethods() public {
// Setup
uint duration = 9 weeks;
uint period = 1000 seconds;
duration = duration-(duration%period); // exact loop iterations for step market
uint256 duration = 9 weeks;
uint256 period = 1000 seconds;
duration = duration - (duration % period); // exact loop iterations for step market

MarketParams memory stepMarketParams = _freshMarket({supply:1e18, borrow: 0.9e18});
MarketParams memory leapMarketParams = _freshMarket({supply:1e18, borrow: 0.9e18});
MarketParams memory stepMarketParams = _freshMarket({supply: 1e18, borrow: 0.9e18});
MarketParams memory leapMarketParams = _freshMarket({supply: 1e18, borrow: 0.9e18});

_forward(1);
morpho.accrueInterest(stepMarketParams);
morpho.accrueInterest(leapMarketParams);

// Same initial borrow for both
uint initialBorrow = morpho.totalBorrowAssets(stepMarketParams.id());
uint256 initialBorrow = morpho.totalBorrowAssets(stepMarketParams.id());

// Accrue step market

for (uint i=0;i<duration/period;i++) {
for (uint256 i = 0; i < duration / period; i++) {
_forward(period);
morpho.accrueInterest(stepMarketParams);
}
Expand All @@ -86,27 +99,29 @@ contract IntegrationAdaptativeCurveIrmTest is BaseTest {

// Results

uint stepFinalBorrow = morpho.totalBorrowAssets(stepMarketParams.id());
uint leapFinalBorrow = morpho.totalBorrowAssets(leapMarketParams.id());
uint256 stepFinalBorrow = morpho.totalBorrowAssets(stepMarketParams.id());
uint256 leapFinalBorrow = morpho.totalBorrowAssets(leapMarketParams.id());

console.log("initial borrow ",initialBorrow);
console.log("step final borrow ",stepFinalBorrow);
console.log("leap final borrow ",leapFinalBorrow);
console.log("initial borrow ", initialBorrow);
console.log("step final borrow ", stepFinalBorrow);
console.log("leap final borrow ", leapFinalBorrow);
console.log();
uint stepBorrowIncrease = stepFinalBorrow-initialBorrow;
uint leapBorrowIncrease = leapFinalBorrow-initialBorrow;
console.log("step borrow increase",stepBorrowIncrease);
console.log("leap borrow increase",leapBorrowIncrease);
uint256 stepBorrowIncrease = stepFinalBorrow - initialBorrow;
uint256 leapBorrowIncrease = leapFinalBorrow - initialBorrow;
console.log("step borrow increase", stepBorrowIncrease);
console.log("leap borrow increase", leapBorrowIncrease);
console.log();

console.log("1 block step rate ",IIrm(irm).borrowRateView(stepMarketParams,morpho.market(stepMarketParams.id())));
console.log("1 block leap rate ",IIrm(irm).borrowRateView(leapMarketParams,morpho.market(leapMarketParams.id())));
console.log(
"1 block step rate ", IIrm(irm).borrowRateView(stepMarketParams, morpho.market(stepMarketParams.id()))
);
console.log(
"1 block leap rate ", IIrm(irm).borrowRateView(leapMarketParams, morpho.market(leapMarketParams.id()))
);
console.log();
console.log("block.number ",block.number);
console.log("block.timestamp ",block.timestamp);
console.log("block.number ", block.number);
console.log("block.timestamp ", block.timestamp);

console.log(unicode"stepΔ/leapΔ %s%",stepBorrowIncrease*100/leapBorrowIncrease);
console.log(unicode"stepΔ/leapΔ %s%", stepBorrowIncrease * 100 / leapBorrowIncrease);
}


}
Loading