From f178c5876f40ea26f540ef6189fa9e85f919debb Mon Sep 17 00:00:00 2001 From: Quentin Garchery Date: Sun, 29 Sep 2024 15:40:41 +0200 Subject: [PATCH 1/3] test: fix precision of repaid assets computation --- test/forge/invariant/MorphoInvariantTest.sol | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/forge/invariant/MorphoInvariantTest.sol b/test/forge/invariant/MorphoInvariantTest.sol index 8e007e9d..6b4866ca 100644 --- a/test/forge/invariant/MorphoInvariantTest.sol +++ b/test/forge/invariant/MorphoInvariantTest.sol @@ -170,12 +170,11 @@ contract MorphoInvariantTest is InvariantTest { { uint256 collateralPrice = oracle.price(); uint256 liquidationIncentiveFactor = _liquidationIncentiveFactor(_marketParams.lltv); - Market memory _market = morpho.market(_marketParams.id()); + (,, uint256 totalBorrowAssets, uint256 totalBorrowShares) = morpho.expectedMarketBalances(_marketParams); uint256 seizedAssetsQuoted = seizedAssets.mulDivUp(collateralPrice, ORACLE_PRICE_SCALE); - uint256 repaidShares = seizedAssetsQuoted.wDivUp(liquidationIncentiveFactor).toSharesUp( - _market.totalBorrowAssets, _market.totalBorrowShares - ); - uint256 repaidAssets = repaidShares.toAssetsUp(_market.totalBorrowAssets, _market.totalBorrowShares); + uint256 repaidShares = + seizedAssetsQuoted.wDivUp(liquidationIncentiveFactor).toSharesUp(totalBorrowAssets, totalBorrowShares); + uint256 repaidAssets = repaidShares.toAssetsUp(totalBorrowAssets, totalBorrowShares); loanToken.setBalance(msg.sender, repaidAssets); From fc6793a8ede377a6fc6c501de9ea53ba9e270c1f Mon Sep 17 00:00:00 2001 From: Quentin Garchery Date: Sun, 29 Sep 2024 22:25:58 +0200 Subject: [PATCH 2/3] fix: truly bound unhealthy --- test/forge/BaseTest.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/forge/BaseTest.sol b/test/forge/BaseTest.sol index 29020d1a..99dd0152 100644 --- a/test/forge/BaseTest.sol +++ b/test/forge/BaseTest.sol @@ -189,7 +189,7 @@ contract BaseTest is Test { amountBorrowed.wDivDown(marketParams.lltv).mulDivDown(ORACLE_PRICE_SCALE, priceCollateral); amountCollateral = bound(amountCollateral, 0, Math.min(maxCollateral, MAX_COLLATERAL_ASSETS)); - vm.assume(amountCollateral > 0); + vm.assume(amountCollateral > 0 && amountCollateral < maxCollateral); return (amountCollateral, amountBorrowed, priceCollateral); } From 208c886d813cf24e96776d06ae27d02e46252c08 Mon Sep 17 00:00:00 2001 From: Quentin Garchery Date: Sun, 29 Sep 2024 23:35:30 +0200 Subject: [PATCH 3/3] fix: bound liquidate seized assets rounding up --- test/forge/BaseTest.sol | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/forge/BaseTest.sol b/test/forge/BaseTest.sol index 99dd0152..13a23237 100644 --- a/test/forge/BaseTest.sol +++ b/test/forge/BaseTest.sol @@ -330,13 +330,15 @@ contract BaseTest is Test { { Id _id = _marketParams.id(); - uint256 collateral = morpho.collateral(_id, borrower); uint256 collateralPrice = IOracle(_marketParams.oracle).price(); - uint256 maxRepaidAssets = morpho.expectedBorrowAssets(_marketParams, borrower); + uint256 borrowShares = morpho.borrowShares(_id, borrower); + (,, uint256 totalBorrowAssets, uint256 totalBorrowShares) = morpho.expectedMarketBalances(_marketParams); + uint256 maxRepaidAssets = borrowShares.toAssetsDown(totalBorrowAssets, totalBorrowShares); uint256 maxSeizedAssets = maxRepaidAssets.wMulDown(_liquidationIncentiveFactor(_marketParams.lltv)).mulDivDown( ORACLE_PRICE_SCALE, collateralPrice ); + uint256 collateral = morpho.collateral(_id, borrower); return bound(seizedAssets, 0, Math.min(collateral, maxSeizedAssets)); }