From 4b33eefe444761adda947d08fb9da86768c328c6 Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Thu, 7 Dec 2023 12:19:29 +0100 Subject: [PATCH 1/4] feat: log bad debt but require via ir --- src/Morpho.sol | 11 +++++++---- src/libraries/EventsLib.sol | 1 + test/forge/integration/LiquidateIntegrationTest.sol | 5 +++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Morpho.sol b/src/Morpho.sol index f755904b6..e522024ef 100644 --- a/src/Morpho.sol +++ b/src/Morpho.sol @@ -384,15 +384,16 @@ contract Morpho is IMorphoStaticTyping { position[id][borrower].collateral -= seizedAssets.toUint128(); uint256 badDebtShares; + uint256 badDebtAssets; if (position[id][borrower].collateral == 0) { badDebtShares = position[id][borrower].borrowShares; - uint256 badDebt = UtilsLib.min( + badDebtAssets = 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].totalBorrowAssets -= badDebtAssets.toUint128(); + market[id].totalSupplyAssets -= badDebtAssets.toUint128(); market[id].totalBorrowShares -= badDebtShares.toUint128(); position[id][borrower].borrowShares = 0; } @@ -400,7 +401,9 @@ contract Morpho is IMorphoStaticTyping { IERC20(marketParams.collateralToken).safeTransfer(msg.sender, seizedAssets); // `repaidAssets` may be greater than `totalBorrowAssets` by 1. - emit EventsLib.Liquidate(id, msg.sender, borrower, repaidAssets, repaidShares, seizedAssets, badDebtShares); + emit EventsLib.Liquidate( + id, msg.sender, borrower, repaidAssets, repaidShares, seizedAssets, badDebtAssets, badDebtShares + ); if (data.length > 0) IMorphoLiquidateCallback(msg.sender).onMorphoLiquidate(repaidAssets, data); diff --git a/src/libraries/EventsLib.sol b/src/libraries/EventsLib.sol index 825ea1791..2ab1cdfc7 100644 --- a/src/libraries/EventsLib.sol +++ b/src/libraries/EventsLib.sol @@ -114,6 +114,7 @@ library EventsLib { uint256 repaidAssets, uint256 repaidShares, uint256 seizedAssets, + uint256 badDebtAssets, uint256 badDebtShares ); diff --git a/test/forge/integration/LiquidateIntegrationTest.sol b/test/forge/integration/LiquidateIntegrationTest.sol index 0fb9325c9..f59223f37 100644 --- a/test/forge/integration/LiquidateIntegrationTest.sol +++ b/test/forge/integration/LiquidateIntegrationTest.sol @@ -154,7 +154,7 @@ contract LiquidateIntegrationTest is BaseTest { vm.prank(LIQUIDATOR); vm.expectEmit(true, true, true, true, address(morpho)); - emit EventsLib.Liquidate(id, LIQUIDATOR, BORROWER, expectedRepaid, expectedRepaidShares, amountSeized, 0); + emit EventsLib.Liquidate(id, LIQUIDATOR, BORROWER, expectedRepaid, expectedRepaidShares, amountSeized, 0, 0); (uint256 returnSeized, uint256 returnRepaid) = morpho.liquidate(marketParams, BORROWER, amountSeized, 0, hex""); uint256 expectedCollateral = params.amountCollateral - amountSeized; @@ -214,7 +214,7 @@ contract LiquidateIntegrationTest is BaseTest { vm.prank(LIQUIDATOR); vm.expectEmit(true, true, true, true, address(morpho)); - emit EventsLib.Liquidate(id, LIQUIDATOR, BORROWER, expectedRepaid, sharesRepaid, expectedSeized, 0); + emit EventsLib.Liquidate(id, LIQUIDATOR, BORROWER, expectedRepaid, sharesRepaid, expectedSeized, 0, 0); (uint256 returnSeized, uint256 returnRepaid) = morpho.liquidate(marketParams, BORROWER, 0, sharesRepaid, hex""); uint256 expectedCollateral = params.amountCollateral - expectedSeized; @@ -303,6 +303,7 @@ contract LiquidateIntegrationTest is BaseTest { params.expectedRepaid, params.expectedRepaidShares, amountCollateral, + params.expectedBadDebt, params.expectedBadDebt * SharesMathLib.VIRTUAL_SHARES ); (uint256 returnSeized, uint256 returnRepaid) = From 8d81c3829a1bcf7e8c87061b203f222f4dd222e0 Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Thu, 7 Dec 2023 12:24:01 +0100 Subject: [PATCH 2/4] refactor: no need for via ir anymore --- src/Morpho.sol | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Morpho.sol b/src/Morpho.sol index e522024ef..787013200 100644 --- a/src/Morpho.sol +++ b/src/Morpho.sol @@ -354,12 +354,12 @@ contract Morpho is IMorphoStaticTyping { _accrueInterest(marketParams, id); - uint256 collateralPrice = IOracle(marketParams.oracle).price(); - - require(!_isHealthy(marketParams, id, borrower, collateralPrice), ErrorsLib.HEALTHY_POSITION); - uint256 repaidAssets; { + uint256 collateralPrice = IOracle(marketParams.oracle).price(); + + require(!_isHealthy(marketParams, id, borrower, collateralPrice), ErrorsLib.HEALTHY_POSITION); + // The liquidation incentive factor is min(maxLiquidationIncentiveFactor, 1/(1 - cursor*(1 - lltv))). uint256 liquidationIncentiveFactor = UtilsLib.min( MAX_LIQUIDATION_INCENTIVE_FACTOR, From e7bfeeae9908b3e6f59ad8a42215eed685e1fa68 Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Thu, 7 Dec 2023 12:25:40 +0100 Subject: [PATCH 3/4] docs: add missing param --- src/libraries/EventsLib.sol | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libraries/EventsLib.sol b/src/libraries/EventsLib.sol index 2ab1cdfc7..5b35fa9eb 100644 --- a/src/libraries/EventsLib.sol +++ b/src/libraries/EventsLib.sol @@ -106,6 +106,7 @@ library EventsLib { /// @param repaidAssets The amount of assets repaid. May be 1 over the corresponding market's `totalBorrowAssets`. /// @param repaidShares The amount of shares burned. /// @param seizedAssets The amount of collateral seized. + /// @param badDebtAssets The amount of assets minted as bad debt. /// @param badDebtShares The amount of shares minted as bad debt. event Liquidate( Id indexed id, From 44281bda82f193231a15e0a9b468edd72bfb77c7 Mon Sep 17 00:00:00 2001 From: Merlin Egalite <44097430+MerlinEgalite@users.noreply.github.com> Date: Thu, 7 Dec 2023 14:12:42 +0100 Subject: [PATCH 4/4] docs: apply suggestion Co-authored-by: MathisGD <74971347+MathisGD@users.noreply.github.com> Signed-off-by: Merlin Egalite <44097430+MerlinEgalite@users.noreply.github.com> --- src/libraries/EventsLib.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/EventsLib.sol b/src/libraries/EventsLib.sol index 5b35fa9eb..166f4d296 100644 --- a/src/libraries/EventsLib.sol +++ b/src/libraries/EventsLib.sol @@ -106,8 +106,8 @@ library EventsLib { /// @param repaidAssets The amount of assets repaid. May be 1 over the corresponding market's `totalBorrowAssets`. /// @param repaidShares The amount of shares burned. /// @param seizedAssets The amount of collateral seized. - /// @param badDebtAssets The amount of assets minted as bad debt. - /// @param badDebtShares The amount of shares minted as bad debt. + /// @param badDebtAssets The amount of assets of bad debt realized. + /// @param badDebtShares The amount of borrow shares of bad debt realized. event Liquidate( Id indexed id, address indexed caller,