Skip to content

Commit

Permalink
Merge pull request #557 from morpho-org/fix/zero-floor-sub-58
Browse files Browse the repository at this point in the history
`UtilsLib.zeroFloorSub` is not utilised consistently
  • Loading branch information
MerlinEgalite authored Nov 8, 2023
2 parents 2ae62b0 + 0e8174f commit e52ab6b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Morpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,13 @@ contract Morpho is IMorphoStaticTyping {
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"");
}
}

0 comments on commit e52ab6b

Please sign in to comment.