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

UtilsLib.zeroFloorSub is not utilised consistently #557

Merged
merged 8 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 deletions src/Morpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,13 @@ contract Morpho is IMorpho {
uint256 badDebtShares;
if (position[id][borrower].collateral == 0) {
badDebtShares = position[id][borrower].borrowShares;
uint256 badDebt = badDebtShares.toAssetsUp(market[id].totalBorrowAssets, market[id].totalBorrowShares);
market[id].totalSupplyAssets -= badDebt.toUint128();
uint256 badDebt = UtilsLib.min(
market[id].totalBorrowAssets,
badDebtShares.toAssetsUp(market[id].totalBorrowAssets, market[id].totalBorrowShares)
);

market[id].totalBorrowAssets -= badDebt.toUint128();
market[id].totalSupplyAssets -= badDebt.toUint128();
market[id].totalBorrowShares -= badDebtShares.toUint128();
position[id][borrower].borrowShares = 0;
}
Expand Down
20 changes: 20 additions & 0 deletions test/forge/integration/LiquidateIntegrationTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,24 @@ contract LiquidateIntegrationTest is BaseTest {
morpho.totalSupplyAssets(id), params.totalSupplyBeforeLiquidation - params.expectedBadDebt, "total supply"
);
}

function testBadDebtOverTotalBorrowAssets() public {
uint256 collateralAmount = 10 ether;
uint256 loanAmount = 1 ether;
_supply(loanAmount);

collateralToken.setBalance(BORROWER, collateralAmount);
vm.startPrank(BORROWER);
morpho.supplyCollateral(marketParams, collateralAmount, BORROWER, hex"");
morpho.borrow(marketParams, loanAmount, 0, BORROWER, BORROWER);
// Trick to inflate shares, so that the computed bad debt is greater than the total debt of the market.
morpho.borrow(marketParams, 0, 1, BORROWER, BORROWER);
vm.stopPrank();

oracle.setPrice(1e36 / 100);

loanToken.setBalance(LIQUIDATOR, loanAmount);
vm.prank(LIQUIDATOR);
morpho.liquidate(marketParams, BORROWER, collateralAmount, 0, hex"");
}
}
Loading