From b2f8bb992da0ce6c7dbc499a9e3524ac460ae2f5 Mon Sep 17 00:00:00 2001 From: Santiago Sanchez Avalos Date: Wed, 22 May 2024 16:31:30 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20market:=20accrue=20earnings=20be?= =?UTF-8?q?fore=20liquidation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/witty-actors-look.md | 5 + .gas-snapshot | 263 ++++++++++++++++---------------- contracts/Market.sol | 1 + test/Market.t.sol | 42 ++++- 4 files changed, 179 insertions(+), 132 deletions(-) create mode 100644 .changeset/witty-actors-look.md diff --git a/.changeset/witty-actors-look.md b/.changeset/witty-actors-look.md new file mode 100644 index 00000000..f162877e --- /dev/null +++ b/.changeset/witty-actors-look.md @@ -0,0 +1,5 @@ +--- +"@exactly/protocol": patch +--- + +🐛 market: accrue earnings before liquidation diff --git a/.gas-snapshot b/.gas-snapshot index 0044059e..ebe29583 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -49,7 +49,7 @@ DebtManagerTest:testFloatingToFixedRollHigherThanAvailableLiquidityWithSlippage( DebtManagerTest:testFloatingToFixedRollHigherThanAvailableLiquidityWithSlippageWithThreePools() (gas: 2787984) DebtManagerTest:testFloatingToFixedRollWithAccurateSlippage() (gas: 1687218) DebtManagerTest:testFloatingToFixedRollWithAccurateSlippageWithPreviousPosition() (gas: 1924341) -DebtManagerTest:testFuzzRolls(uint8[4],uint8[4],uint256[4],uint40[4],uint8[4]) (runs: 256, μ: 6526945, ~: 6552300) +DebtManagerTest:testFuzzRolls(uint8[4],uint8[4],uint256[4],uint40[4],uint8[4]) (runs: 256, μ: 6526942, ~: 6552300) DebtManagerTest:testLateFixedRoll() (gas: 1310831) DebtManagerTest:testLateFixedRollWithThreeLoops() (gas: 1897545) DebtManagerTest:testLateFixedToFloatingRoll() (gas: 1273784) @@ -137,149 +137,150 @@ InstallmentsRouterTest:testInsufficientMaxRepay() (gas: 630433) InstallmentsRouterTest:testMaxRepay() (gas: 630420) InstallmentsRouterTest:testMissingMarketWETH() (gas: 776941) InstallmentsRouterTest:testMoreBorrowsThanMaxPools() (gas: 634272) -InterestRateModelTest:testFixedBorrowRate() (gas: 2063708) -InterestRateModelTest:testFixedRateRevertAlreadyMatured() (gas: 2057890) -InterestRateModelTest:testFixedRateRevertUtilizationExceeded() (gas: 2065053) -InterestRateModelTest:testFloatingBorrowRate() (gas: 2057194) -InterestRateModelTest:testFuzzFixedRateGrowth(uint256,uint256,uint256,uint256) (runs: 256, μ: 2078736, ~: 2075558) -InterestRateModelTest:testFuzzFixedRateTimeSensitivity(uint256,uint256,uint256) (runs: 256, μ: 2084808, ~: 2084864) -InterestRateModelTest:testFuzzReferenceLegacyRateFixed(uint32,uint256,uint256[2],uint256[2],uint256,uint256,uint256) (runs: 256, μ: 10046611, ~: 10217461) -InterestRateModelTest:testFuzzReferenceRateFixed(uint256,uint256,uint256,uint256,uint256,uint256,(uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,int256,uint256,uint256)) (runs: 256, μ: 2349161, ~: 2351577) -InterestRateModelTest:testFuzzReferenceRateFloating(uint256,uint256,(uint256,uint256,uint256,uint256,uint256,uint256,uint256)) (runs: 256, μ: 2287219, ~: 2288115) -InterestRateModelTest:testMinTimeToMaturity() (gas: 2075208) +InterestRateModelTest:testFixedBorrowRate() (gas: 2063720) +InterestRateModelTest:testFixedRateRevertAlreadyMatured() (gas: 2057902) +InterestRateModelTest:testFixedRateRevertUtilizationExceeded() (gas: 2065065) +InterestRateModelTest:testFloatingBorrowRate() (gas: 2057206) +InterestRateModelTest:testFuzzFixedRateGrowth(uint256,uint256,uint256,uint256) (runs: 256, μ: 2078748, ~: 2075570) +InterestRateModelTest:testFuzzFixedRateTimeSensitivity(uint256,uint256,uint256) (runs: 256, μ: 2084820, ~: 2084876) +InterestRateModelTest:testFuzzReferenceLegacyRateFixed(uint32,uint256,uint256[2],uint256[2],uint256,uint256,uint256) (runs: 256, μ: 10052794, ~: 10223639) +InterestRateModelTest:testFuzzReferenceRateFixed(uint256,uint256,uint256,uint256,uint256,uint256,(uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,int256,uint256,uint256)) (runs: 256, μ: 2349173, ~: 2351589) +InterestRateModelTest:testFuzzReferenceRateFloating(uint256,uint256,(uint256,uint256,uint256,uint256,uint256,uint256,uint256)) (runs: 256, μ: 2287231, ~: 2288127) +InterestRateModelTest:testMinTimeToMaturity() (gas: 2075220) InterestRateModelTest:testRevertMaxUtilizationLowerThanWad() (gas: 267545) -MarketTest:testAccountLiquidityAdjustedDebt() (gas: 499479) -MarketTest:testAnotherUserRedeemWhenOwnerHasShortfall() (gas: 819114) +MarketTest:testAccountLiquidityAdjustedDebt() (gas: 499501) +MarketTest:testAccrueEarningsBeforeLiquidation() (gas: 1252277) +MarketTest:testAnotherUserRedeemWhenOwnerHasShortfall() (gas: 819136) MarketTest:testAnotherUserWithdrawWhenOwnerHasShortfall() (gas: 806873) -MarketTest:testBorrowAfterFreezing() (gas: 491482) -MarketTest:testBorrowAtMaturity() (gas: 499583) -MarketTest:testBorrowAtMaturityAfterFreezing() (gas: 573199) -MarketTest:testBorrowAtMaturityUpdatesFloatingDebtAndFloatingAssets() (gas: 884672) -MarketTest:testBorrowAtMaturityWhenFrozen() (gas: 93696) +MarketTest:testBorrowAfterFreezing() (gas: 491397) +MarketTest:testBorrowAtMaturity() (gas: 499605) +MarketTest:testBorrowAtMaturityAfterFreezing() (gas: 573221) +MarketTest:testBorrowAtMaturityUpdatesFloatingDebtAndFloatingAssets() (gas: 884694) +MarketTest:testBorrowAtMaturityWhenFrozen() (gas: 93718) MarketTest:testBorrowAtMaturityWithZeroAssets() (gas: 40403) -MarketTest:testBorrowDisagreement() (gas: 295454) -MarketTest:testBorrowFromFreeLunchShouldNotRevertWithFloatingFullUtilization() (gas: 1294331) -MarketTest:testBorrowWhenFrozen() (gas: 93202) +MarketTest:testBorrowDisagreement() (gas: 295476) +MarketTest:testBorrowFromFreeLunchShouldNotRevertWithFloatingFullUtilization() (gas: 1294353) +MarketTest:testBorrowWhenFrozen() (gas: 93224) MarketTest:testBorrowWithZeroAssets() (gas: 39749) -MarketTest:testCappedLiquidation() (gas: 1197621) +MarketTest:testCappedLiquidation() (gas: 1199762) MarketTest:testChargeTreasuryToEarlyWithdraws() (gas: 1218557) -MarketTest:testChargeTreasuryToFixedBorrows() (gas: 1588271) +MarketTest:testChargeTreasuryToFixedBorrows() (gas: 1588184) MarketTest:testClearBadDebtAvoidingFixedBorrowsIfAccumulatorLower() (gas: 2289189) -MarketTest:testClearBadDebtBeforeMaturity() (gas: 2347581) -MarketTest:testClearBadDebtCalledByAccount() (gas: 35021) -MarketTest:testClearBadDebtExactlyRepaysFixedBorrowWithAccumulatorAmount() (gas: 2020258) -MarketTest:testClearBadDebtPartiallyRepaysEachFixedBorrow() (gas: 1967633) +MarketTest:testClearBadDebtBeforeMaturity() (gas: 2345465) +MarketTest:testClearBadDebtCalledByAccount() (gas: 35043) +MarketTest:testClearBadDebtExactlyRepaysFixedBorrowWithAccumulatorAmount() (gas: 2020191) +MarketTest:testClearBadDebtPartiallyRepaysEachFixedBorrow() (gas: 1967655) MarketTest:testClearBadDebtPartiallyRepaysFloatingDebt() (gas: 2307443) -MarketTest:testClearBadDebtShouldAccrueAccumulatedEarningsBeforeSpreadingLosses() (gas: 2087072) -MarketTest:testClearBadDebtWithEmptyAccumulatorShouldNotRevert() (gas: 969888) -MarketTest:testClearMaturity() (gas: 1693655) -MarketTest:testCollectTreasuryFreeLunchToEarlyWithdraws() (gas: 1766321) -MarketTest:testCollectTreasuryFreeLunchToEarlyWithdrawsWithZeroFees() (gas: 559337) -MarketTest:testCollectTreasuryFreeLunchToFixedBorrows() (gas: 1820539) -MarketTest:testCollectTreasuryFreeLunchToFixedBorrowsWithZeroFees() (gas: 722735) -MarketTest:testCrossMaturityLiquidation() (gas: 2627290) -MarketTest:testDepositAfterFreezing() (gas: 256205) -MarketTest:testDepositAtMaturity() (gas: 193456) -MarketTest:testDepositAtMaturityAfterFreezing() (gas: 267449) -MarketTest:testDepositAtMaturityWhenFrozen() (gas: 93187) +MarketTest:testClearBadDebtShouldAccrueAccumulatedEarningsBeforeSpreadingLosses() (gas: 2087094) +MarketTest:testClearBadDebtWithEmptyAccumulatorShouldNotRevert() (gas: 969910) +MarketTest:testClearMaturity() (gas: 1693677) +MarketTest:testCollectTreasuryFreeLunchToEarlyWithdraws() (gas: 1766343) +MarketTest:testCollectTreasuryFreeLunchToEarlyWithdrawsWithZeroFees() (gas: 559359) +MarketTest:testCollectTreasuryFreeLunchToFixedBorrows() (gas: 1820561) +MarketTest:testCollectTreasuryFreeLunchToFixedBorrowsWithZeroFees() (gas: 722757) +MarketTest:testCrossMaturityLiquidation() (gas: 2625085) +MarketTest:testDepositAfterFreezing() (gas: 256227) +MarketTest:testDepositAtMaturity() (gas: 193478) +MarketTest:testDepositAtMaturityAfterFreezing() (gas: 267471) +MarketTest:testDepositAtMaturityWhenFrozen() (gas: 93209) MarketTest:testDepositAtMaturityWithZeroAssets() (gas: 39977) -MarketTest:testDepositDisagreement() (gas: 73882) -MarketTest:testDepositShouldUpdateFlexibleBorrowVariables() (gas: 716949) +MarketTest:testDepositDisagreement() (gas: 73904) +MarketTest:testDepositShouldUpdateFlexibleBorrowVariables() (gas: 716971) MarketTest:testDepositToSmartPool() (gas: 181827) MarketTest:testDepositWhenFrozen() (gas: 176471) -MarketTest:testDistributeMultipleAccumulatedEarnings() (gas: 1382756) -MarketTest:testDistributionOfLossesShouldReduceFromFloatingBackupBorrowedAccordingly() (gas: 6415682) -MarketTest:testEarlyRepayLiquidationUnassignedEarnings() (gas: 2116976) -MarketTest:testEarlyRepaymentWithExcessiveAmountOfFees() (gas: 3435387) -MarketTest:testEarlyWithdrawFromFreeLunchShouldNotRevertWithFloatingFullUtilization() (gas: 1050081) +MarketTest:testDistributeMultipleAccumulatedEarnings() (gas: 1382778) +MarketTest:testDistributionOfLossesShouldReduceFromFloatingBackupBorrowedAccordingly() (gas: 6420098) +MarketTest:testEarlyRepayLiquidationUnassignedEarnings() (gas: 2116887) +MarketTest:testEarlyRepaymentWithExcessiveAmountOfFees() (gas: 3435320) +MarketTest:testEarlyWithdrawFromFreeLunchShouldNotRevertWithFloatingFullUtilization() (gas: 1050103) MarketTest:testEmergencyAdminRole() (gas: 317263) -MarketTest:testEmitFrozen() (gas: 91114) -MarketTest:testFixedBorrowFailingWhenFlexibleBorrowAccruesDebt() (gas: 1530872) +MarketTest:testEmitFrozen() (gas: 91047) +MarketTest:testFixedBorrowFailingWhenFlexibleBorrowAccruesDebt() (gas: 1530894) MarketTest:testFixedBorrowRateToMaturity() (gas: 562527) MarketTest:testFixedOperationsUpdateFloatingDebt() (gas: 865402) -MarketTest:testFlexibleBorrow() (gas: 423743) +MarketTest:testFlexibleBorrow() (gas: 423656) MarketTest:testFlexibleBorrowAccountingDebt() (gas: 607131) MarketTest:testFlexibleBorrowChargingDebtToTreasury() (gas: 751155) -MarketTest:testFlexibleBorrowExceedingReserve() (gas: 878408) -MarketTest:testFlexibleBorrowExceedingReserveIncludingFixedBorrow() (gas: 1267573) -MarketTest:testFlexibleBorrowExceedingReserveWithNewDebt() (gas: 1001187) +MarketTest:testFlexibleBorrowExceedingReserve() (gas: 878430) +MarketTest:testFlexibleBorrowExceedingReserveIncludingFixedBorrow() (gas: 1267485) +MarketTest:testFlexibleBorrowExceedingReserveWithNewDebt() (gas: 1001209) MarketTest:testFlexibleBorrowFromAnotherUserSubtractsAllowance() (gas: 469265) -MarketTest:testFlexibleBorrowFromAnotherUserWithAllowance() (gas: 458659) -MarketTest:testFlexibleBorrowFromAnotherUserWithoutAllowance() (gas: 252359) -MarketTest:testFrontRunSmartPoolEarningsDistributionWithBigPenaltyRepayment() (gas: 1382035) -MarketTest:testFullPause() (gas: 5956376) +MarketTest:testFlexibleBorrowFromAnotherUserWithAllowance() (gas: 458681) +MarketTest:testFlexibleBorrowFromAnotherUserWithoutAllowance() (gas: 252381) +MarketTest:testFrontRunSmartPoolEarningsDistributionWithBigPenaltyRepayment() (gas: 1381968) +MarketTest:testFullPause() (gas: 5954954) MarketTest:testInitiallyUnfrozen() (gas: 15592) -MarketTest:testInsufficientProtocolLiquidity() (gas: 1940396) -MarketTest:testLiquidateAndChargeIncentiveForLenders() (gas: 2431316) -MarketTest:testLiquidateAndDistributeLosses() (gas: 3240850) -MarketTest:testLiquidateAndSeizeExactAmountWithDustAsCollateral() (gas: 2807946) -MarketTest:testLiquidateAndSeizeFromEmptyCollateral() (gas: 1039341) -MarketTest:testLiquidateAndSubtractLossesFromAccumulator() (gas: 3915764) -MarketTest:testLiquidateFlexibleAndFixedBorrowPositionsInSingleCall() (gas: 2631898) -MarketTest:testLiquidateFlexibleBorrow() (gas: 2156292) -MarketTest:testLiquidateFlexibleBorrowChargeLendersAssetsToLiquidator() (gas: 1150267) -MarketTest:testLiquidateFlexibleBorrowConsideringDebtOverTime() (gas: 1166248) -MarketTest:testLiquidateLeavingDustAsCollateral() (gas: 3634794) -MarketTest:testLiquidateTransferRepayAssetsBeforeSeize() (gas: 1045448) -MarketTest:testLiquidateUpdateFloatingDebt() (gas: 1901804) -MarketTest:testLiquidateWhenFrozen() (gas: 1230766) -MarketTest:testLiquidateWithTwoUnitsAsMaxAssets() (gas: 1576998) -MarketTest:testLiquidateWithZeroAsMaxAssets() (gas: 1039182) -MarketTest:testLiquidationClearingDebtOfAllAccountMarkets() (gas: 3105164) -MarketTest:testLiquidationResultingInZeroCollateralAndZeroDebt() (gas: 1981125) -MarketTest:testMaturityInsufficientProtocolLiquidity() (gas: 1464202) -MarketTest:testMultipleBorrowsForMultipleAssets() (gas: 2463626652) -MarketTest:testMultipleDepositsToSmartPool() (gas: 915551) -MarketTest:testMultipleFixedBorrowsRepays() (gas: 1348398) -MarketTest:testMultipleLiquidationSameUser() (gas: 2958727) -MarketTest:testNotEnteredMarketShouldNotBeSeized() (gas: 8605044) -MarketTest:testOnlyAdminCanFreezeUnfreeze() (gas: 207681) +MarketTest:testInsufficientProtocolLiquidity() (gas: 1940418) +MarketTest:testLiquidateAndChargeIncentiveForLenders() (gas: 2433524) +MarketTest:testLiquidateAndDistributeLosses() (gas: 3243058) +MarketTest:testLiquidateAndSeizeExactAmountWithDustAsCollateral() (gas: 2808630) +MarketTest:testLiquidateAndSeizeFromEmptyCollateral() (gas: 1043463) +MarketTest:testLiquidateAndSubtractLossesFromAccumulator() (gas: 3916470) +MarketTest:testLiquidateFlexibleAndFixedBorrowPositionsInSingleCall() (gas: 2638544) +MarketTest:testLiquidateFlexibleBorrow() (gas: 2160641) +MarketTest:testLiquidateFlexibleBorrowChargeLendersAssetsToLiquidator() (gas: 1152387) +MarketTest:testLiquidateFlexibleBorrowConsideringDebtOverTime() (gas: 1167683) +MarketTest:testLiquidateLeavingDustAsCollateral() (gas: 3629548) +MarketTest:testLiquidateTransferRepayAssetsBeforeSeize() (gas: 1045359) +MarketTest:testLiquidateUpdateFloatingDebt() (gas: 1903239) +MarketTest:testLiquidateWhenFrozen() (gas: 1232974) +MarketTest:testLiquidateWithTwoUnitsAsMaxAssets() (gas: 1579206) +MarketTest:testLiquidateWithZeroAsMaxAssets() (gas: 1043412) +MarketTest:testLiquidationClearingDebtOfAllAccountMarkets() (gas: 3107394) +MarketTest:testLiquidationResultingInZeroCollateralAndZeroDebt() (gas: 1983333) +MarketTest:testMaturityInsufficientProtocolLiquidity() (gas: 1464114) +MarketTest:testMultipleBorrowsForMultipleAssets() (gas: 2463651203) +MarketTest:testMultipleDepositsToSmartPool() (gas: 915573) +MarketTest:testMultipleFixedBorrowsRepays() (gas: 1348420) +MarketTest:testMultipleLiquidationSameUser() (gas: 2952389) +MarketTest:testNotEnteredMarketShouldNotBeSeized() (gas: 8617648) +MarketTest:testOnlyAdminCanFreezeUnfreeze() (gas: 207703) MarketTest:testOperationsShouldUpdateFloatingAssetsAverage() (gas: 1445531) -MarketTest:testOperationsWithBtcWbtcRate() (gas: 8306487) -MarketTest:testOperationsWithStEthAsset() (gas: 8182385) -MarketTest:testPausable() (gas: 158114) -MarketTest:testPauserRole() (gas: 78206) -MarketTest:testPreviewOperationsWithSmartPoolCorrectlyAccountingEarnings() (gas: 1976295) +MarketTest:testOperationsWithBtcWbtcRate() (gas: 8312565) +MarketTest:testOperationsWithStEthAsset() (gas: 8188573) +MarketTest:testPausable() (gas: 158136) +MarketTest:testPauserRole() (gas: 78228) +MarketTest:testPreviewOperationsWithSmartPoolCorrectlyAccountingEarnings() (gas: 1976317) MarketTest:testRepayAtMaturity() (gas: 586716) MarketTest:testRepayDisagreement() (gas: 594037) -MarketTest:testRepayFlexibleBorrow() (gas: 1076329) -MarketTest:testRepayWhenFrozen() (gas: 535304) -MarketTest:testRoundingDownAssetsToValidateShortfallWhenTransferring() (gas: 7565414) -MarketTest:testRoundingDownAssetsToValidateShortfallWhenTransferringFrom() (gas: 7620743) -MarketTest:testRoundingDownAssetsWhenTransferingFromAnAccountWithoutShortfall() (gas: 1030161) -MarketTest:testRoundingDownAssetsWhenTransferingWithAnAccountWithoutShortfall() (gas: 974816) -MarketTest:testRoundingUpAllowanceWhenBorrowingAtMaturity() (gas: 845669) -MarketTest:testRoundingUpAllowanceWhenWithdrawingAtMaturity() (gas: 791710) -MarketTest:testSetAssetSymbol() (gas: 54991) +MarketTest:testRepayFlexibleBorrow() (gas: 1076351) +MarketTest:testRepayWhenFrozen() (gas: 535326) +MarketTest:testRoundingDownAssetsToValidateShortfallWhenTransferring() (gas: 7571697) +MarketTest:testRoundingDownAssetsToValidateShortfallWhenTransferringFrom() (gas: 7626916) +MarketTest:testRoundingDownAssetsWhenTransferingFromAnAccountWithoutShortfall() (gas: 1030183) +MarketTest:testRoundingDownAssetsWhenTransferingWithAnAccountWithoutShortfall() (gas: 974749) +MarketTest:testRoundingUpAllowanceWhenBorrowingAtMaturity() (gas: 845691) +MarketTest:testRoundingUpAllowanceWhenWithdrawingAtMaturity() (gas: 791732) +MarketTest:testSetAssetSymbol() (gas: 55013) MarketTest:testSetAssetSymbolNotAdmin() (gas: 66288) MarketTest:testSetDampSpeedFactorShouldUpdateFloatingAssetsAverage() (gas: 386872) -MarketTest:testSetEarningsAccumulatorSmoothFactorShouldDistributeEarnings() (gas: 722410) +MarketTest:testSetEarningsAccumulatorSmoothFactorShouldDistributeEarnings() (gas: 722432) MarketTest:testSetInterestRateModelShouldUpdateFloatingDebt() (gas: 2821264) -MarketTest:testSetInterestRateModelWithAddressZeroShouldNotUpdateFloatingDebt() (gas: 2582220) -MarketTest:testShareValueNotDecreasingAfterDeposit() (gas: 707842) -MarketTest:testShareValueNotDecreasingWhenMintingToTreasury() (gas: 763178) -MarketTest:testSingleFloatingBorrow() (gas: 417405) -MarketTest:testSingleFloatingRepay() (gas: 484627) +MarketTest:testSetInterestRateModelWithAddressZeroShouldNotUpdateFloatingDebt() (gas: 2582242) +MarketTest:testShareValueNotDecreasingAfterDeposit() (gas: 707864) +MarketTest:testShareValueNotDecreasingWhenMintingToTreasury() (gas: 763200) +MarketTest:testSingleFloatingBorrow() (gas: 417427) +MarketTest:testSingleFloatingRepay() (gas: 484649) MarketTest:testSmartPoolEarningsDistribution() (gas: 1104567) MarketTest:testSmartPoolSharesDoNotAccountUnassignedEarningsFromMoreThanOneIntervalPastMaturities() (gas: 528549) -MarketTest:testSumDebtPlusEffectsShouldntRoundUpWhenWithdrawing() (gas: 1005563) -MarketTest:testTotalAssetsProjectingBackupEarningsCorrectly() (gas: 508258) -MarketTest:testTotalAssetsProjectingFloatingDebtCorrectly() (gas: 713915) +MarketTest:testSumDebtPlusEffectsShouldntRoundUpWhenWithdrawing() (gas: 1005585) +MarketTest:testTotalAssetsProjectingBackupEarningsCorrectly() (gas: 508280) +MarketTest:testTotalAssetsProjectingFloatingDebtCorrectly() (gas: 713937) MarketTest:testUpdateAccumulatedEarningsFactorToZero() (gas: 1482137) MarketTest:testUpdateFloatingAssetsAverageWhenDepositingAndBorrowingContinuously() (gas: 344727) -MarketTest:testUpdateFloatingAssetsAverageWhenDepositingRightBeforeBorrow() (gas: 670432) -MarketTest:testUpdateFloatingAssetsAverageWhenDepositingRightBeforeEarlyWithdraw() (gas: 630610) -MarketTest:testUpdateFloatingAssetsAverageWhenDepositingSomeSecondsBeforeBorrow() (gas: 901020) -MarketTest:testUpdateFloatingAssetsAverageWhenWithdrawingRightBeforeBorrow() (gas: 683563) -MarketTest:testUpdateFloatingAssetsAverageWhenWithdrawingRightBeforeEarlyWithdraw() (gas: 643760) +MarketTest:testUpdateFloatingAssetsAverageWhenDepositingRightBeforeBorrow() (gas: 670454) +MarketTest:testUpdateFloatingAssetsAverageWhenDepositingRightBeforeEarlyWithdraw() (gas: 630543) +MarketTest:testUpdateFloatingAssetsAverageWhenDepositingSomeSecondsBeforeBorrow() (gas: 901042) +MarketTest:testUpdateFloatingAssetsAverageWhenWithdrawingRightBeforeBorrow() (gas: 683496) +MarketTest:testUpdateFloatingAssetsAverageWhenWithdrawingRightBeforeEarlyWithdraw() (gas: 643673) MarketTest:testUpdateFloatingAssetsAverageWhenWithdrawingSomeSecondsBeforeBorrow() (gas: 373112) MarketTest:testUpdateFloatingAssetsAverageWithDampSpeedDown() (gas: 358048) -MarketTest:testUpdateFloatingAssetsAverageWithDampSpeedUp() (gas: 199763) -MarketTest:testUpdateFloatingDebtBeforeSettingTreasury() (gas: 106373) -MarketTest:testWithdrawAtMaturity() (gas: 304281) -MarketTest:testWithdrawFromSmartPool() (gas: 279177) +MarketTest:testUpdateFloatingAssetsAverageWithDampSpeedUp() (gas: 199785) +MarketTest:testUpdateFloatingDebtBeforeSettingTreasury() (gas: 106395) +MarketTest:testWithdrawAtMaturity() (gas: 304303) +MarketTest:testWithdrawFromSmartPool() (gas: 279199) MarketTest:testWithdrawShouldUpdateFlexibleBorrowVariables() (gas: 863981) -MarketTest:testWithdrawWhenFrozen() (gas: 325939) +MarketTest:testWithdrawWhenFrozen() (gas: 325961) PoolLibTest:testAtomicDepositBorrowRepayWithdraw() (gas: 46018) PoolLibTest:testBackupBorrow() (gas: 33676) PoolLibTest:testEarningsAccrual() (gas: 38999) @@ -292,24 +293,24 @@ PreviewerTest:testAccountsReturningUtilizationForDifferentMaturities() (gas: 418 PreviewerTest:testAccountsWithAccountOnlyDeposit() (gas: 862391) PreviewerTest:testAccountsWithAccountThatHasBalances() (gas: 2265104) PreviewerTest:testAccountsWithEmptyAccount() (gas: 690558) -PreviewerTest:testAccountsWithIntermediateOperationsReturningAccurateAmounts() (gas: 17668144) +PreviewerTest:testAccountsWithIntermediateOperationsReturningAccurateAmounts() (gas: 17674304) PreviewerTest:testActualTimeBeforeStartDistributionRewards() (gas: 7809786) PreviewerTest:testEmptyExactly() (gas: 5690096) PreviewerTest:testExactlyReturningInterestRateModelData() (gas: 688149) -PreviewerTest:testFixedAvailableLiquidityProjectingNewFloatingDebt() (gas: 13352198) -PreviewerTest:testFixedPoolsA() (gas: 19368943) +PreviewerTest:testFixedAvailableLiquidityProjectingNewFloatingDebt() (gas: 13358358) +PreviewerTest:testFixedPoolsA() (gas: 19375103) PreviewerTest:testFixedPoolsChangingMaturityInTime() (gas: 1627253) -PreviewerTest:testFixedPoolsRatesAndUtilizations() (gas: 14865686) -PreviewerTest:testFixedPoolsWithFloatingAssetsAverage() (gas: 15688871) -PreviewerTest:testFlexibleAvailableLiquidity() (gas: 17293000) +PreviewerTest:testFixedPoolsRatesAndUtilizations() (gas: 14871846) +PreviewerTest:testFixedPoolsWithFloatingAssetsAverage() (gas: 15695031) +PreviewerTest:testFlexibleAvailableLiquidity() (gas: 17299160) PreviewerTest:testFlexibleBorrowSharesAndAssets() (gas: 4401038) -PreviewerTest:testFloatingAvailableLiquidityProjectingNewFloatingDebt() (gas: 12604858) +PreviewerTest:testFloatingAvailableLiquidityProjectingNewFloatingDebt() (gas: 12611018) PreviewerTest:testFloatingRateAndUtilization() (gas: 1128246) PreviewerTest:testJustUpdatedRewardRatesShouldStillReturnRate() (gas: 7219492) PreviewerTest:testMaxBorrowAssetsCapacity() (gas: 2469700) -PreviewerTest:testMaxBorrowAssetsCapacityForAccountWithShortfall() (gas: 11042383) -PreviewerTest:testMaxBorrowAssetsCapacityPerMarket() (gas: 13232502) -PreviewerTest:testOraclePriceReturningAccurateValues() (gas: 10184223) +PreviewerTest:testMaxBorrowAssetsCapacityForAccountWithShortfall() (gas: 11048549) +PreviewerTest:testMaxBorrowAssetsCapacityPerMarket() (gas: 13238662) +PreviewerTest:testOraclePriceReturningAccurateValues() (gas: 10190389) PreviewerTest:testPreviewBorrowAtAllMaturitiesReturningAccurateAmount() (gas: 4242626) PreviewerTest:testPreviewBorrowAtMaturityReturningAccurateAmount() (gas: 623428) PreviewerTest:testPreviewBorrowAtMaturityReturningAccurateAmountWithIntermediateOperations() (gas: 1954610) @@ -355,8 +356,8 @@ PreviewerTest:testReserveFactor() (gas: 707280) PreviewerTest:testReturnRewardAssetUsdPrice() (gas: 6753659) PreviewerTest:testRewardsRateAfterDistributionEnd() (gas: 7525793) PreviewerTest:testRewardsRateOnlyWithFixedBorrows() (gas: 6832985) -PreviewerTest:testRewardsRateWithDifferentRewardLengths() (gas: 19347934) -PreviewerTest:testRewardsRateWithMarketWithDifferentDecimals() (gas: 18479258) +PreviewerTest:testRewardsRateWithDifferentRewardLengths() (gas: 19354094) +PreviewerTest:testRewardsRateWithMarketWithDifferentDecimals() (gas: 18485418) PreviewerTest:testRewardsRateX() (gas: 8172438) PriceFeedDoubleTest:testPriceFeedDoubleReturningAccurateDecimals() (gas: 632562) PriceFeedDoubleTest:testPriceFeedDoubleReturningPrice() (gas: 53190) @@ -375,10 +376,10 @@ PriceFeedWrapperTest:testPriceFeedWrapperWithActualOnChainValues() (gas: 75210) PriceFeedWrapperTest:testPriceFeedWrapperWithNegativePriceShouldRevert() (gas: 164216) PriceFeedWrapperTest:testPriceFeedWrapperWithUsdPriceFeed() (gas: 1278197) RewardsControllerTest:testAccrueRewardsForWholeDistributionPeriod() (gas: 1245322) -RewardsControllerTest:testAccrueRewardsWithBadDebtClearingOfFixedBorrow() (gas: 3338784) -RewardsControllerTest:testAccrueRewardsWithRepayOfBorrowBalance() (gas: 1602615) -RewardsControllerTest:testAccrueRewardsWithRepayOfFixedBorrowBalance() (gas: 1791684) -RewardsControllerTest:testAccrueRewardsWithSeizeOfAllDepositShares() (gas: 1991114) +RewardsControllerTest:testAccrueRewardsWithBadDebtClearingOfFixedBorrow() (gas: 3339446) +RewardsControllerTest:testAccrueRewardsWithRepayOfBorrowBalance() (gas: 1603277) +RewardsControllerTest:testAccrueRewardsWithRepayOfFixedBorrowBalance() (gas: 1792346) +RewardsControllerTest:testAccrueRewardsWithSeizeOfAllDepositShares() (gas: 1991776) RewardsControllerTest:testAfterDistributionPeriodEnd() (gas: 1818322) RewardsControllerTest:testAllClaimableUSDCWithAnotherAccountInPool() (gas: 2274252) RewardsControllerTest:testAllClaimableUSDCWithDeposit() (gas: 1628756) @@ -410,7 +411,7 @@ RewardsControllerTest:testLastUndistributed() (gas: 2189926) RewardsControllerTest:testOperationAfterDistributionEnded() (gas: 723020) RewardsControllerTest:testOperationsBeforeDistributionStart() (gas: 1674553) RewardsControllerTest:testPermitClaim() (gas: 1275326) -RewardsControllerTest:testSetDistributionConfigWithDifferentDecimals() (gas: 11507183) +RewardsControllerTest:testSetDistributionConfigWithDifferentDecimals() (gas: 11513349) RewardsControllerTest:testSetDistributionOperationShouldUpdateIndex() (gas: 136066) RewardsControllerTest:testSetDistributionWithOnGoingMarketOperations() (gas: 1202335) RewardsControllerTest:testSetHigherTotalDistribution() (gas: 1831337) diff --git a/contracts/Market.sol b/contracts/Market.sol index b22d5e38..0725d94b 100644 --- a/contracts/Market.sol +++ b/contracts/Market.sol @@ -555,6 +555,7 @@ contract Market is Initializable, AccessControlUpgradeable, PausableUpgradeable, Market seizeMarket ) external whenNotPaused returns (uint256 repaidAssets) { if (msg.sender == borrower) revert SelfLiquidation(); + floatingAssets += accrueAccumulatedEarnings(); maxAssets = auditor.checkLiquidation(this, seizeMarket, borrower, maxAssets); if (maxAssets == 0) revert ZeroRepay(); diff --git a/test/Market.t.sol b/test/Market.t.sol index be940476..6c1d60a7 100644 --- a/test/Market.t.sol +++ b/test/Market.t.sol @@ -931,6 +931,44 @@ contract MarketTest is Test { assertEq(market.lastFloatingDebtUpdate(), 4 days); } + function testAccrueEarningsBeforeLiquidation() external { + uint256 maturity = FixedLib.INTERVAL * 2; + uint256 assets = 10_000 ether; + + // BOB adds liquidity for liquidation + vm.prank(BOB); + market.deposit(assets, BOB); + + // ALICE deposits and borrows + ERC20 asset = market.asset(); + deal(address(asset), ALICE, assets); + vm.startPrank(ALICE); + market.deposit(assets, ALICE); + market.borrowAtMaturity(maturity, (assets * 78 * 78) / 100 / 100, type(uint256).max, ALICE, ALICE); + vm.stopPrank(); + + // Maturity is over and some time has passed, accruing extra debt fees + skip(maturity + (FixedLib.INTERVAL * 90) / 100); + + // ALICE has a health factor below 1 and should be liquidated and end up with 0 assets + (uint256 collateral, uint256 debt) = market.accountSnapshot(address(ALICE)); + assertEq(collateral, 10046671780821917806594); // 10046e18 + assertEq(debt, 9290724716705852929432); // 9290e18 + + // Liquidator liquidates + address liquidator = makeAddr("liquidator"); + deal(address(asset), liquidator, assets); + vm.startPrank(liquidator); + asset.approve(address(market), type(uint256).max); + market.liquidate(ALICE, type(uint256).max, market); + vm.stopPrank(); + + (collateral, debt) = market.accountSnapshot(address(ALICE)); + + assertEq(collateral, 0); + assertEq(debt, 0); + } + function testLiquidateUpdateFloatingDebt() external { irm.setRate(0); marketWETH.deposit(1.15 ether, address(this)); @@ -1637,6 +1675,7 @@ contract MarketTest is Test { market.repayAtMaturity(FixedLib.INTERVAL, principal + fee, debt, ALICE); uint256 earningsAccumulatorBefore = market.earningsAccumulator(); uint256 lendersIncentive = 1181818181818181800; + uint256 distributedEarnings = 680440721299864513; uint256 badDebt = 981818181818181818100 + 1100000000000000000000 + 1100000000000000000000 + 1100000000000000000000; uint256 untrackedUnassignedEarnings = 300000000000000000000; uint256 earlyRepayEarnings = 3069658128703695345; @@ -1660,7 +1699,8 @@ contract MarketTest is Test { accumulatedEarnings + earlyRepayEarnings + lendersIncentive + - untrackedUnassignedEarnings, + untrackedUnassignedEarnings + + distributedEarnings, 1e2 ); (, uint256 fixedBorrows, ) = market.accounts(address(this));