From bb38a54388bc8065c8f3c9fade9b3174896461e7 Mon Sep 17 00:00:00 2001 From: Santiago Sanchez Avalos Date: Tue, 21 May 2024 09:41:21 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20market:=20track=20earnings=20in?= =?UTF-8?q?=20bad=20debt=20clearing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/violet-hairs-attend.md | 5 + .gas-snapshot | 153 +++++++++++++++--------------- contracts/Market.sol | 5 + test/Market.t.sol | 43 ++++++++- 4 files changed, 127 insertions(+), 79 deletions(-) create mode 100644 .changeset/violet-hairs-attend.md diff --git a/.changeset/violet-hairs-attend.md b/.changeset/violet-hairs-attend.md new file mode 100644 index 00000000..5dc02ad0 --- /dev/null +++ b/.changeset/violet-hairs-attend.md @@ -0,0 +1,5 @@ +--- +"@exactly/protocol": patch +--- + +🐛 market: track earnings in bad debt clearing diff --git a/.gas-snapshot b/.gas-snapshot index c02616ed..1fe0c603 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -143,141 +143,142 @@ InterestRateModelTest:testFixedRateRevertUtilizationExceeded() (gas: 2053399) InterestRateModelTest:testFloatingBorrowRate() (gas: 2045540) InterestRateModelTest:testFuzzFixedRateGrowth(uint256,uint256,uint256,uint256) (runs: 256, μ: 2067117, ~: 2063904) InterestRateModelTest:testFuzzFixedRateTimeSensitivity(uint256,uint256,uint256) (runs: 256, μ: 2073166, ~: 2073210) -InterestRateModelTest:testFuzzReferenceLegacyRateFixed(uint32,uint256,uint256[2],uint256[2],uint256,uint256,uint256) (runs: 256, μ: 9955962, ~: 10120518) +InterestRateModelTest:testFuzzReferenceLegacyRateFixed(uint32,uint256,uint256[2],uint256[2],uint256,uint256,uint256) (runs: 256, μ: 9973344, ~: 10137900) InterestRateModelTest:testFuzzReferenceRateFixed(uint256,uint256,uint256,uint256,uint256,uint256,(uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,int256,uint256,uint256)) (runs: 256, μ: 2337454, ~: 2339865) InterestRateModelTest:testFuzzReferenceRateFloating(uint256,uint256,(uint256,uint256,uint256,uint256,uint256,uint256,uint256)) (runs: 256, μ: 2275596, ~: 2276456) InterestRateModelTest:testMinTimeToMaturity() (gas: 2063554) InterestRateModelTest:testRevertMaxUtilizationLowerThanWad() (gas: 266682) MarketTest:testAccountLiquidityAdjustedDebt() (gas: 499440) MarketTest:testAnotherUserRedeemWhenOwnerHasShortfall() (gas: 819075) -MarketTest:testAnotherUserWithdrawWhenOwnerHasShortfall() (gas: 806767) +MarketTest:testAnotherUserWithdrawWhenOwnerHasShortfall() (gas: 806834) MarketTest:testBorrowAfterFreezing() (gas: 491482) MarketTest:testBorrowAtMaturity() (gas: 499544) MarketTest:testBorrowAtMaturityAfterFreezing() (gas: 573160) -MarketTest:testBorrowAtMaturityUpdatesFloatingDebtAndFloatingAssets() (gas: 884694) +MarketTest:testBorrowAtMaturityUpdatesFloatingDebtAndFloatingAssets() (gas: 884672) MarketTest:testBorrowAtMaturityWhenFrozen() (gas: 93696) -MarketTest:testBorrowAtMaturityWithZeroAssets() (gas: 40425) +MarketTest:testBorrowAtMaturityWithZeroAssets() (gas: 40403) MarketTest:testBorrowDisagreement() (gas: 295415) -MarketTest:testBorrowFromFreeLunchShouldNotRevertWithFloatingFullUtilization() (gas: 1294314) -MarketTest:testBorrowWhenFrozen() (gas: 93224) -MarketTest:testBorrowWithZeroAssets() (gas: 39771) +MarketTest:testBorrowFromFreeLunchShouldNotRevertWithFloatingFullUtilization() (gas: 1294292) +MarketTest:testBorrowWhenFrozen() (gas: 93202) +MarketTest:testBorrowWithZeroAssets() (gas: 39749) MarketTest:testCappedLiquidation() (gas: 1197582) MarketTest:testChargeTreasuryToEarlyWithdraws() (gas: 1218518) MarketTest:testChargeTreasuryToFixedBorrows() (gas: 1576488) -MarketTest:testClearBadDebtAvoidingFixedBorrowsIfAccumulatorLower() (gas: 2277467) +MarketTest:testClearBadDebtAvoidingFixedBorrowsIfAccumulatorLower() (gas: 2277445) +MarketTest:testClearBadDebtBeforeMaturity() (gas: 2347464) MarketTest:testClearBadDebtCalledByAccount() (gas: 35021) -MarketTest:testClearBadDebtExactlyRepaysFixedBorrowWithAccumulatorAmount() (gas: 2008262) -MarketTest:testClearBadDebtPartiallyRepaysEachFixedBorrow() (gas: 1955355) -MarketTest:testClearBadDebtPartiallyRepaysFloatingDebt() (gas: 2285567) +MarketTest:testClearBadDebtExactlyRepaysFixedBorrowWithAccumulatorAmount() (gas: 2008550) +MarketTest:testClearBadDebtPartiallyRepaysEachFixedBorrow() (gas: 1955889) +MarketTest:testClearBadDebtPartiallyRepaysFloatingDebt() (gas: 2295699) MarketTest:testClearBadDebtShouldAccrueAccumulatedEarningsBeforeSpreadingLosses() (gas: 2075328) MarketTest:testClearBadDebtWithEmptyAccumulatorShouldNotRevert() (gas: 969888) MarketTest:testClearMaturity() (gas: 1681866) -MarketTest:testCollectTreasuryFreeLunchToEarlyWithdraws() (gas: 1766265) +MarketTest:testCollectTreasuryFreeLunchToEarlyWithdraws() (gas: 1766243) MarketTest:testCollectTreasuryFreeLunchToEarlyWithdrawsWithZeroFees() (gas: 559337) MarketTest:testCollectTreasuryFreeLunchToFixedBorrows() (gas: 1808756) MarketTest:testCollectTreasuryFreeLunchToFixedBorrowsWithZeroFees() (gas: 722696) MarketTest:testCrossMaturityLiquidation() (gas: 2627134) MarketTest:testDepositAfterFreezing() (gas: 256205) -MarketTest:testDepositAtMaturity() (gas: 193478) +MarketTest:testDepositAtMaturity() (gas: 193456) MarketTest:testDepositAtMaturityAfterFreezing() (gas: 267449) -MarketTest:testDepositAtMaturityWhenFrozen() (gas: 93209) +MarketTest:testDepositAtMaturityWhenFrozen() (gas: 93187) MarketTest:testDepositAtMaturityWithZeroAssets() (gas: 39977) MarketTest:testDepositDisagreement() (gas: 73882) MarketTest:testDepositShouldUpdateFlexibleBorrowVariables() (gas: 716949) -MarketTest:testDepositToSmartPool() (gas: 181849) +MarketTest:testDepositToSmartPool() (gas: 181827) MarketTest:testDepositWhenFrozen() (gas: 176471) -MarketTest:testDistributeMultipleAccumulatedEarnings() (gas: 1371073) -MarketTest:testDistributionOfLossesShouldReduceFromFloatingBackupBorrowedAccordingly() (gas: 6401781) -MarketTest:testEarlyRepayLiquidationUnassignedEarnings() (gas: 2063715) +MarketTest:testDistributeMultipleAccumulatedEarnings() (gas: 1371051) +MarketTest:testDistributionOfLossesShouldReduceFromFloatingBackupBorrowedAccordingly() (gas: 6403665) +MarketTest:testEarlyRepayLiquidationUnassignedEarnings() (gas: 2063693) MarketTest:testEarlyRepaymentWithExcessiveAmountOfFees() (gas: 3407434) MarketTest:testEarlyWithdrawFromFreeLunchShouldNotRevertWithFloatingFullUtilization() (gas: 1050081) -MarketTest:testEmergencyAdminRole() (gas: 317196) +MarketTest:testEmergencyAdminRole() (gas: 317263) MarketTest:testEmitFrozen() (gas: 91114) MarketTest:testFixedBorrowFailingWhenFlexibleBorrowAccruesDebt() (gas: 1530833) MarketTest:testFixedBorrowRateToMaturity() (gas: 562488) MarketTest:testFixedOperationsUpdateFloatingDebt() (gas: 865363) MarketTest:testFlexibleBorrow() (gas: 423743) MarketTest:testFlexibleBorrowAccountingDebt() (gas: 607131) -MarketTest:testFlexibleBorrowChargingDebtToTreasury() (gas: 751177) +MarketTest:testFlexibleBorrowChargingDebtToTreasury() (gas: 751155) MarketTest:testFlexibleBorrowExceedingReserve() (gas: 878408) MarketTest:testFlexibleBorrowExceedingReserveIncludingFixedBorrow() (gas: 1267534) MarketTest:testFlexibleBorrowExceedingReserveWithNewDebt() (gas: 1001187) MarketTest:testFlexibleBorrowFromAnotherUserSubtractsAllowance() (gas: 469265) MarketTest:testFlexibleBorrowFromAnotherUserWithAllowance() (gas: 458659) MarketTest:testFlexibleBorrowFromAnotherUserWithoutAllowance() (gas: 252359) -MarketTest:testFrontRunSmartPoolEarningsDistributionWithBigPenaltyRepayment() (gas: 1370263) -MarketTest:testFullPause() (gas: 5956320) -MarketTest:testInitiallyUnfrozen() (gas: 15614) -MarketTest:testInsufficientProtocolLiquidity() (gas: 1940379) +MarketTest:testFrontRunSmartPoolEarningsDistributionWithBigPenaltyRepayment() (gas: 1370330) +MarketTest:testFullPause() (gas: 5956298) +MarketTest:testInitiallyUnfrozen() (gas: 15592) +MarketTest:testInsufficientProtocolLiquidity() (gas: 1940357) MarketTest:testLiquidateAndChargeIncentiveForLenders() (gas: 2431160) -MarketTest:testLiquidateAndDistributeLosses() (gas: 3228069) -MarketTest:testLiquidateAndSeizeExactAmountWithDustAsCollateral() (gas: 2795830) +MarketTest:testLiquidateAndDistributeLosses() (gas: 3228989) +MarketTest:testLiquidateAndSeizeExactAmountWithDustAsCollateral() (gas: 2796118) MarketTest:testLiquidateAndSeizeFromEmptyCollateral() (gas: 1039302) -MarketTest:testLiquidateAndSubtractLossesFromAccumulator() (gas: 3920994) -MarketTest:testLiquidateFlexibleAndFixedBorrowPositionsInSingleCall() (gas: 2631842) -MarketTest:testLiquidateFlexibleBorrow() (gas: 2156186) -MarketTest:testLiquidateFlexibleBorrowChargeLendersAssetsToLiquidator() (gas: 1150179) -MarketTest:testLiquidateFlexibleBorrowConsideringDebtOverTime() (gas: 1166270) -MarketTest:testLiquidateLeavingDustAsCollateral() (gas: 3622684) +MarketTest:testLiquidateAndSubtractLossesFromAccumulator() (gas: 3903864) +MarketTest:testLiquidateFlexibleAndFixedBorrowPositionsInSingleCall() (gas: 2631820) +MarketTest:testLiquidateFlexibleBorrow() (gas: 2156253) +MarketTest:testLiquidateFlexibleBorrowChargeLendersAssetsToLiquidator() (gas: 1150267) +MarketTest:testLiquidateFlexibleBorrowConsideringDebtOverTime() (gas: 1166248) +MarketTest:testLiquidateLeavingDustAsCollateral() (gas: 3622972) MarketTest:testLiquidateTransferRepayAssetsBeforeSeize() (gas: 1045448) MarketTest:testLiquidateUpdateFloatingDebt() (gas: 1901726) -MarketTest:testLiquidateWhenFrozen() (gas: 1230788) -MarketTest:testLiquidateWithTwoUnitsAsMaxAssets() (gas: 1576942) +MarketTest:testLiquidateWhenFrozen() (gas: 1230766) +MarketTest:testLiquidateWithTwoUnitsAsMaxAssets() (gas: 1576920) MarketTest:testLiquidateWithZeroAsMaxAssets() (gas: 1039143) MarketTest:testLiquidationClearingDebtOfAllAccountMarkets() (gas: 3093414) -MarketTest:testLiquidationResultingInZeroCollateralAndZeroDebt() (gas: 1969108) +MarketTest:testLiquidationResultingInZeroCollateralAndZeroDebt() (gas: 1969374) MarketTest:testMaturityInsufficientProtocolLiquidity() (gas: 1464163) -MarketTest:testMultipleBorrowsForMultipleAssets() (gas: 2463367095) +MarketTest:testMultipleBorrowsForMultipleAssets() (gas: 2463436529) MarketTest:testMultipleDepositsToSmartPool() (gas: 915551) -MarketTest:testMultipleFixedBorrowsRepays() (gas: 1348303) -MarketTest:testMultipleLiquidationSameUser() (gas: 2946695) -MarketTest:testNotEnteredMarketShouldNotBeSeized() (gas: 8537120) +MarketTest:testMultipleFixedBorrowsRepays() (gas: 1348281) +MarketTest:testMultipleLiquidationSameUser() (gas: 2946983) +MarketTest:testNotEnteredMarketShouldNotBeSeized() (gas: 8554502) MarketTest:testOnlyAdminCanFreezeUnfreeze() (gas: 207681) -MarketTest:testOperationsShouldUpdateFloatingAssetsAverage() (gas: 1395020) -MarketTest:testOperationsWithBtcWbtcRate() (gas: 8215237) -MarketTest:testOperationsWithStEthAsset() (gas: 8102579) +MarketTest:testOperationsShouldUpdateFloatingAssetsAverage() (gas: 1395087) +MarketTest:testOperationsWithBtcWbtcRate() (gas: 8232619) +MarketTest:testOperationsWithStEthAsset() (gas: 8119961) MarketTest:testPausable() (gas: 158114) MarketTest:testPauserRole() (gas: 78206) MarketTest:testPreviewOperationsWithSmartPoolCorrectlyAccountingEarnings() (gas: 1976217) -MarketTest:testRepayAtMaturity() (gas: 586699) -MarketTest:testRepayDisagreement() (gas: 594020) -MarketTest:testRepayFlexibleBorrow() (gas: 1076351) +MarketTest:testRepayAtMaturity() (gas: 586677) +MarketTest:testRepayDisagreement() (gas: 593998) +MarketTest:testRepayFlexibleBorrow() (gas: 1076329) MarketTest:testRepayWhenFrozen() (gas: 535304) -MarketTest:testRoundingDownAssetsToValidateShortfallWhenTransferring() (gas: 7520833) -MarketTest:testRoundingDownAssetsToValidateShortfallWhenTransferringFrom() (gas: 7576140) +MarketTest:testRoundingDownAssetsToValidateShortfallWhenTransferring() (gas: 7538124) +MarketTest:testRoundingDownAssetsToValidateShortfallWhenTransferringFrom() (gas: 7593453) MarketTest:testRoundingDownAssetsWhenTransferingFromAnAccountWithoutShortfall() (gas: 1030122) MarketTest:testRoundingDownAssetsWhenTransferingWithAnAccountWithoutShortfall() (gas: 974777) -MarketTest:testRoundingUpAllowanceWhenBorrowingAtMaturity() (gas: 845574) +MarketTest:testRoundingUpAllowanceWhenBorrowingAtMaturity() (gas: 845552) MarketTest:testRoundingUpAllowanceWhenWithdrawingAtMaturity() (gas: 791671) MarketTest:testSetAssetSymbol() (gas: 54991) MarketTest:testSetAssetSymbolNotAdmin() (gas: 66288) -MarketTest:testSetDampSpeedFactorShouldUpdateFloatingAssetsAverage() (gas: 386894) +MarketTest:testSetDampSpeedFactorShouldUpdateFloatingAssetsAverage() (gas: 386872) MarketTest:testSetEarningsAccumulatorSmoothFactorShouldDistributeEarnings() (gas: 722371) -MarketTest:testSetInterestRateModelShouldUpdateFloatingDebt() (gas: 2797956) +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:testSmartPoolEarningsDistribution() (gas: 1092767) -MarketTest:testSmartPoolSharesDoNotAccountUnassignedEarningsFromMoreThanOneIntervalPastMaturities() (gas: 528424) +MarketTest:testSmartPoolEarningsDistribution() (gas: 1092855) +MarketTest:testSmartPoolSharesDoNotAccountUnassignedEarningsFromMoreThanOneIntervalPastMaturities() (gas: 528510) MarketTest:testSumDebtPlusEffectsShouldntRoundUpWhenWithdrawing() (gas: 1005524) MarketTest:testTotalAssetsProjectingBackupEarningsCorrectly() (gas: 508219) MarketTest:testTotalAssetsProjectingFloatingDebtCorrectly() (gas: 713915) -MarketTest:testUpdateAccumulatedEarningsFactorToZero() (gas: 1470345) -MarketTest:testUpdateFloatingAssetsAverageWhenDepositingAndBorrowingContinuously() (gas: 344749) +MarketTest:testUpdateAccumulatedEarningsFactorToZero() (gas: 1470432) +MarketTest:testUpdateFloatingAssetsAverageWhenDepositingAndBorrowingContinuously() (gas: 344727) MarketTest:testUpdateFloatingAssetsAverageWhenDepositingRightBeforeBorrow() (gas: 630288) MarketTest:testUpdateFloatingAssetsAverageWhenDepositingRightBeforeEarlyWithdraw() (gas: 630610) MarketTest:testUpdateFloatingAssetsAverageWhenDepositingSomeSecondsBeforeBorrow() (gas: 854932) -MarketTest:testUpdateFloatingAssetsAverageWhenWithdrawingRightBeforeBorrow() (gas: 643352) -MarketTest:testUpdateFloatingAssetsAverageWhenWithdrawingRightBeforeEarlyWithdraw() (gas: 643673) -MarketTest:testUpdateFloatingAssetsAverageWhenWithdrawingSomeSecondsBeforeBorrow() (gas: 373134) -MarketTest:testUpdateFloatingAssetsAverageWithDampSpeedDown() (gas: 358070) -MarketTest:testUpdateFloatingAssetsAverageWithDampSpeedUp() (gas: 199785) -MarketTest:testUpdateFloatingDebtBeforeSettingTreasury() (gas: 106395) -MarketTest:testWithdrawAtMaturity() (gas: 304303) -MarketTest:testWithdrawFromSmartPool() (gas: 279199) -MarketTest:testWithdrawShouldUpdateFlexibleBorrowVariables() (gas: 864003) +MarketTest:testUpdateFloatingAssetsAverageWhenWithdrawingRightBeforeBorrow() (gas: 643419) +MarketTest:testUpdateFloatingAssetsAverageWhenWithdrawingRightBeforeEarlyWithdraw() (gas: 643760) +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:testWithdrawShouldUpdateFlexibleBorrowVariables() (gas: 863981) MarketTest:testWithdrawWhenFrozen() (gas: 325939) PoolLibTest:testAtomicDepositBorrowRepayWithdraw() (gas: 46018) PoolLibTest:testBackupBorrow() (gas: 33676) @@ -291,24 +292,24 @@ PreviewerTest:testAccountsReturningUtilizationForDifferentMaturities() (gas: 418 PreviewerTest:testAccountsWithAccountOnlyDeposit() (gas: 862391) PreviewerTest:testAccountsWithAccountThatHasBalances() (gas: 2265026) PreviewerTest:testAccountsWithEmptyAccount() (gas: 690558) -PreviewerTest:testAccountsWithIntermediateOperationsReturningAccurateAmounts() (gas: 17600139) +PreviewerTest:testAccountsWithIntermediateOperationsReturningAccurateAmounts() (gas: 17617503) PreviewerTest:testActualTimeBeforeStartDistributionRewards() (gas: 7753427) PreviewerTest:testEmptyExactly() (gas: 5645469) PreviewerTest:testExactlyReturningInterestRateModelData() (gas: 688149) -PreviewerTest:testFixedAvailableLiquidityProjectingNewFloatingDebt() (gas: 13284232) -PreviewerTest:testFixedPoolsA() (gas: 19300977) +PreviewerTest:testFixedAvailableLiquidityProjectingNewFloatingDebt() (gas: 13301596) +PreviewerTest:testFixedPoolsA() (gas: 19318341) PreviewerTest:testFixedPoolsChangingMaturityInTime() (gas: 1627253) -PreviewerTest:testFixedPoolsRatesAndUtilizations() (gas: 14797720) -PreviewerTest:testFixedPoolsWithFloatingAssetsAverage() (gas: 15620944) -PreviewerTest:testFlexibleAvailableLiquidity() (gas: 17225073) +PreviewerTest:testFixedPoolsRatesAndUtilizations() (gas: 14815084) +PreviewerTest:testFixedPoolsWithFloatingAssetsAverage() (gas: 15638308) +PreviewerTest:testFlexibleAvailableLiquidity() (gas: 17242437) PreviewerTest:testFlexibleBorrowSharesAndAssets() (gas: 4401038) -PreviewerTest:testFloatingAvailableLiquidityProjectingNewFloatingDebt() (gas: 12536970) +PreviewerTest:testFloatingAvailableLiquidityProjectingNewFloatingDebt() (gas: 12554334) PreviewerTest:testFloatingRateAndUtilization() (gas: 1128246) PreviewerTest:testJustUpdatedRewardRatesShouldStillReturnRate() (gas: 7174821) PreviewerTest:testMaxBorrowAssetsCapacity() (gas: 2469700) -PreviewerTest:testMaxBorrowAssetsCapacityForAccountWithShortfall() (gas: 10974464) -PreviewerTest:testMaxBorrowAssetsCapacityPerMarket() (gas: 13164614) -PreviewerTest:testOraclePriceReturningAccurateValues() (gas: 10116304) +PreviewerTest:testMaxBorrowAssetsCapacityForAccountWithShortfall() (gas: 10991846) +PreviewerTest:testMaxBorrowAssetsCapacityPerMarket() (gas: 13181978) +PreviewerTest:testOraclePriceReturningAccurateValues() (gas: 10133686) PreviewerTest:testPreviewBorrowAtAllMaturitiesReturningAccurateAmount() (gas: 4240988) PreviewerTest:testPreviewBorrowAtMaturityReturningAccurateAmount() (gas: 623350) PreviewerTest:testPreviewBorrowAtMaturityReturningAccurateAmountWithIntermediateOperations() (gas: 1954376) @@ -354,8 +355,8 @@ PreviewerTest:testReserveFactor() (gas: 707280) PreviewerTest:testReturnRewardAssetUsdPrice() (gas: 6697322) PreviewerTest:testRewardsRateAfterDistributionEnd() (gas: 7481122) PreviewerTest:testRewardsRateOnlyWithFixedBorrows() (gas: 6788297) -PreviewerTest:testRewardsRateWithDifferentRewardLengths() (gas: 19223904) -PreviewerTest:testRewardsRateWithMarketWithDifferentDecimals() (gas: 18354928) +PreviewerTest:testRewardsRateWithDifferentRewardLengths() (gas: 19241268) +PreviewerTest:testRewardsRateWithMarketWithDifferentDecimals() (gas: 18372292) PreviewerTest:testRewardsRateX() (gas: 8127662) PriceFeedDoubleTest:testPriceFeedDoubleReturningAccurateDecimals() (gas: 597567) PriceFeedDoubleTest:testPriceFeedDoubleReturningPrice() (gas: 53190) @@ -409,7 +410,7 @@ RewardsControllerTest:testLastUndistributed() (gas: 2189661) RewardsControllerTest:testOperationAfterDistributionEnded() (gas: 722976) RewardsControllerTest:testOperationsBeforeDistributionStart() (gas: 1674576) RewardsControllerTest:testPermitClaim() (gas: 1275282) -RewardsControllerTest:testSetDistributionConfigWithDifferentDecimals() (gas: 11427677) +RewardsControllerTest:testSetDistributionConfigWithDifferentDecimals() (gas: 11445059) RewardsControllerTest:testSetDistributionOperationShouldUpdateIndex() (gas: 136200) RewardsControllerTest:testSetDistributionWithOnGoingMarketOperations() (gas: 1202358) RewardsControllerTest:testSetHigherTotalDistribution() (gas: 1831201) diff --git a/contracts/Market.sol b/contracts/Market.sol index 1047edb2..ebd194cf 100644 --- a/contracts/Market.sol +++ b/contracts/Market.sol @@ -647,6 +647,11 @@ contract Market is Initializable, AccessControlUpgradeable, PausableUpgradeable, 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 f1b9cb58..c5f763c2 100644 --- a/test/Market.t.sol +++ b/test/Market.t.sol @@ -1140,10 +1140,12 @@ contract MarketTest is Test { // only first fixed borrow is covered (uint256 principal, uint256 fee) = marketWETH.fixedBorrowPositions(FixedLib.INTERVAL, address(this)); + (, , uint256 unassignedEarningsAfter, ) = market.fixedPools(FixedLib.INTERVAL); + assertEq(unassignedEarningsAfter, 0); assertEq(principal + fee, 0); (principal, fee) = marketWETH.fixedBorrowPositions(FixedLib.INTERVAL * 2, address(this)); assertEq(principal + fee, 400 ether); - assertEq(marketWETH.earningsAccumulator(), 10 ether); + assertEq(marketWETH.earningsAccumulator(), 15 ether); } function testClearBadDebtExactlyRepaysFixedBorrowWithAccumulatorAmount() external { @@ -1204,9 +1206,11 @@ contract MarketTest is Test { assertEq(principal + fee, 0); (principal, fee) = marketWETH.fixedBorrowPositions(FixedLib.INTERVAL * 2, address(this)); (, , uint256 floatingBorrowShares) = marketWETH.accounts(address(this)); + (, , uint256 unassignedEarningsAfter, ) = market.fixedPools(FixedLib.INTERVAL); + assertEq(unassignedEarningsAfter, 0); assertEq(principal + fee, 400 ether); assertEq(marketWETH.previewRefund(floatingBorrowShares), 40 ether); - assertEq(marketWETH.earningsAccumulator(), 0); + assertEq(marketWETH.earningsAccumulator(), 5 ether); } function testClearBadDebtAvoidingFixedBorrowsIfAccumulatorLower() external { @@ -1568,6 +1572,37 @@ contract MarketTest is Test { vm.stopPrank(); } + function testClearBadDebtBeforeMaturity() external { + market.deposit(5 ether, address(this)); + market.deposit(5_000 ether, ALICE); + marketWETH.deposit(100_000 ether, ALICE); + + uint256 maxVal = type(uint256).max; + vm.prank(ALICE); + market.borrowAtMaturity(4 weeks, 1_00 ether, maxVal, ALICE, ALICE); + + vm.warp(12 weeks); + market.repayAtMaturity(4 weeks, maxVal, maxVal, ALICE); + + uint256 maturity_16 = 16 weeks; + market.borrowAtMaturity(maturity_16, 1 ether, maxVal, address(this), address(this)); + + daiPriceFeed.setPrice(5_000e18); + uint256 borrowAmount = 5000 ether; + marketWETH.borrowAtMaturity(maturity_16, borrowAmount, borrowAmount * 2, address(this), address(this)); + + daiPriceFeed.setPrice(1_000e18); + weth.mint(ALICE, 1_000_000 ether); + vm.prank(ALICE); + weth.approve(address(marketWETH), maxVal); + + vm.prank(ALICE); + marketWETH.liquidate(address(this), maxVal, market); + + (, , uint256 unassignedEarningsAfter, ) = market.fixedPools(maturity_16); + assertEq(unassignedEarningsAfter, 0); + } + function testLiquidateAndSubtractLossesFromAccumulator() external { vm.warp(1); marketWETH.deposit(1.3 ether, address(this)); @@ -1603,6 +1638,7 @@ contract MarketTest is Test { uint256 earningsAccumulatorBefore = market.earningsAccumulator(); uint256 lendersIncentive = 1181818181818181800; uint256 badDebt = 981818181818181818100 + 1100000000000000000000 + 1100000000000000000000 + 1100000000000000000000; + uint256 untrackedUnassignedEarnings = 300000000000000000000; uint256 earlyRepayEarnings = 3069658128703695345; uint256 accumulatedEarnings = (earningsAccumulatorBefore + lendersIncentive).mulDivDown( block.timestamp - market.lastAccumulatorAccrual(), @@ -1623,7 +1659,8 @@ contract MarketTest is Test { market.earningsAccumulator() - accumulatedEarnings + earlyRepayEarnings + - lendersIncentive, + lendersIncentive + + untrackedUnassignedEarnings, 1e2 ); (, uint256 fixedBorrows, ) = market.accounts(address(this));