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 e58b2bc9..a9f34adf 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -143,143 +143,144 @@ InterestRateModelTest:testFixedRateRevertUtilizationExceeded() (gas: 2053399) InterestRateModelTest:testFloatingBorrowRate() (gas: 2045540) InterestRateModelTest:testFuzzFixedRateGrowth(uint256,uint256,uint256,uint256) (runs: 256, μ: 2067148, ~: 2063904) InterestRateModelTest:testFuzzFixedRateTimeSensitivity(uint256,uint256,uint256) (runs: 256, μ: 2073165, ~: 2073210) -InterestRateModelTest:testFuzzReferenceLegacyRateFixed(uint32,uint256,uint256[2],uint256[2],uint256,uint256,uint256) (runs: 256, μ: 9991193, ~: 10161825) +InterestRateModelTest:testFuzzReferenceLegacyRateFixed(uint32,uint256,uint256[2],uint256[2],uint256,uint256,uint256) (runs: 256, μ: 9997383, ~: 10168015) InterestRateModelTest:testFuzzReferenceRateFixed(uint256,uint256,uint256,uint256,uint256,uint256,(uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,int256,uint256,uint256)) (runs: 256, μ: 2337320, ~: 2339917) InterestRateModelTest:testFuzzReferenceRateFloating(uint256,uint256,(uint256,uint256,uint256,uint256,uint256,uint256,uint256)) (runs: 256, μ: 2275592, ~: 2276418) InterestRateModelTest:testMinTimeToMaturity() (gas: 2063554) InterestRateModelTest:testRevertMaxUtilizationLowerThanWad() (gas: 266682) -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: 1576605) +MarketTest:testChargeTreasuryToFixedBorrows() (gas: 1576518) MarketTest:testClearBadDebtAvoidingFixedBorrowsIfAccumulatorLower() (gas: 2277523) -MarketTest:testClearBadDebtBeforeMaturity() (gas: 2347581) -MarketTest:testClearBadDebtCalledByAccount() (gas: 35021) -MarketTest:testClearBadDebtExactlyRepaysFixedBorrowWithAccumulatorAmount() (gas: 2008589) -MarketTest:testClearBadDebtPartiallyRepaysEachFixedBorrow() (gas: 1955967) +MarketTest:testClearBadDebtBeforeMaturity() (gas: 2345465) +MarketTest:testClearBadDebtCalledByAccount() (gas: 35043) +MarketTest:testClearBadDebtExactlyRepaysFixedBorrowWithAccumulatorAmount() (gas: 2008522) +MarketTest:testClearBadDebtPartiallyRepaysEachFixedBorrow() (gas: 1955989) MarketTest:testClearBadDebtPartiallyRepaysFloatingDebt() (gas: 2295777) -MarketTest:testClearBadDebtShouldAccrueAccumulatedEarningsBeforeSpreadingLosses() (gas: 2075406) -MarketTest:testClearBadDebtWithEmptyAccumulatorShouldNotRevert() (gas: 969888) -MarketTest:testClearMaturity() (gas: 1681983) -MarketTest:testCollectTreasuryFreeLunchToEarlyWithdraws() (gas: 1766321) -MarketTest:testCollectTreasuryFreeLunchToEarlyWithdrawsWithZeroFees() (gas: 559337) -MarketTest:testCollectTreasuryFreeLunchToFixedBorrows() (gas: 1808873) -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: 2075428) +MarketTest:testClearBadDebtWithEmptyAccumulatorShouldNotRevert() (gas: 969910) +MarketTest:testClearMaturity() (gas: 1682005) +MarketTest:testCollectTreasuryFreeLunchToEarlyWithdraws() (gas: 1766343) +MarketTest:testCollectTreasuryFreeLunchToEarlyWithdrawsWithZeroFees() (gas: 559359) +MarketTest:testCollectTreasuryFreeLunchToFixedBorrows() (gas: 1808895) +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: 1371090) -MarketTest:testDistributionOfLossesShouldReduceFromFloatingBackupBorrowedAccordingly() (gas: 6404016) -MarketTest:testEarlyRepayLiquidationUnassignedEarnings() (gas: 2116976) -MarketTest:testEarlyRepaymentWithExcessiveAmountOfFees() (gas: 3423717) -MarketTest:testEarlyWithdrawFromFreeLunchShouldNotRevertWithFloatingFullUtilization() (gas: 1050081) +MarketTest:testDistributeMultipleAccumulatedEarnings() (gas: 1371112) +MarketTest:testDistributionOfLossesShouldReduceFromFloatingBackupBorrowedAccordingly() (gas: 6408432) +MarketTest:testEarlyRepayLiquidationUnassignedEarnings() (gas: 2116887) +MarketTest:testEarlyRepaymentWithExcessiveAmountOfFees() (gas: 3423650) +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: 1370369) -MarketTest:testFullPause() (gas: 5956376) +MarketTest:testFlexibleBorrowFromAnotherUserWithAllowance() (gas: 458681) +MarketTest:testFlexibleBorrowFromAnotherUserWithoutAllowance() (gas: 252381) +MarketTest:testFrontRunSmartPoolEarningsDistributionWithBigPenaltyRepayment() (gas: 1370302) +MarketTest:testFullPause() (gas: 5954954) MarketTest:testInitiallyUnfrozen() (gas: 15592) -MarketTest:testInsufficientProtocolLiquidity() (gas: 1940396) -MarketTest:testLiquidateAndChargeIncentiveForLenders() (gas: 2431316) -MarketTest:testLiquidateAndDistributeLosses() (gas: 3229184) -MarketTest:testLiquidateAndSeizeExactAmountWithDustAsCollateral() (gas: 2796274) -MarketTest:testLiquidateAndSeizeFromEmptyCollateral() (gas: 1039341) -MarketTest:testLiquidateAndSubtractLossesFromAccumulator() (gas: 3904098) -MarketTest:testLiquidateFlexibleAndFixedBorrowPositionsInSingleCall() (gas: 2631898) -MarketTest:testLiquidateFlexibleBorrow() (gas: 2156292) -MarketTest:testLiquidateFlexibleBorrowChargeLendersAssetsToLiquidator() (gas: 1150267) -MarketTest:testLiquidateFlexibleBorrowConsideringDebtOverTime() (gas: 1166248) -MarketTest:testLiquidateLeavingDustAsCollateral() (gas: 3623128) -MarketTest:testLiquidateTransferRepayAssetsBeforeSeize() (gas: 1045448) -MarketTest:testLiquidateUpdateFloatingDebt() (gas: 1901804) -MarketTest:testLiquidateWhenFrozen() (gas: 1230766) -MarketTest:testLiquidateWithTwoUnitsAsMaxAssets() (gas: 1576998) -MarketTest:testLiquidateWithZeroAsMaxAssets() (gas: 1039182) -MarketTest:testLiquidationClearingDebtOfAllAccountMarkets() (gas: 3093492) -MarketTest:testLiquidationResultingInZeroCollateralAndZeroDebt() (gas: 1969452) -MarketTest:testMaturityInsufficientProtocolLiquidity() (gas: 1464202) -MarketTest:testMultipleBorrowsForMultipleAssets() (gas: 2463488384) -MarketTest:testMultipleDepositsToSmartPool() (gas: 915551) -MarketTest:testMultipleFixedBorrowsRepays() (gas: 1348398) -MarketTest:testMultipleLiquidationSameUser() (gas: 2947061) -MarketTest:testNotEnteredMarketShouldNotBeSeized() (gas: 8558798) -MarketTest:testOnlyAdminCanFreezeUnfreeze() (gas: 207681) +MarketTest:testInsufficientProtocolLiquidity() (gas: 1940418) +MarketTest:testLiquidateAndChargeIncentiveForLenders() (gas: 2433524) +MarketTest:testLiquidateAndDistributeLosses() (gas: 3231392) +MarketTest:testLiquidateAndSeizeExactAmountWithDustAsCollateral() (gas: 2796958) +MarketTest:testLiquidateAndSeizeFromEmptyCollateral() (gas: 1043463) +MarketTest:testLiquidateAndSubtractLossesFromAccumulator() (gas: 3904804) +MarketTest:testLiquidateFlexibleAndFixedBorrowPositionsInSingleCall() (gas: 2638544) +MarketTest:testLiquidateFlexibleBorrow() (gas: 2160641) +MarketTest:testLiquidateFlexibleBorrowChargeLendersAssetsToLiquidator() (gas: 1152387) +MarketTest:testLiquidateFlexibleBorrowConsideringDebtOverTime() (gas: 1167683) +MarketTest:testLiquidateLeavingDustAsCollateral() (gas: 3617882) +MarketTest:testLiquidateTransferRepayAssetsBeforeSeize() (gas: 1045359) +MarketTest:testLiquidateUpdateFloatingDebt() (gas: 1903239) +MarketTest:testLiquidateWhenFrozen() (gas: 1232974) +MarketTest:testLiquidateWithTwoUnitsAsMaxAssets() (gas: 1579206) +MarketTest:testLiquidateWithZeroAsMaxAssets() (gas: 1043412) +MarketTest:testLiquidationClearingDebtOfAllAccountMarkets() (gas: 3095722) +MarketTest:testLiquidationResultingInZeroCollateralAndZeroDebt() (gas: 1971660) +MarketTest:testMaturityInsufficientProtocolLiquidity() (gas: 1464114) +MarketTest:testMultipleBorrowsForMultipleAssets() (gas: 2463513031) +MarketTest:testMultipleDepositsToSmartPool() (gas: 915573) +MarketTest:testMultipleFixedBorrowsRepays() (gas: 1348420) +MarketTest:testMultipleLiquidationSameUser() (gas: 2940723) +MarketTest:testNotEnteredMarketShouldNotBeSeized() (gas: 8571427) +MarketTest:testOnlyAdminCanFreezeUnfreeze() (gas: 207703) MarketTest:testOperationsShouldUpdateFloatingAssetsAverage() (gas: 1445531) -MarketTest:testOperationsWithBtcWbtcRate() (gas: 8236915) -MarketTest:testOperationsWithStEthAsset() (gas: 8124257) -MarketTest:testPausable() (gas: 158114) -MarketTest:testPauserRole() (gas: 78206) -MarketTest:testPreviewOperationsWithSmartPoolCorrectlyAccountingEarnings() (gas: 1976295) +MarketTest:testOperationsWithBtcWbtcRate() (gas: 8243018) +MarketTest:testOperationsWithStEthAsset() (gas: 8130470) +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: 7542494) -MarketTest:testRoundingDownAssetsToValidateShortfallWhenTransferringFrom() (gas: 7597823) -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: 7548790) +MarketTest:testRoundingDownAssetsToValidateShortfallWhenTransferringFrom() (gas: 7604009) +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: 2797934) -MarketTest:testSetInterestRateModelWithAddressZeroShouldNotUpdateFloatingDebt() (gas: 2570550) -MarketTest:testShareValueNotDecreasingAfterDeposit() (gas: 707842) -MarketTest:testShareValueNotDecreasingWhenMintingToTreasury() (gas: 763178) -MarketTest:testSingleFloatingBorrow() (gas: 417405) -MarketTest:testSingleFloatingRepay() (gas: 484627) +MarketTest:testSetInterestRateModelWithAddressZeroShouldNotUpdateFloatingDebt() (gas: 2570572) +MarketTest:testShareValueNotDecreasingAfterDeposit() (gas: 707864) +MarketTest:testShareValueNotDecreasingWhenMintingToTreasury() (gas: 763200) +MarketTest:testSingleFloatingBorrow() (gas: 417427) +MarketTest:testSingleFloatingRepay() (gas: 484649) MarketTest:testSmartPoolEarningsDistribution() (gas: 1092894) 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: 1470471) 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: 17621916) +PreviewerTest:testAccountsWithIntermediateOperationsReturningAccurateAmounts() (gas: 17628100) PreviewerTest:testActualTimeBeforeStartDistributionRewards() (gas: 7759515) PreviewerTest:testEmptyExactly() (gas: 5651562) PreviewerTest:testExactlyReturningInterestRateModelData() (gas: 688149) -PreviewerTest:testFixedAvailableLiquidityProjectingNewFloatingDebt() (gas: 13305970) -PreviewerTest:testFixedPoolsA() (gas: 19322715) +PreviewerTest:testFixedAvailableLiquidityProjectingNewFloatingDebt() (gas: 13312154) +PreviewerTest:testFixedPoolsA() (gas: 19328899) PreviewerTest:testFixedPoolsChangingMaturityInTime() (gas: 1627253) -PreviewerTest:testFixedPoolsRatesAndUtilizations() (gas: 14819458) -PreviewerTest:testFixedPoolsWithFloatingAssetsAverage() (gas: 15642643) -PreviewerTest:testFlexibleAvailableLiquidity() (gas: 17246772) +PreviewerTest:testFixedPoolsRatesAndUtilizations() (gas: 14825642) +PreviewerTest:testFixedPoolsWithFloatingAssetsAverage() (gas: 15648827) +PreviewerTest:testFlexibleAvailableLiquidity() (gas: 17252956) PreviewerTest:testFlexibleBorrowSharesAndAssets() (gas: 4401038) -PreviewerTest:testFloatingAvailableLiquidityProjectingNewFloatingDebt() (gas: 12558630) +PreviewerTest:testFloatingAvailableLiquidityProjectingNewFloatingDebt() (gas: 12564814) PreviewerTest:testFloatingRateAndUtilization() (gas: 1128246) PreviewerTest:testJustUpdatedRewardRatesShouldStillReturnRate() (gas: 7180914) PreviewerTest:testMaxBorrowAssetsCapacity() (gas: 2469700) -PreviewerTest:testMaxBorrowAssetsCapacityForAccountWithShortfall() (gas: 10996142) -PreviewerTest:testMaxBorrowAssetsCapacityPerMarket() (gas: 13186274) -PreviewerTest:testOraclePriceReturningAccurateValues() (gas: 10137982) +PreviewerTest:testMaxBorrowAssetsCapacityForAccountWithShortfall() (gas: 11002333) +PreviewerTest:testMaxBorrowAssetsCapacityPerMarket() (gas: 13192458) +PreviewerTest:testOraclePriceReturningAccurateValues() (gas: 10144173) PreviewerTest:testPreviewBorrowAtAllMaturitiesReturningAccurateAmount() (gas: 4242626) PreviewerTest:testPreviewBorrowAtMaturityReturningAccurateAmount() (gas: 623428) PreviewerTest:testPreviewBorrowAtMaturityReturningAccurateAmountWithIntermediateOperations() (gas: 1954610) @@ -355,8 +356,8 @@ PreviewerTest:testReserveFactor() (gas: 707280) PreviewerTest:testReturnRewardAssetUsdPrice() (gas: 6703410) PreviewerTest:testRewardsRateAfterDistributionEnd() (gas: 7487215) PreviewerTest:testRewardsRateOnlyWithFixedBorrows() (gas: 6794429) -PreviewerTest:testRewardsRateWithDifferentRewardLengths() (gas: 19251652) -PreviewerTest:testRewardsRateWithMarketWithDifferentDecimals() (gas: 18382715) +PreviewerTest:testRewardsRateWithDifferentRewardLengths() (gas: 19257836) +PreviewerTest:testRewardsRateWithMarketWithDifferentDecimals() (gas: 18388899) PreviewerTest:testRewardsRateX() (gas: 8133789) PriceFeedDoubleTest:testPriceFeedDoubleReturningAccurateDecimals() (gas: 597567) PriceFeedDoubleTest:testPriceFeedDoubleReturningPrice() (gas: 53190) @@ -375,10 +376,10 @@ PriceFeedWrapperTest:testPriceFeedWrapperWithActualOnChainValues() (gas: 75210) PriceFeedWrapperTest:testPriceFeedWrapperWithNegativePriceShouldRevert() (gas: 164216) PriceFeedWrapperTest:testPriceFeedWrapperWithUsdPriceFeed() (gas: 1243191) RewardsControllerTest:testAccrueRewardsForWholeDistributionPeriod() (gas: 1245252) -RewardsControllerTest:testAccrueRewardsWithBadDebtClearingOfFixedBorrow() (gas: 3338696) -RewardsControllerTest:testAccrueRewardsWithRepayOfBorrowBalance() (gas: 1602527) -RewardsControllerTest:testAccrueRewardsWithRepayOfFixedBorrowBalance() (gas: 1791640) -RewardsControllerTest:testAccrueRewardsWithSeizeOfAllDepositShares() (gas: 1991092) +RewardsControllerTest:testAccrueRewardsWithBadDebtClearingOfFixedBorrow() (gas: 3339358) +RewardsControllerTest:testAccrueRewardsWithRepayOfBorrowBalance() (gas: 1603189) +RewardsControllerTest:testAccrueRewardsWithRepayOfFixedBorrowBalance() (gas: 1792302) +RewardsControllerTest:testAccrueRewardsWithSeizeOfAllDepositShares() (gas: 1991754) RewardsControllerTest:testAfterDistributionPeriodEnd() (gas: 1818275) RewardsControllerTest:testAllClaimableUSDCWithAnotherAccountInPool() (gas: 2273354) RewardsControllerTest:testAllClaimableUSDCWithDeposit() (gas: 1627706) @@ -411,7 +412,7 @@ RewardsControllerTest:testLastUndistributed() (gas: 2189649) RewardsControllerTest:testOperationAfterDistributionEnded() (gas: 722976) RewardsControllerTest:testOperationsBeforeDistributionStart() (gas: 1674572) RewardsControllerTest:testPermitClaim() (gas: 1275278) -RewardsControllerTest:testSetDistributionConfigWithDifferentDecimals() (gas: 11449351) +RewardsControllerTest:testSetDistributionConfigWithDifferentDecimals() (gas: 11455542) RewardsControllerTest:testSetDistributionOperationShouldUpdateIndex() (gas: 136200) RewardsControllerTest:testSetDistributionWithOnGoingMarketOperations() (gas: 1202354) RewardsControllerTest:testSetHigherTotalDistribution() (gas: 1831286) 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));