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

Add liquidate margin test #598

Merged
merged 2 commits into from
Nov 24, 2023
Merged
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
37 changes: 37 additions & 0 deletions test/forge/integration/LiquidateIntegrationTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,43 @@ contract LiquidateIntegrationTest is BaseTest {
uint256 lltv;
}

function testLiquidateMargin(LiquidateTestParams memory params, uint256 amountSeized, uint256 elapsed) public {
_setLltv(_boundTestLltv(params.lltv));
(params.amountCollateral, params.amountBorrowed, params.priceCollateral) =
_boundHealthyPosition(params.amountCollateral, params.amountBorrowed, 1e36);

elapsed = bound(elapsed, 0, 365 days);

params.amountSupplied =
bound(params.amountSupplied, params.amountBorrowed, params.amountBorrowed + MAX_TEST_AMOUNT);
_supply(params.amountSupplied);

amountSeized = bound(amountSeized, 1, params.amountCollateral);

oracle.setPrice(params.priceCollateral);

loanToken.setBalance(LIQUIDATOR, params.amountBorrowed);
collateralToken.setBalance(BORROWER, params.amountCollateral);

vm.startPrank(BORROWER);
morpho.supplyCollateral(marketParams, params.amountCollateral, BORROWER, hex"");
morpho.borrow(marketParams, params.amountBorrowed, 0, BORROWER, BORROWER);
vm.stopPrank();

// We have to estimate the ratio after borrowing because the borrow rate depends on the utilization.
uint256 maxRatio = WAD + irm.borrowRate(marketParams, morpho.market(id)).wTaylorCompounded(elapsed);
// Sanity check: multiply maxBorrow by 2.
uint256 maxBorrow = _maxBorrow(marketParams, BORROWER).wDivDown(maxRatio);
// Should not omit too many tests because elapsed is reasonably bounded.
vm.assume(params.amountBorrowed < maxBorrow);

vm.warp(block.timestamp + elapsed);

vm.prank(LIQUIDATOR);
vm.expectRevert(bytes(ErrorsLib.HEALTHY_POSITION));
morpho.liquidate(marketParams, BORROWER, amountSeized, 0, hex"");
}

function testLiquidateSeizedInputNoBadDebtRealized(LiquidateTestParams memory params, uint256 amountSeized)
public
{
Expand Down