Skip to content

Commit

Permalink
馃悰 market: trigger rewards before floatingDebt increase
Browse files Browse the repository at this point in the history
  • Loading branch information
santichez committed Apr 12, 2023
1 parent 5e8f17e commit 953f33f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-moose-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@exactly-protocol/protocol": patch
---

馃悰 market: trigger rewards before `floatingDebt` increase
65 changes: 33 additions & 32 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ MarketTest:testFixedBorrowFailingWhenFlexibleBorrowAccruesDebt() (gas: 773534)
MarketTest:testFlexibleBorrow() (gas: 329289)
MarketTest:testFlexibleBorrowAccountingDebt() (gas: 322293)
MarketTest:testFlexibleBorrowChargingDebtToTreasury() (gas: 476494)
MarketTest:testFlexibleBorrowExceedingReserve() (gas: 520649)
MarketTest:testFlexibleBorrowExceedingReserveIncludingFixedBorrow() (gas: 703043)
MarketTest:testFlexibleBorrowExceedingReserve() (gas: 520790)
MarketTest:testFlexibleBorrowExceedingReserveIncludingFixedBorrow() (gas: 703184)
MarketTest:testFlexibleBorrowExceedingReserveWithNewDebt() (gas: 656739)
MarketTest:testFlexibleBorrowFromAnotherUserSubtractsAllowance() (gas: 351605)
MarketTest:testFlexibleBorrowFromAnotherUserWithAllowance() (gas: 344548)
Expand All @@ -82,7 +82,7 @@ MarketTest:testLiquidateWithTwoUnitsAsMaxAssets() (gas: 869116)
MarketTest:testLiquidateWithZeroAsMaxAssets() (gas: 612540)
MarketTest:testLiquidationClearingDebtOfAllAccountMarkets() (gas: 1318472)
MarketTest:testLiquidationResultingInZeroCollateralAndZeroDebt() (gas: 913182)
MarketTest:testMaturityInsufficientProtocolLiquidity() (gas: 877118)
MarketTest:testMaturityInsufficientProtocolLiquidity() (gas: 877259)
MarketTest:testMultipleBorrowsForMultipleAssets() (gas: 601619415)
MarketTest:testMultipleDepositsToSmartPool() (gas: 326688)
MarketTest:testMultipleFixedBorrowsRepays() (gas: 755233)
Expand Down Expand Up @@ -148,7 +148,7 @@ PreviewerTest:testFixedPoolsA() (gas: 15067367)
PreviewerTest:testFixedPoolsChangingMaturityInTime() (gas: 1619713)
PreviewerTest:testFixedPoolsRatesAndUtilizations() (gas: 9975909)
PreviewerTest:testFixedPoolsWithFloatingAssetsAverage() (gas: 11938543)
PreviewerTest:testFlexibleAvailableLiquidity() (gas: 13524423)
PreviewerTest:testFlexibleAvailableLiquidity() (gas: 13524564)
PreviewerTest:testFlexibleBorrowSharesAndAssets() (gas: 3491385)
PreviewerTest:testFloatingAvailableLiquidityProjectingNewFloatingDebt() (gas: 9137291)
PreviewerTest:testFloatingRateAndUtilization() (gas: 935395)
Expand Down Expand Up @@ -214,63 +214,64 @@ PriceFeedWrapperTest:testPriceFeedWrapperWithActualOnChainValues() (gas: 30295)
PriceFeedWrapperTest:testPriceFeedWrapperWithNegativePriceShouldRevert() (gas: 42290)
PriceFeedWrapperTest:testPriceFeedWrapperWithUsdPriceFeed() (gas: 1054289)
RewardsControllerTest:testAccrueRewardsForWholeDistributionPeriod() (gas: 1031797)
RewardsControllerTest:testAccrueRewardsWithBadDebtClearingOfFixedBorrow() (gas: 2091484)
RewardsControllerTest:testAccrueRewardsWithBadDebtClearingOfFixedBorrow() (gas: 2091491)
RewardsControllerTest:testAccrueRewardsWithRepayOfBorrowBalance() (gas: 1136747)
RewardsControllerTest:testAccrueRewardsWithRepayOfFixedBorrowBalance() (gas: 1254238)
RewardsControllerTest:testAccrueRewardsWithSeizeOfAllDepositShares() (gas: 1316167)
RewardsControllerTest:testAccrueRewardsWithRepayOfFixedBorrowBalance() (gas: 1254260)
RewardsControllerTest:testAccrueRewardsWithSeizeOfAllDepositShares() (gas: 1316189)
RewardsControllerTest:testAfterDistributionPeriodEnd() (gas: 1531572)
RewardsControllerTest:testAllClaimableUSDCWithAnotherAccountInPool() (gas: 1995086)
RewardsControllerTest:testAllClaimableUSDCWithDeposit() (gas: 1588205)
RewardsControllerTest:testAllClaimableUSDCWithAnotherAccountInPool() (gas: 1995108)
RewardsControllerTest:testAllClaimableUSDCWithDeposit() (gas: 1588138)
RewardsControllerTest:testAllClaimableUSDCWithFloatingBorrow() (gas: 1419648)
RewardsControllerTest:testAllClaimableUSDCWithFloatingRefund() (gas: 1327839)
RewardsControllerTest:testAllClaimableUSDCWithFloatingRefund() (gas: 1327861)
RewardsControllerTest:testAllClaimableUSDCWithFloatingRepay() (gas: 1333191)
RewardsControllerTest:testAllClaimableUSDCWithMint() (gas: 1219409)
RewardsControllerTest:testAllClaimableUSDCWithRedeem() (gas: 1276268)
RewardsControllerTest:testAllClaimableUSDCWithRedeem() (gas: 1276290)
RewardsControllerTest:testAllClaimableUSDCWithTransfer() (gas: 1919524)
RewardsControllerTest:testAllClaimableUSDCWithTransferFrom() (gas: 1792929)
RewardsControllerTest:testAllClaimableUSDCWithTransferFrom() (gas: 1792951)
RewardsControllerTest:testAllClaimableUSDCWithWithdraw() (gas: 1277338)
RewardsControllerTest:testAllClaimableWETH() (gas: 1199935)
RewardsControllerTest:testAllClaimableWithMaturedFixedPool() (gas: 1067312)
RewardsControllerTest:testAllClaimableWETH() (gas: 1199957)
RewardsControllerTest:testAllClaimableWithMaturedFixedPool() (gas: 1067334)
RewardsControllerTest:testAllClaimableWithTimeElapsedZero() (gas: 1200083)
RewardsControllerTest:testClaim() (gas: 1002683)
RewardsControllerTest:testClaim() (gas: 1002712)
RewardsControllerTest:testClaimAll() (gas: 1756424)
RewardsControllerTest:testClaimMarketWithoutRewards() (gas: 939894)
RewardsControllerTest:testClaimWithNotEnabledRewardAsset() (gas: 1040667)
RewardsControllerTest:testClaimMarketWithoutRewards() (gas: 939901)
RewardsControllerTest:testClaimWithNotEnabledRewardAsset() (gas: 1040586)
RewardsControllerTest:testConfigSettingNewStartWithOnGoingDistributionShouldNotUpdate() (gas: 367206)
RewardsControllerTest:testConfigWithDistributionNotYetStartedShouldNotFail() (gas: 497317)
RewardsControllerTest:testConfigWithTransitionFactorHigherOrEqThanCap() (gas: 147139)
RewardsControllerTest:testConfigWithZeroDepositAllocationWeightFactorShouldRevert() (gas: 91969)
RewardsControllerTest:testDifferentDistributionTimeForDifferentRewards() (gas: 1571465)
RewardsControllerTest:testEmitAccrue() (gas: 1024854)
RewardsControllerTest:testEmitClaimRewards() (gas: 919142)
RewardsControllerTest:testEmitClaimRewards() (gas: 919164)
RewardsControllerTest:testEmitConfigUpdate() (gas: 385024)
RewardsControllerTest:testEmitIndexUpdate() (gas: 1092123)
RewardsControllerTest:testEmitIndexUpdate() (gas: 1092145)
RewardsControllerTest:testLastUndistributed() (gas: 1551470)
RewardsControllerTest:testOperationAfterDistributionEnded() (gas: 627356)
RewardsControllerTest:testOperationsBeforeDistributionStart() (gas: 1267595)
RewardsControllerTest:testSetDistributionConfigWithDifferentDecimals() (gas: 9777017)
RewardsControllerTest:testSetDistributionOperationShouldUpdateIndex() (gas: 112897)
RewardsControllerTest:testSetDistributionWithOnGoingMarketOperations() (gas: 972191)
RewardsControllerTest:testSetHigherTotalDistribution() (gas: 1336219)
RewardsControllerTest:testSetLowerAndEqualDistributionPeriodThanCurrentTimestampShouldRevert() (gas: 914080)
RewardsControllerTest:testSetLowerAndEqualTotalDistributionThanReleasedShouldRevert() (gas: 911871)
RewardsControllerTest:testSetDistributionOperationShouldUpdateIndex() (gas: 112809)
RewardsControllerTest:testSetDistributionWithOnGoingMarketOperations() (gas: 972212)
RewardsControllerTest:testSetHigherTotalDistribution() (gas: 1336241)
RewardsControllerTest:testSetLowerAndEqualDistributionPeriodThanCurrentTimestampShouldRevert() (gas: 914102)
RewardsControllerTest:testSetLowerAndEqualTotalDistributionThanReleasedShouldRevert() (gas: 911893)
RewardsControllerTest:testSetLowerDistributionPeriod() (gas: 1563634)
RewardsControllerTest:testSetLowerDistributionPeriodAndLowerTotalDistribution() (gas: 1566403)
RewardsControllerTest:testSetLowerTotalDistribution() (gas: 1336241)
RewardsControllerTest:testSetNewDistributionPeriod() (gas: 2073850)
RewardsControllerTest:testSetLowerTotalDistribution() (gas: 1336154)
RewardsControllerTest:testSetNewDistributionPeriod() (gas: 2073872)
RewardsControllerTest:testSetNewDistributionPeriodAfterDistributionEnds() (gas: 1162065)
RewardsControllerTest:testSetNewTargetDebt() (gas: 1249846)
RewardsControllerTest:testSetNewTargetDebtAfterDistributionEnds() (gas: 1281468)
RewardsControllerTest:testSetNewTargetDebtWithClaimOnlyAtEnd() (gas: 1132357)
RewardsControllerTest:testSetNewTreasuryFeeShouldImpactAllocation() (gas: 514033)
RewardsControllerTest:testSetNewTreasuryFeeShouldImpactAllocation() (gas: 514055)
RewardsControllerTest:testSetTargetDebtMultipleTimes() (gas: 1809851)
RewardsControllerTest:testSetTargetDebtMultipleTimesAfterEnd() (gas: 1814410)
RewardsControllerTest:testSetTotalDistributionMultipleTimes() (gas: 1306096)
RewardsControllerTest:testSetTargetDebtMultipleTimesAfterEnd() (gas: 1814432)
RewardsControllerTest:testSetTotalDistributionMultipleTimes() (gas: 1306118)
RewardsControllerTest:testTriggerHandleBorrowHookBeforeUpdatingFloatingDebt() (gas: 1305161)
RewardsControllerTest:testUpdateConfig() (gas: 1074805)
RewardsControllerTest:testUpdateIndexesWithUtilizationEqualToOne() (gas: 1007733)
RewardsControllerTest:testUpdateIndexesWithUtilizationEqualToOne() (gas: 1007666)
RewardsControllerTest:testUpdateIndexesWithUtilizationHigherThanOne() (gas: 1059213)
RewardsControllerTest:testUpdateWithTotalDebtZeroShouldUpdateLastUndistributed() (gas: 426489)
RewardsControllerTest:testUtilizationEqualZero() (gas: 625070)
RewardsControllerTest:testUtilizationEqualZero() (gas: 625092)
RewardsControllerTest:testWithTwelveFixedPools() (gas: 4020477)
RewardsControllerTest:testWithdrawAllRewardBalance() (gas: 47213)
RewardsControllerTest:testWithdrawOnlyAdminRole() (gas: 84697)
RewardsControllerTest:testWithdrawOnlyAdminRole() (gas: 84719)
5 changes: 3 additions & 2 deletions contracts/Market.sol
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ contract Market is Initializable, AccessControlUpgradeable, PausableUpgradeable,
) external whenNotPaused returns (uint256 borrowShares) {
spendAllowance(borrower, assets);

RewardsController memRewardsController = rewardsController;
if (address(memRewardsController) != address(0)) memRewardsController.handleBorrow(borrower);

depositToTreasury(updateFloatingDebt());

borrowShares = previewBorrow(assets);
Expand All @@ -151,8 +154,6 @@ contract Market is Initializable, AccessControlUpgradeable, PausableUpgradeable,
if (floatingBackupBorrowed + newFloatingDebt > floatingAssets.mulWadDown(1e18 - reserveFactor)) {
revert InsufficientProtocolLiquidity();
}
RewardsController memRewardsController = rewardsController;
if (address(memRewardsController) != address(0)) memRewardsController.handleBorrow(borrower);

totalFloatingBorrowShares += borrowShares;
accounts[borrower].floatingBorrowShares += borrowShares;
Expand Down
20 changes: 20 additions & 0 deletions test/solidity/RewardsController.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,26 @@ contract RewardsControllerTest is Test {
wbtc.approve(address(marketWBTC), type(uint256).max);
}

function testTriggerHandleBorrowHookBeforeUpdatingFloatingDebt() external {
marketWBTC.deposit(1e8, address(this));
marketWBTC.deposit(1e8, ALICE);
auditor.enterMarket(marketWBTC);
vm.prank(ALICE);
auditor.enterMarket(marketWBTC);

marketWETH.deposit(65_000, ALICE);
vm.warp(10_000);
marketWETH.depositAtMaturity(FixedLib.INTERVAL, 20_000, 20_000, address(this));
marketWETH.borrowAtMaturity(FixedLib.INTERVAL, 20_000, 40_000, address(this), address(this));

vm.warp(2694383);
uint256 rewards = rewardsController.allClaimable(address(this), opRewardAsset);
vm.warp(2694384);
vm.prank(ALICE);
marketWETH.borrow(60_008, ALICE, ALICE);
assertGe(rewardsController.allClaimable(address(this), opRewardAsset), rewards);
}

function testAllClaimableUSDCWithDeposit() external {
marketUSDC.deposit(100e6, address(this));
marketUSDC.borrow(30e6, address(this), address(this));
Expand Down

0 comments on commit 953f33f

Please sign in to comment.