Skip to content

Commit

Permalink
test: add double rate print
Browse files Browse the repository at this point in the history
  • Loading branch information
QGarchery committed Nov 15, 2023
1 parent e6dc7bb commit 6bf9ac0
Showing 1 changed file with 48 additions and 34 deletions.
82 changes: 48 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,62 @@ 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 refBorrow = borrow;
uint256 refI;
for (uint256 i = 0; i < duration / period; i++) {
_forward(period);
morpho.accrueInterest(marketParams);
uint256 newBorrow = morpho.totalBorrowAssets(id);
borrow = newBorrow;
if (borrow > 2 * refBorrow) {
// `(i - refI) * 1000` is the time elapsed to double the rate
console.log(((i - refI) * 1000 * 100) / (1 days));
refBorrow = borrow;
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 +98,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);
}


}

0 comments on commit 6bf9ac0

Please sign in to comment.