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

test(irm): add ping tests #80

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
86 changes: 85 additions & 1 deletion test/SpeedJumpIrmTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ contract AdaptativeCurveIrmTest is Test {

function testFirstBorrowRateUtilizationTarget() public {
Market memory market;
market.totalBorrowAssets = 0.9 ether;
market.totalBorrowAssets = uint128(uint256(TARGET_UTILIZATION));
market.totalSupplyAssets = 1 ether;

assertEq(irm.borrowRate(marketParams, market), uint256(INITIAL_RATE_AT_TARGET), "avgBorrowRate");
Expand Down Expand Up @@ -121,6 +121,90 @@ contract AdaptativeCurveIrmTest is Test {
assertApproxEqRel(irm.borrowRateView(marketParams, market), uint256(0.00057 ether) / 365 days, 0.1 ether);
}

function testRateAfter60DaysUtilizationAboveTargetNoPing() public {
Market memory market;
market.totalSupplyAssets = 1 ether;
market.totalBorrowAssets = uint128(uint256(TARGET_UTILIZATION));
assertEq(irm.borrowRate(marketParams, market), uint256(INITIAL_RATE_AT_TARGET));
assertEq(irm.rateAtTarget(marketParams.id()), INITIAL_RATE_AT_TARGET);
Rubilmax marked this conversation as resolved.
Show resolved Hide resolved

market.lastUpdate = uint128(block.timestamp);
vm.warp(block.timestamp + 60 days);

market.totalBorrowAssets = uint128(uint256(TARGET_UTILIZATION + 1 ether) / 2);
irm.borrowRate(marketParams, market);

assertApproxEqRel(irm.rateAtTarget(marketParams.id()), int256(0.6092 ether) / 365 days, 0.0001 ether);
Rubilmax marked this conversation as resolved.
Show resolved Hide resolved
}

function testRateAfter60DaysUtilizationAboveTargetPingEveryHour() public {
Market memory market;
market.totalSupplyAssets = 1 ether;
market.totalBorrowAssets = uint128(uint256(TARGET_UTILIZATION));
assertEq(irm.borrowRate(marketParams, market), uint256(INITIAL_RATE_AT_TARGET));
assertEq(irm.rateAtTarget(marketParams.id()), INITIAL_RATE_AT_TARGET);

market.totalBorrowAssets = uint128(uint256(TARGET_UTILIZATION + 1 ether) / 2); // Error = 50%

for (uint256 i; i < 60 days / 1 hours; ++i) {
market.lastUpdate = uint128(block.timestamp);
vm.warp(block.timestamp + 1 hours);

uint256 avgBorrowRate = irm.borrowRate(marketParams, market);
uint256 interest = market.totalBorrowAssets.wMulDown(avgBorrowRate.wTaylorCompounded(1 hours));
market.totalSupplyAssets += uint128(interest);
market.totalBorrowAssets += uint128(interest);
}

assertApproxEqRel(market.totalBorrowAssets.wDivDown(market.totalSupplyAssets), 0.95 ether, 0.005 ether);
assertApproxEqRel(irm.rateAtTarget(marketParams.id()), int256(0.6092 ether) / 365 days, 0.06 ether);
Rubilmax marked this conversation as resolved.
Show resolved Hide resolved
Rubilmax marked this conversation as resolved.
Show resolved Hide resolved
}

function testRateAfterUtilizationTargetNoPing(uint256 elapsed) public {
elapsed = bound(elapsed, 0, type(uint48).max);

Market memory market;
market.totalSupplyAssets = 1 ether;
market.totalBorrowAssets = uint128(uint256(TARGET_UTILIZATION));
assertEq(irm.borrowRate(marketParams, market), uint256(INITIAL_RATE_AT_TARGET));
assertEq(irm.rateAtTarget(marketParams.id()), INITIAL_RATE_AT_TARGET);

market.lastUpdate = uint128(block.timestamp);
vm.warp(block.timestamp + elapsed);

irm.borrowRate(marketParams, market);

assertEq(irm.rateAtTarget(marketParams.id()), INITIAL_RATE_AT_TARGET);
}

function testRateAfter3WeeksUtilizationTargetPingEveryMinute() public {
int256 initialRateAtTarget = int256(1 ether) / 365 days; // 100%

irm =
new AdaptativeCurveIrm(address(this), CURVE_STEEPNESS, ADJUSTMENT_SPEED, TARGET_UTILIZATION, initialRateAtTarget);

Market memory market;
market.totalSupplyAssets = 1 ether;
market.totalBorrowAssets = uint128(uint256(TARGET_UTILIZATION));
assertEq(irm.borrowRate(marketParams, market), uint256(initialRateAtTarget));
assertEq(irm.rateAtTarget(marketParams.id()), initialRateAtTarget);

for (uint256 i; i < 3 weeks / 1 minutes; ++i) {
market.lastUpdate = uint128(block.timestamp);
vm.warp(block.timestamp + 1 minutes);

uint256 avgBorrowRate = irm.borrowRate(marketParams, market);
uint256 interest = market.totalBorrowAssets.wMulDown(avgBorrowRate.wTaylorCompounded(1 minutes));
market.totalSupplyAssets += uint128(interest);
market.totalBorrowAssets += uint128(interest);
}

assertApproxEqRel(
market.totalBorrowAssets.wDivDown(market.totalSupplyAssets), uint256(TARGET_UTILIZATION), 0.01 ether
);
assertApproxEqRel(irm.rateAtTarget(marketParams.id()), initialRateAtTarget, 0.1 ether);
QGarchery marked this conversation as resolved.
Show resolved Hide resolved
}

function testFirstBorrowRate(Market memory market) public {
vm.assume(market.totalBorrowAssets > 0);
vm.assume(market.totalSupplyAssets >= market.totalBorrowAssets);
Expand Down