diff --git a/.changeset/famous-mangos-sort.md b/.changeset/famous-mangos-sort.md new file mode 100644 index 00000000..7fd41b3e --- /dev/null +++ b/.changeset/famous-mangos-sort.md @@ -0,0 +1,5 @@ +--- +"@exactly/protocol": patch +--- + +🐛 market: redirect earnings to accumulator in clear bad debt diff --git a/.gas-snapshot b/.gas-snapshot index d591fe0a..991c553b 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -143,64 +143,65 @@ InterestRateModelTest:testFixedRateRevertUtilizationExceeded() (gas: 2056183) InterestRateModelTest:testFloatingBorrowRate() (gas: 2048324) InterestRateModelTest:testFuzzFixedRateGrowth(uint256,uint256,uint256,uint256) (runs: 256, μ: 2069159, ~: 2066427) InterestRateModelTest:testFuzzFixedRateTimeSensitivity(uint256,uint256,uint256) (runs: 256, μ: 2075956, ~: 2075994) -InterestRateModelTest:testFuzzReferenceLegacyRateFixed(uint32,uint256,uint256[2],uint256[2],uint256,uint256,uint256) (runs: 256, μ: 10060739, ~: 10181646) +InterestRateModelTest:testFuzzReferenceLegacyRateFixed(uint32,uint256,uint256[2],uint256[2],uint256,uint256,uint256) (runs: 256, μ: 10068414, ~: 10189321) InterestRateModelTest:testFuzzReferenceRateFixed(uint256,uint256,uint256,uint256,uint256,uint256,(uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,int256,uint256,uint256)) (runs: 256, μ: 2340184, ~: 2342095) InterestRateModelTest:testFuzzReferenceRateFloating(uint256,uint256,(uint256,uint256,uint256,uint256,uint256,uint256,uint256)) (runs: 256, μ: 2278308, ~: 2279144) InterestRateModelTest:testMinTimeToMaturity() (gas: 2066338) InterestRateModelTest:testRevertMaxUtilizationLowerThanWad() (gas: 266877) MarketTest:testAccountLiquidityAdjustedDebt() (gas: 499501) -MarketTest:testAccrueEarningsBeforeLiquidation() (gas: 1252277) +MarketTest:testAccrueEarningsBeforeLiquidation() (gas: 1253181) MarketTest:testAnotherUserRedeemWhenOwnerHasShortfall() (gas: 819136) -MarketTest:testAnotherUserWithdrawWhenOwnerHasShortfall() (gas: 806873) +MarketTest:testAnotherUserWithdrawWhenOwnerHasShortfall() (gas: 806806) MarketTest:testBorrowAfterFreezing() (gas: 491397) MarketTest:testBorrowAtMaturity() (gas: 499605) MarketTest:testBorrowAtMaturityAfterFreezing() (gas: 573221) -MarketTest:testBorrowAtMaturityUpdatesFloatingDebtAndFloatingAssets() (gas: 884694) +MarketTest:testBorrowAtMaturityUpdatesFloatingDebtAndFloatingAssets() (gas: 884716) MarketTest:testBorrowAtMaturityWhenFrozen() (gas: 93718) MarketTest:testBorrowAtMaturityWithZeroAssets() (gas: 40403) MarketTest:testBorrowDisagreement() (gas: 295476) -MarketTest:testBorrowFromFreeLunchShouldNotRevertWithFloatingFullUtilization() (gas: 1294353) -MarketTest:testBorrowWhenFrozen() (gas: 93224) -MarketTest:testBorrowWithZeroAssets() (gas: 39749) -MarketTest:testCappedLiquidation() (gas: 1199762) +MarketTest:testBorrowFromFreeLunchShouldNotRevertWithFloatingFullUtilization() (gas: 1294375) +MarketTest:testBorrowWhenFrozen() (gas: 93157) +MarketTest:testBorrowWithZeroAssets() (gas: 39771) +MarketTest:testCappedLiquidation() (gas: 1199751) MarketTest:testChargeTreasuryToEarlyWithdraws() (gas: 1218557) MarketTest:testChargeTreasuryToFixedBorrows() (gas: 1579309) -MarketTest:testClearBadDebtAvoidingFixedBorrowsIfAccumulatorLower() (gas: 2280314) -MarketTest:testClearBadDebtBeforeMaturity() (gas: 2345465) +MarketTest:testClearBadDebtAvoidingFixedBorrowsIfAccumulatorLower() (gas: 2280336) +MarketTest:testClearBadDebtBeforeMaturity() (gas: 2345704) MarketTest:testClearBadDebtCalledByAccount() (gas: 35043) -MarketTest:testClearBadDebtExactlyRepaysFixedBorrowWithAccumulatorAmount() (gas: 2011313) -MarketTest:testClearBadDebtPartiallyRepaysEachFixedBorrow() (gas: 1958780) -MarketTest:testClearBadDebtPartiallyRepaysFloatingDebt() (gas: 2298568) +MarketTest:testClearBadDebtEmptiesUnassignedEarnings() (gas: 2497931) +MarketTest:testClearBadDebtExactlyRepaysFixedBorrowWithAccumulatorAmount() (gas: 2011033) +MarketTest:testClearBadDebtPartiallyRepaysEachFixedBorrow() (gas: 1959019) +MarketTest:testClearBadDebtPartiallyRepaysFloatingDebt() (gas: 2298807) MarketTest:testClearBadDebtShouldAccrueAccumulatedEarningsBeforeSpreadingLosses() (gas: 2078219) MarketTest:testClearBadDebtWithEmptyAccumulatorShouldNotRevert() (gas: 969910) -MarketTest:testClearMaturity() (gas: 1684799) -MarketTest:testCollectTreasuryFreeLunchToEarlyWithdraws() (gas: 1766343) +MarketTest:testClearMaturity() (gas: 1684770) +MarketTest:testCollectTreasuryFreeLunchToEarlyWithdraws() (gas: 1766256) MarketTest:testCollectTreasuryFreeLunchToEarlyWithdrawsWithZeroFees() (gas: 559359) MarketTest:testCollectTreasuryFreeLunchToFixedBorrows() (gas: 1811686) MarketTest:testCollectTreasuryFreeLunchToFixedBorrowsWithZeroFees() (gas: 722757) -MarketTest:testCrossMaturityLiquidation() (gas: 2625085) +MarketTest:testCrossMaturityLiquidation() (gas: 2625052) MarketTest:testDepositAfterFreezing() (gas: 256227) -MarketTest:testDepositAtMaturity() (gas: 193478) +MarketTest:testDepositAtMaturity() (gas: 193500) MarketTest:testDepositAtMaturityAfterFreezing() (gas: 267471) -MarketTest:testDepositAtMaturityWhenFrozen() (gas: 93209) +MarketTest:testDepositAtMaturityWhenFrozen() (gas: 93231) MarketTest:testDepositAtMaturityWithZeroAssets() (gas: 39977) MarketTest:testDepositDisagreement() (gas: 73904) MarketTest:testDepositShouldUpdateFlexibleBorrowVariables() (gas: 716971) -MarketTest:testDepositToSmartPool() (gas: 181827) +MarketTest:testDepositToSmartPool() (gas: 181849) MarketTest:testDepositWhenFrozen() (gas: 176471) -MarketTest:testDistributeMultipleAccumulatedEarnings() (gas: 1373903) -MarketTest:testDistributionOfLossesShouldReduceFromFloatingBackupBorrowedAccordingly() (gas: 6411223) -MarketTest:testEarlyRepayLiquidationUnassignedEarnings() (gas: 2116887) -MarketTest:testEarlyRepaymentWithExcessiveAmountOfFees() (gas: 3426445) +MarketTest:testDistributeMultipleAccumulatedEarnings() (gas: 1373914) +MarketTest:testDistributionOfLossesShouldReduceFromFloatingBackupBorrowedAccordingly() (gas: 6410451) +MarketTest:testEarlyRepayLiquidationUnassignedEarnings() (gas: 2116898) +MarketTest:testEarlyRepaymentWithExcessiveAmountOfFees() (gas: 3426434) MarketTest:testEarlyWithdrawFromFreeLunchShouldNotRevertWithFloatingFullUtilization() (gas: 1050103) MarketTest:testEmergencyAdminRole() (gas: 322263) MarketTest:testEmitFrozen() (gas: 91047) MarketTest:testFixedBorrowFailingWhenFlexibleBorrowAccruesDebt() (gas: 1530894) MarketTest:testFixedBorrowRateToMaturity() (gas: 562527) -MarketTest:testFixedOperationsUpdateFloatingDebt() (gas: 865402) +MarketTest:testFixedOperationsUpdateFloatingDebt() (gas: 865391) MarketTest:testFlexibleBorrow() (gas: 423656) MarketTest:testFlexibleBorrowAccountingDebt() (gas: 607131) -MarketTest:testFlexibleBorrowChargingDebtToTreasury() (gas: 751155) +MarketTest:testFlexibleBorrowChargingDebtToTreasury() (gas: 751177) MarketTest:testFlexibleBorrowExceedingReserve() (gas: 878430) MarketTest:testFlexibleBorrowExceedingReserveIncludingFixedBorrow() (gas: 1267485) MarketTest:testFlexibleBorrowExceedingReserveWithNewDebt() (gas: 1001209) @@ -208,77 +209,77 @@ MarketTest:testFlexibleBorrowFromAnotherUserSubtractsAllowance() (gas: 469265) MarketTest:testFlexibleBorrowFromAnotherUserWithAllowance() (gas: 458681) MarketTest:testFlexibleBorrowFromAnotherUserWithoutAllowance() (gas: 252381) MarketTest:testFrontRunSmartPoolEarningsDistributionWithBigPenaltyRepayment() (gas: 1373093) -MarketTest:testFullPause() (gas: 5959954) +MarketTest:testFullPause() (gas: 5959965) MarketTest:testInitiallyUnfrozen() (gas: 15592) -MarketTest:testInsufficientProtocolLiquidity() (gas: 1940418) -MarketTest:testLiquidateAndChargeIncentiveForLenders() (gas: 2433524) -MarketTest:testLiquidateAndDistributeLosses() (gas: 3234183) -MarketTest:testLiquidateAndSeizeExactAmountWithDustAsCollateral() (gas: 2799752) +MarketTest:testInsufficientProtocolLiquidity() (gas: 1940330) +MarketTest:testLiquidateAndChargeIncentiveForLenders() (gas: 2433480) +MarketTest:testLiquidateAndDistributeLosses() (gas: 3234378) +MarketTest:testLiquidateAndSeizeExactAmountWithDustAsCollateral() (gas: 2800634) MarketTest:testLiquidateAndSeizeFromEmptyCollateral() (gas: 1043463) -MarketTest:testLiquidateAndSubtractLossesFromAccumulator() (gas: 3907595) -MarketTest:testLiquidateFlexibleAndFixedBorrowPositionsInSingleCall() (gas: 2638544) -MarketTest:testLiquidateFlexibleBorrow() (gas: 2160641) -MarketTest:testLiquidateFlexibleBorrowChargeLendersAssetsToLiquidator() (gas: 1152387) +MarketTest:testLiquidateAndSubtractLossesFromAccumulator() (gas: 3899583) +MarketTest:testLiquidateFlexibleAndFixedBorrowPositionsInSingleCall() (gas: 2638533) +MarketTest:testLiquidateFlexibleBorrow() (gas: 2160663) +MarketTest:testLiquidateFlexibleBorrowChargeLendersAssetsToLiquidator() (gas: 1152409) MarketTest:testLiquidateFlexibleBorrowConsideringDebtOverTime() (gas: 1167683) -MarketTest:testLiquidateLeavingDustAsCollateral() (gas: 3643173) +MarketTest:testLiquidateLeavingDustAsCollateral() (gas: 3646055) MarketTest:testLiquidateTransferRepayAssetsBeforeSeize() (gas: 1045359) -MarketTest:testLiquidateUpdateFloatingDebt() (gas: 1903239) +MarketTest:testLiquidateUpdateFloatingDebt() (gas: 1903228) MarketTest:testLiquidateWhenFrozen() (gas: 1232974) -MarketTest:testLiquidateWithTwoUnitsAsMaxAssets() (gas: 1579206) +MarketTest:testLiquidateWithTwoUnitsAsMaxAssets() (gas: 1579217) MarketTest:testLiquidateWithZeroAsMaxAssets() (gas: 1043412) MarketTest:testLiquidationClearingDebtOfAllAccountMarkets() (gas: 3098516) -MarketTest:testLiquidationResultingInZeroCollateralAndZeroDebt() (gas: 1974455) +MarketTest:testLiquidationResultingInZeroCollateralAndZeroDebt() (gas: 1975359) MarketTest:testMaturityInsufficientProtocolLiquidity() (gas: 1464114) -MarketTest:testMultipleBorrowsForMultipleAssets() (gas: 2463544803) +MarketTest:testMultipleBorrowsForMultipleAssets() (gas: 2463575435) MarketTest:testMultipleDepositsToSmartPool() (gas: 915573) -MarketTest:testMultipleFixedBorrowsRepays() (gas: 1348420) -MarketTest:testMultipleLiquidationSameUser() (gas: 2943514) -MarketTest:testNotEnteredMarketShouldNotBeSeized() (gas: 8582160) +MarketTest:testMultipleFixedBorrowsRepays() (gas: 1348413) +MarketTest:testMultipleLiquidationSameUser() (gas: 2944374) +MarketTest:testNotEnteredMarketShouldNotBeSeized() (gas: 8589835) MarketTest:testOnlyAdminCanFreezeUnfreeze() (gas: 212203) MarketTest:testOperationsShouldUpdateFloatingAssetsAverage() (gas: 1445531) -MarketTest:testOperationsWithBtcWbtcRate() (gas: 8259325) -MarketTest:testOperationsWithStEthAsset() (gas: 8144210) +MarketTest:testOperationsWithBtcWbtcRate() (gas: 8267000) +MarketTest:testOperationsWithStEthAsset() (gas: 8151885) MarketTest:testPausable() (gas: 158136) MarketTest:testPauserRole() (gas: 78228) -MarketTest:testPreviewOperationsWithSmartPoolCorrectlyAccountingEarnings() (gas: 1976317) -MarketTest:testRepayAtMaturity() (gas: 586716) -MarketTest:testRepayDisagreement() (gas: 594037) -MarketTest:testRepayFlexibleBorrow() (gas: 1076351) +MarketTest:testPreviewOperationsWithSmartPoolCorrectlyAccountingEarnings() (gas: 1976306) +MarketTest:testRepayAtMaturity() (gas: 586727) +MarketTest:testRepayDisagreement() (gas: 594059) +MarketTest:testRepayFlexibleBorrow() (gas: 1076284) MarketTest:testRepayWhenFrozen() (gas: 535326) -MarketTest:testRoundingDownAssetsToValidateShortfallWhenTransferring() (gas: 7553932) -MarketTest:testRoundingDownAssetsToValidateShortfallWhenTransferringFrom() (gas: 7609151) +MarketTest:testRoundingDownAssetsToValidateShortfallWhenTransferring() (gas: 7561472) +MarketTest:testRoundingDownAssetsToValidateShortfallWhenTransferringFrom() (gas: 7616758) MarketTest:testRoundingDownAssetsWhenTransferingFromAnAccountWithoutShortfall() (gas: 1030183) MarketTest:testRoundingDownAssetsWhenTransferingWithAnAccountWithoutShortfall() (gas: 974749) -MarketTest:testRoundingUpAllowanceWhenBorrowingAtMaturity() (gas: 845691) +MarketTest:testRoundingUpAllowanceWhenBorrowingAtMaturity() (gas: 845713) MarketTest:testRoundingUpAllowanceWhenWithdrawingAtMaturity() (gas: 791732) -MarketTest:testSetAssetSymbol() (gas: 55013) +MarketTest:testSetAssetSymbol() (gas: 55035) MarketTest:testSetAssetSymbolNotAdmin() (gas: 66288) MarketTest:testSetDampSpeedFactorShouldUpdateFloatingAssetsAverage() (gas: 386872) MarketTest:testSetEarningsAccumulatorSmoothFactorShouldDistributeEarnings() (gas: 722432) -MarketTest:testSetInterestRateModelShouldUpdateFloatingDebt() (gas: 2803520) +MarketTest:testSetInterestRateModelShouldUpdateFloatingDebt() (gas: 2803542) MarketTest:testSetInterestRateModelWithAddressZeroShouldNotUpdateFloatingDebt() (gas: 2573368) MarketTest:testShareValueNotDecreasingAfterDeposit() (gas: 707864) MarketTest:testShareValueNotDecreasingWhenMintingToTreasury() (gas: 763200) MarketTest:testSingleFloatingBorrow() (gas: 417427) MarketTest:testSingleFloatingRepay() (gas: 484649) MarketTest:testSmartPoolEarningsDistribution() (gas: 1095688) -MarketTest:testSmartPoolSharesDoNotAccountUnassignedEarningsFromMoreThanOneIntervalPastMaturities() (gas: 528549) +MarketTest:testSmartPoolSharesDoNotAccountUnassignedEarningsFromMoreThanOneIntervalPastMaturities() (gas: 528463) MarketTest:testSumDebtPlusEffectsShouldntRoundUpWhenWithdrawing() (gas: 1005585) MarketTest:testTotalAssetsProjectingBackupEarningsCorrectly() (gas: 508280) MarketTest:testTotalAssetsProjectingFloatingDebtCorrectly() (gas: 713937) MarketTest:testUpdateAccumulatedEarningsFactorToZero() (gas: 1473262) -MarketTest:testUpdateFloatingAssetsAverageWhenDepositingAndBorrowingContinuously() (gas: 344727) +MarketTest:testUpdateFloatingAssetsAverageWhenDepositingAndBorrowingContinuously() (gas: 344749) MarketTest:testUpdateFloatingAssetsAverageWhenDepositingRightBeforeBorrow() (gas: 670454) MarketTest:testUpdateFloatingAssetsAverageWhenDepositingRightBeforeEarlyWithdraw() (gas: 630543) MarketTest:testUpdateFloatingAssetsAverageWhenDepositingSomeSecondsBeforeBorrow() (gas: 901042) -MarketTest:testUpdateFloatingAssetsAverageWhenWithdrawingRightBeforeBorrow() (gas: 683496) -MarketTest:testUpdateFloatingAssetsAverageWhenWithdrawingRightBeforeEarlyWithdraw() (gas: 643673) +MarketTest:testUpdateFloatingAssetsAverageWhenWithdrawingRightBeforeBorrow() (gas: 683518) +MarketTest:testUpdateFloatingAssetsAverageWhenWithdrawingRightBeforeEarlyWithdraw() (gas: 643695) MarketTest:testUpdateFloatingAssetsAverageWhenWithdrawingSomeSecondsBeforeBorrow() (gas: 373112) MarketTest:testUpdateFloatingAssetsAverageWithDampSpeedDown() (gas: 358048) -MarketTest:testUpdateFloatingAssetsAverageWithDampSpeedUp() (gas: 199785) -MarketTest:testUpdateFloatingDebtBeforeSettingTreasury() (gas: 106395) -MarketTest:testWithdrawAtMaturity() (gas: 304303) -MarketTest:testWithdrawFromSmartPool() (gas: 279199) +MarketTest:testUpdateFloatingAssetsAverageWithDampSpeedUp() (gas: 199697) +MarketTest:testUpdateFloatingDebtBeforeSettingTreasury() (gas: 106417) +MarketTest:testWithdrawAtMaturity() (gas: 304325) +MarketTest:testWithdrawFromSmartPool() (gas: 279221) MarketTest:testWithdrawShouldUpdateFlexibleBorrowVariables() (gas: 863981) MarketTest:testWithdrawWhenFrozen() (gas: 325961) PauserTest:testPauseProtocolFromRando() (gas: 32843) @@ -300,24 +301,24 @@ PreviewerTest:testAccountsReturningUtilizationForDifferentMaturities() (gas: 418 PreviewerTest:testAccountsWithAccountOnlyDeposit() (gas: 862391) PreviewerTest:testAccountsWithAccountThatHasBalances() (gas: 2265104) PreviewerTest:testAccountsWithEmptyAccount() (gas: 690558) -PreviewerTest:testAccountsWithIntermediateOperationsReturningAccurateAmounts() (gas: 17638834) +PreviewerTest:testAccountsWithIntermediateOperationsReturningAccurateAmounts() (gas: 17646503) PreviewerTest:testActualTimeBeforeStartDistributionRewards() (gas: 7836506) PreviewerTest:testEmptyExactly() (gas: 5725652) PreviewerTest:testExactlyReturningInterestRateModelData() (gas: 688149) -PreviewerTest:testFixedAvailableLiquidityProjectingNewFloatingDebt() (gas: 13322888) -PreviewerTest:testFixedPoolsA() (gas: 19344633) +PreviewerTest:testFixedAvailableLiquidityProjectingNewFloatingDebt() (gas: 13330557) +PreviewerTest:testFixedPoolsA() (gas: 19352291) PreviewerTest:testFixedPoolsChangingMaturityInTime() (gas: 1627253) -PreviewerTest:testFixedPoolsRatesAndUtilizations() (gas: 14836376) -PreviewerTest:testFixedPoolsWithFloatingAssetsAverage() (gas: 15659561) -PreviewerTest:testFlexibleAvailableLiquidity() (gas: 17268690) +PreviewerTest:testFixedPoolsRatesAndUtilizations() (gas: 14844045) +PreviewerTest:testFixedPoolsWithFloatingAssetsAverage() (gas: 15667219) +PreviewerTest:testFlexibleAvailableLiquidity() (gas: 17276348) PreviewerTest:testFlexibleBorrowSharesAndAssets() (gas: 4401038) -PreviewerTest:testFloatingAvailableLiquidityProjectingNewFloatingDebt() (gas: 12575548) +PreviewerTest:testFloatingAvailableLiquidityProjectingNewFloatingDebt() (gas: 12583217) PreviewerTest:testFloatingRateAndUtilization() (gas: 1128246) PreviewerTest:testJustUpdatedRewardRatesShouldStillReturnRate() (gas: 7255302) PreviewerTest:testMaxBorrowAssetsCapacity() (gas: 2482200) -PreviewerTest:testMaxBorrowAssetsCapacityForAccountWithShortfall() (gas: 11013073) -PreviewerTest:testMaxBorrowAssetsCapacityPerMarket() (gas: 13225692) -PreviewerTest:testOraclePriceReturningAccurateValues() (gas: 10154913) +PreviewerTest:testMaxBorrowAssetsCapacityForAccountWithShortfall() (gas: 11020748) +PreviewerTest:testMaxBorrowAssetsCapacityPerMarket() (gas: 13233361) +PreviewerTest:testOraclePriceReturningAccurateValues() (gas: 10162588) PreviewerTest:testPreviewBorrowAtAllMaturitiesReturningAccurateAmount() (gas: 4242626) PreviewerTest:testPreviewBorrowAtMaturityReturningAccurateAmount() (gas: 623428) PreviewerTest:testPreviewBorrowAtMaturityReturningAccurateAmountWithIntermediateOperations() (gas: 1954610) @@ -338,9 +339,9 @@ PreviewerTest:testPreviewDepositAtMaturityWithMaturedMaturity() (gas: 12565) PreviewerTest:testPreviewDepositAtMaturityWithOneUnit() (gas: 589435) PreviewerTest:testPreviewDepositAtMaturityWithSameTimestamp() (gas: 48970) PreviewerTest:testPreviewDepositAtMaturityWithZeroAmount() (gas: 589457) -PreviewerTest:testPreviewRepayAtMaturityLastAccrualIsMaturity() (gas: 1427968) -PreviewerTest:testPreviewRepayAtMaturityReturningAccurateAmount() (gas: 1249754) -PreviewerTest:testPreviewRepayAtMaturityReturningAccurateAmountWithIntermediateOperations() (gas: 1553636) +PreviewerTest:testPreviewRepayAtMaturityLastAccrualIsMaturity() (gas: 1427957) +PreviewerTest:testPreviewRepayAtMaturityReturningAccurateAmount() (gas: 1249743) +PreviewerTest:testPreviewRepayAtMaturityReturningAccurateAmountWithIntermediateOperations() (gas: 1553603) PreviewerTest:testPreviewRepayAtMaturityWithEmptyMaturity() (gas: 35313) PreviewerTest:testPreviewRepayAtMaturityWithEmptyMaturityAndZeroAmount() (gas: 35355) PreviewerTest:testPreviewRepayAtMaturityWithInvalidMaturity() (gas: 35355) @@ -363,8 +364,8 @@ PreviewerTest:testReserveFactor() (gas: 707280) PreviewerTest:testReturnRewardAssetUsdPrice() (gas: 6780317) PreviewerTest:testRewardsRateAfterDistributionEnd() (gas: 7561623) PreviewerTest:testRewardsRateOnlyWithFixedBorrows() (gas: 6868753) -PreviewerTest:testRewardsRateWithDifferentRewardLengths() (gas: 19345407) -PreviewerTest:testRewardsRateWithMarketWithDifferentDecimals() (gas: 18476882) +PreviewerTest:testRewardsRateWithDifferentRewardLengths() (gas: 19353076) +PreviewerTest:testRewardsRateWithMarketWithDifferentDecimals() (gas: 18484551) PreviewerTest:testRewardsRateX() (gas: 8208254) PriceFeedDoubleTest:testPriceFeedDoubleReturningAccurateDecimals() (gas: 605925) PriceFeedDoubleTest:testPriceFeedDoubleReturningPrice() (gas: 53190) @@ -388,9 +389,9 @@ RatePreviewerTest:testSnapshot() (gas: 1029231) RatePreviewerTest:testSnapshotWithSignificantElapsedTime() (gas: 1063339) RewardsControllerTest:testAccountKeeperClaimOnBehalfOf() (gas: 1294500) RewardsControllerTest:testAccrueRewardsForWholeDistributionPeriod() (gas: 1246256) -RewardsControllerTest:testAccrueRewardsWithBadDebtClearingOfFixedBorrow() (gas: 3340292) +RewardsControllerTest:testAccrueRewardsWithBadDebtClearingOfFixedBorrow() (gas: 3340281) RewardsControllerTest:testAccrueRewardsWithRepayOfBorrowBalance() (gas: 1603825) -RewardsControllerTest:testAccrueRewardsWithRepayOfFixedBorrowBalance() (gas: 1793176) +RewardsControllerTest:testAccrueRewardsWithRepayOfFixedBorrowBalance() (gas: 1793165) RewardsControllerTest:testAccrueRewardsWithSeizeOfAllDepositShares() (gas: 1992324) RewardsControllerTest:testAfterDistributionPeriodEnd() (gas: 1819278) RewardsControllerTest:testAllClaimableUSDCWithAnotherAccountInPool() (gas: 2274712) @@ -426,7 +427,7 @@ RewardsControllerTest:testNotKeeperClaimOnBehalfOf() (gas: 185650) RewardsControllerTest:testOperationAfterDistributionEnded() (gas: 723254) RewardsControllerTest:testOperationsBeforeDistributionStart() (gas: 1675913) RewardsControllerTest:testPermitClaim() (gas: 1276089) -RewardsControllerTest:testSetDistributionConfigWithDifferentDecimals() (gas: 11470350) +RewardsControllerTest:testSetDistributionConfigWithDifferentDecimals() (gas: 11478025) RewardsControllerTest:testSetDistributionOperationShouldUpdateIndex() (gas: 136066) RewardsControllerTest:testSetDistributionWithOnGoingMarketOperations() (gas: 1202681) RewardsControllerTest:testSetHigherTotalDistribution() (gas: 1833726) diff --git a/contracts/Market.sol b/contracts/Market.sol index 71f15b3f..b17d2ca5 100644 --- a/contracts/Market.sol +++ b/contracts/Market.sol @@ -643,16 +643,22 @@ contract Market is Initializable, AccessControlUpgradeable, PausableUpgradeable, if (address(memRewardsController) != address(0)) memRewardsController.handleBorrow(borrower); accumulator -= badDebt; totalBadDebt += badDebt; - floatingBackupBorrowed -= fixedPools[maturity].repay(position.principal); + uint256 backupDebtReduction = fixedPools[maturity].repay(position.principal); + floatingBackupBorrowed -= backupDebtReduction; + + if (backupDebtReduction != 0) { + uint256 yield = fixedPools[maturity].unassignedEarnings.mulDivDown( + Math.min(position.principal, backupDebtReduction), + backupDebtReduction + ); + fixedPools[maturity].unassignedEarnings -= yield; + earningsAccumulator += yield; + } + delete fixedBorrowPositions[maturity][borrower]; account.fixedBorrows = account.fixedBorrows.clearMaturity(maturity); emit RepayAtMaturity(maturity, msg.sender, borrower, badDebt, badDebt); - - if (fixedPools[maturity].borrowed == position.principal) { - earningsAccumulator += fixedPools[maturity].unassignedEarnings; - fixedPools[maturity].unassignedEarnings = 0; - } } } packedMaturities >>= 1; diff --git a/test/Market.t.sol b/test/Market.t.sol index 0083438a..a3f4fce8 100644 --- a/test/Market.t.sol +++ b/test/Market.t.sol @@ -1126,6 +1126,51 @@ contract MarketTest is Test { assertEq(market.floatingDebt(), floatingDebtBefore); } + function testClearBadDebtEmptiesUnassignedEarnings() external { + uint256 maxVal = type(uint256).max; + + marketWETH.deposit(100000 ether, address(this)); + marketWETH.borrowAtMaturity(4 weeks, 10000e18, maxVal, address(this), address(this)); + marketWETH.repayAtMaturity(4 weeks, maxVal, maxVal, address(this)); + + vm.startPrank(BOB); + market.deposit(100 ether, BOB); + auditor.enterMarket(market); + marketWETH.borrowAtMaturity(4 weeks, 60e18, maxVal, BOB, BOB); + vm.stopPrank(); + + vm.startPrank(ALICE); + weth.mint(ALICE, 1_000_000 ether); + weth.approve(address(marketWETH), type(uint256).max); + marketWETH.deposit(10, ALICE); + marketWETH.borrowAtMaturity(4 weeks, 1, maxVal, ALICE, ALICE); + vm.stopPrank(); + + daiPriceFeed.setPrice(0.6e18); + + vm.startPrank(ALICE); + marketWETH.liquidate(BOB, 100_000 ether, market); + vm.stopPrank(); + + (, , uint256 unassignedEarningsAfter, ) = marketWETH.fixedPools(4 weeks); + assertEq(unassignedEarningsAfter, 0); + + address account = makeAddr("account"); + vm.startPrank(account); + deal(address(weth), account, 10); + weth.approve(address(marketWETH), 10); + marketWETH.depositAtMaturity(4 weeks, 10, 0, account); + vm.stopPrank(); + + (, , unassignedEarningsAfter, ) = marketWETH.fixedPools(4 weeks); + assertEq(unassignedEarningsAfter, 0); + + // account should not get unassigned earnings + (uint256 principal, uint256 fee) = marketWETH.fixedDepositPositions(4 weeks, account); + assertEq(principal, 10); + assertEq(fee, 0); + } + function testClearBadDebtCalledByAccount() external { vm.expectRevert(NotAuditor.selector); market.clearBadDebt(address(this)); @@ -1677,7 +1722,7 @@ contract MarketTest is Test { uint256 lendersIncentive = 1181818181818181800; uint256 distributedEarnings = 680440721299864513; uint256 badDebt = 981818181818181818100 + 1100000000000000000000 + 1100000000000000000000 + 1100000000000000000000; - uint256 untrackedUnassignedEarnings = 300000000000000000000; + uint256 untrackedUnassignedEarnings = 300000000000000000000 + 29752070215138961802; uint256 earlyRepayEarnings = 3069658128703695345; uint256 accumulatedEarnings = (earningsAccumulatorBefore + lendersIncentive).mulDivDown( block.timestamp - market.lastAccumulatorAccrual(),