Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 rewards: fix released calculation #725

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/moody-beds-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@exactly/protocol": patch
---

🐛 rewards: fix released calculation
101 changes: 51 additions & 50 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -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, μ: 6508761, ~: 6529787)
DebtManagerTest:testFuzzRolls(uint8[4],uint8[4],uint256[4],uint40[4],uint8[4]) (runs: 256, μ: 6504217, ~: 6529787)
DebtManagerTest:testLateFixedRoll() (gas: 1310831)
DebtManagerTest:testLateFixedRollWithThreeLoops() (gas: 1897545)
DebtManagerTest:testLateFixedToFloatingRoll() (gas: 1273784)
Expand Down Expand Up @@ -141,11 +141,11 @@ InterestRateModelTest:testFixedBorrowRate() (gas: 2052054)
InterestRateModelTest:testFixedRateRevertAlreadyMatured() (gas: 2046236)
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, μ: 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: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, μ: 9970772, ~: 10137385)
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: 499440)
Expand Down Expand Up @@ -284,7 +284,7 @@ PoolLibTest:testAtomicDepositBorrowRepayWithdraw() (gas: 46018)
PoolLibTest:testBackupBorrow() (gas: 33676)
PoolLibTest:testEarningsAccrual() (gas: 38999)
PoolLibTest:testEarningsDistribution() (gas: 32640)
PoolLibTest:testFuzzAddRemoveAll(uint8[12]) (runs: 256, μ: 55244, ~: 54164)
PoolLibTest:testFuzzAddRemoveAll(uint8[12]) (runs: 256, μ: 55186, ~: 54164)
PoolLibTest:testMaturityRangeLimit() (gas: 7873)
PoolLibTest:testMaturityRangeTooWide() (gas: 6831)
PreviewerTest:testAccountsReturningAccurateAmounts() (gas: 1393698)
Expand All @@ -293,8 +293,8 @@ PreviewerTest:testAccountsWithAccountOnlyDeposit() (gas: 862391)
PreviewerTest:testAccountsWithAccountThatHasBalances() (gas: 2265026)
PreviewerTest:testAccountsWithEmptyAccount() (gas: 690558)
PreviewerTest:testAccountsWithIntermediateOperationsReturningAccurateAmounts() (gas: 17617503)
PreviewerTest:testActualTimeBeforeStartDistributionRewards() (gas: 7753427)
PreviewerTest:testEmptyExactly() (gas: 5645469)
PreviewerTest:testActualTimeBeforeStartDistributionRewards() (gas: 7759515)
PreviewerTest:testEmptyExactly() (gas: 5651562)
PreviewerTest:testExactlyReturningInterestRateModelData() (gas: 688149)
PreviewerTest:testFixedAvailableLiquidityProjectingNewFloatingDebt() (gas: 13301596)
PreviewerTest:testFixedPoolsA() (gas: 19318341)
Expand All @@ -305,7 +305,7 @@ PreviewerTest:testFlexibleAvailableLiquidity() (gas: 17242437)
PreviewerTest:testFlexibleBorrowSharesAndAssets() (gas: 4401038)
PreviewerTest:testFloatingAvailableLiquidityProjectingNewFloatingDebt() (gas: 12554334)
PreviewerTest:testFloatingRateAndUtilization() (gas: 1128246)
PreviewerTest:testJustUpdatedRewardRatesShouldStillReturnRate() (gas: 7174821)
PreviewerTest:testJustUpdatedRewardRatesShouldStillReturnRate() (gas: 7180914)
PreviewerTest:testMaxBorrowAssetsCapacity() (gas: 2469700)
PreviewerTest:testMaxBorrowAssetsCapacityForAccountWithShortfall() (gas: 10991846)
PreviewerTest:testMaxBorrowAssetsCapacityPerMarket() (gas: 13181978)
Expand Down Expand Up @@ -352,12 +352,12 @@ PreviewerTest:testPreviewWithdrawAtMaturityWithOneUnit() (gas: 251718)
PreviewerTest:testPreviewWithdrawAtMaturityWithSameTimestamp() (gas: 233406)
PreviewerTest:testPreviewWithdrawAtMaturityWithZeroAmount() (gas: 251675)
PreviewerTest:testReserveFactor() (gas: 707280)
PreviewerTest:testReturnRewardAssetUsdPrice() (gas: 6697322)
PreviewerTest:testRewardsRateAfterDistributionEnd() (gas: 7481122)
PreviewerTest:testRewardsRateOnlyWithFixedBorrows() (gas: 6788297)
PreviewerTest:testRewardsRateWithDifferentRewardLengths() (gas: 19241268)
PreviewerTest:testRewardsRateWithMarketWithDifferentDecimals() (gas: 18372292)
PreviewerTest:testRewardsRateX() (gas: 8127662)
PreviewerTest:testReturnRewardAssetUsdPrice() (gas: 6703410)
PreviewerTest:testRewardsRateAfterDistributionEnd() (gas: 7487215)
PreviewerTest:testRewardsRateOnlyWithFixedBorrows() (gas: 6794390)
PreviewerTest:testRewardsRateWithDifferentRewardLengths() (gas: 19247356)
PreviewerTest:testRewardsRateWithMarketWithDifferentDecimals() (gas: 18378380)
PreviewerTest:testRewardsRateX() (gas: 8133750)
PriceFeedDoubleTest:testPriceFeedDoubleReturningAccurateDecimals() (gas: 597567)
PriceFeedDoubleTest:testPriceFeedDoubleReturningPrice() (gas: 53190)
PriceFeedDoubleTest:testPriceFeedDoubleWithActualOnChainValues() (gas: 76310)
Expand All @@ -374,12 +374,12 @@ PriceFeedWrapperTest:testPriceFeedWrapperReturningPriceAfterRebase() (gas: 48989
PriceFeedWrapperTest:testPriceFeedWrapperWithActualOnChainValues() (gas: 75210)
PriceFeedWrapperTest:testPriceFeedWrapperWithNegativePriceShouldRevert() (gas: 164216)
PriceFeedWrapperTest:testPriceFeedWrapperWithUsdPriceFeed() (gas: 1243191)
RewardsControllerTest:testAccrueRewardsForWholeDistributionPeriod() (gas: 1245256)
RewardsControllerTest:testAccrueRewardsForWholeDistributionPeriod() (gas: 1245252)
RewardsControllerTest:testAccrueRewardsWithBadDebtClearingOfFixedBorrow() (gas: 3338579)
RewardsControllerTest:testAccrueRewardsWithRepayOfBorrowBalance() (gas: 1602527)
RewardsControllerTest:testAccrueRewardsWithRepayOfFixedBorrowBalance() (gas: 1791601)
RewardsControllerTest:testAccrueRewardsWithSeizeOfAllDepositShares() (gas: 1991092)
RewardsControllerTest:testAfterDistributionPeriodEnd() (gas: 1818279)
RewardsControllerTest:testAfterDistributionPeriodEnd() (gas: 1818275)
RewardsControllerTest:testAllClaimableUSDCWithAnotherAccountInPool() (gas: 2273354)
RewardsControllerTest:testAllClaimableUSDCWithDeposit() (gas: 1627706)
RewardsControllerTest:testAllClaimableUSDCWithFloatingBorrow() (gas: 1560044)
Expand All @@ -392,49 +392,50 @@ RewardsControllerTest:testAllClaimableUSDCWithTransferFrom() (gas: 2135247)
RewardsControllerTest:testAllClaimableUSDCWithWithdraw() (gas: 1641434)
RewardsControllerTest:testAllClaimableWETH() (gas: 1247789)
RewardsControllerTest:testAllClaimableWithMaturedFixedPool() (gas: 1127875)
RewardsControllerTest:testAllClaimableWithTimeElapsedZero() (gas: 1624639)
RewardsControllerTest:testClaim() (gas: 1192161)
RewardsControllerTest:testClaimAll() (gas: 2188738)
RewardsControllerTest:testClaimMarketWithoutRewards() (gas: 1240983)
RewardsControllerTest:testClaimWithNotEnabledRewardAsset() (gas: 1222637)
RewardsControllerTest:testConfigSettingNewStartWithOnGoingDistributionShouldNotUpdate() (gas: 430182)
RewardsControllerTest:testAllClaimableWithTimeElapsedZero() (gas: 1624635)
RewardsControllerTest:testAllRewards() (gas: 216384)
RewardsControllerTest:testClaim() (gas: 1192157)
RewardsControllerTest:testClaimAll() (gas: 2188734)
RewardsControllerTest:testClaimMarketWithoutRewards() (gas: 1240979)
RewardsControllerTest:testClaimWithNotEnabledRewardAsset() (gas: 1222633)
RewardsControllerTest:testConfigSettingNewStartWithOnGoingDistributionShouldNotUpdate() (gas: 430275)
RewardsControllerTest:testConfigWithDistributionNotYetStartedShouldNotFail() (gas: 613404)
RewardsControllerTest:testConfigWithTransitionFactorHigherOrEqThanCap() (gas: 107189)
RewardsControllerTest:testConfigWithZeroDepositAllocationWeightFactorShouldRevert() (gas: 71542)
RewardsControllerTest:testDifferentDistributionTimeForDifferentRewards() (gas: 2025649)
RewardsControllerTest:testEmitAccrue() (gas: 1317916)
RewardsControllerTest:testEmitClaimRewards() (gas: 1112375)
RewardsControllerTest:testEmitAccrue() (gas: 1317912)
RewardsControllerTest:testEmitClaimRewards() (gas: 1112371)
RewardsControllerTest:testEmitConfigUpdate() (gas: 439567)
RewardsControllerTest:testEmitIndexUpdate() (gas: 1445530)
RewardsControllerTest:testLastUndistributed() (gas: 2189661)
RewardsControllerTest:testEmitIndexUpdate() (gas: 1445522)
RewardsControllerTest:testLastUndistributed() (gas: 2189649)
RewardsControllerTest:testOperationAfterDistributionEnded() (gas: 722976)
RewardsControllerTest:testOperationsBeforeDistributionStart() (gas: 1674576)
RewardsControllerTest:testPermitClaim() (gas: 1275282)
RewardsControllerTest:testSetDistributionConfigWithDifferentDecimals() (gas: 11445059)
RewardsControllerTest:testOperationsBeforeDistributionStart() (gas: 1674572)
RewardsControllerTest:testPermitClaim() (gas: 1275278)
RewardsControllerTest:testSetDistributionConfigWithDifferentDecimals() (gas: 11445055)
RewardsControllerTest:testSetDistributionOperationShouldUpdateIndex() (gas: 136200)
RewardsControllerTest:testSetDistributionWithOnGoingMarketOperations() (gas: 1202358)
RewardsControllerTest:testSetHigherTotalDistribution() (gas: 1831201)
RewardsControllerTest:testSetLowerAndEqualDistributionPeriodThanCurrentTimestampShouldRevert() (gas: 1274696)
RewardsControllerTest:testSetLowerAndEqualTotalDistributionThanReleasedShouldRevert() (gas: 1267721)
RewardsControllerTest:testSetLowerDistributionPeriod() (gas: 2284176)
RewardsControllerTest:testSetLowerDistributionPeriodAndLowerTotalDistribution() (gas: 2286901)
RewardsControllerTest:testSetLowerTotalDistribution() (gas: 1831114)
RewardsControllerTest:testSetNewDistributionPeriod() (gas: 3143972)
RewardsControllerTest:testSetNewDistributionPeriodAfterDistributionEnds() (gas: 1406666)
RewardsControllerTest:testSetNewTargetDebt() (gas: 1671525)
RewardsControllerTest:testSetNewTargetDebtAfterDistributionEnds() (gas: 1734966)
RewardsControllerTest:testSetNewTargetDebtWithClaimOnlyAtEnd() (gas: 1389661)
RewardsControllerTest:testSetDistributionWithOnGoingMarketOperations() (gas: 1202354)
RewardsControllerTest:testSetHigherTotalDistribution() (gas: 1831286)
RewardsControllerTest:testSetLowerAndEqualDistributionPeriodThanCurrentTimestampShouldRevert() (gas: 1274975)
RewardsControllerTest:testSetLowerAndEqualTotalDistributionThanReleasedShouldRevert() (gas: 1268000)
RewardsControllerTest:testSetLowerDistributionPeriod() (gas: 2284257)
RewardsControllerTest:testSetLowerDistributionPeriodAndLowerTotalDistribution() (gas: 2286982)
RewardsControllerTest:testSetLowerTotalDistribution() (gas: 1831199)
RewardsControllerTest:testSetNewDistributionPeriod() (gas: 3144049)
RewardsControllerTest:testSetNewDistributionPeriodAfterDistributionEnds() (gas: 1406662)
RewardsControllerTest:testSetNewTargetDebt() (gas: 1671610)
RewardsControllerTest:testSetNewTargetDebtAfterDistributionEnds() (gas: 1734958)
RewardsControllerTest:testSetNewTargetDebtWithClaimOnlyAtEnd() (gas: 1389750)
RewardsControllerTest:testSetNewTreasuryFeeShouldImpactAllocation() (gas: 658882)
RewardsControllerTest:testSetTargetDebtMultipleTimes() (gas: 2719324)
RewardsControllerTest:testSetTargetDebtMultipleTimesAfterEnd() (gas: 2756094)
RewardsControllerTest:testSetTotalDistributionMultipleTimes() (gas: 1838222)
RewardsControllerTest:testSetTargetDebtMultipleTimes() (gas: 2719569)
RewardsControllerTest:testSetTargetDebtMultipleTimesAfterEnd() (gas: 2756086)
RewardsControllerTest:testSetTotalDistributionMultipleTimes() (gas: 1838471)
RewardsControllerTest:testTriggerHandleBorrowHookBeforeUpdatingFloatingDebt() (gas: 1879339)
RewardsControllerTest:testUpdateConfig() (gas: 1328855)
RewardsControllerTest:testUpdateIndexesWithUtilizationEqualToOne() (gas: 1257771)
RewardsControllerTest:testUpdateIndexesWithUtilizationHigherThanOne() (gas: 1352512)
RewardsControllerTest:testUpdateConfig() (gas: 1328944)
RewardsControllerTest:testUpdateIndexesWithUtilizationEqualToOne() (gas: 1257767)
RewardsControllerTest:testUpdateIndexesWithUtilizationHigherThanOne() (gas: 1352508)
RewardsControllerTest:testUpdateWithTotalDebtZeroShouldUpdateLastUndistributed() (gas: 575506)
RewardsControllerTest:testUtilizationEqualZero() (gas: 921863)
RewardsControllerTest:testWithTwelveFixedPools() (gas: 8055024)
RewardsControllerTest:testWithTwelveFixedPools() (gas: 8055020)
RewardsControllerTest:testWithdrawAllRewardBalance() (gas: 71913)
RewardsControllerTest:testWithdrawOnlyAdminRole() (gas: 122309)
SwapperTest:testSwapBasic() (gas: 216831)
Expand Down
2 changes: 1 addition & 1 deletion contracts/RewardsController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ contract RewardsController is Initializable, AccessControlUpgradeable {
released =
rewardData.lastConfigReleased +
rewardData.releaseRate *
(block.timestamp - rewardData.lastConfig);
(block.timestamp - Math.max(rewardData.lastConfig, start));
elapsed = block.timestamp - start;
if (configs[i].totalDistribution <= released || configs[i].distributionPeriod <= elapsed) {
revert InvalidConfig();
Expand Down
7 changes: 7 additions & 0 deletions test/RewardsController.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,13 @@ contract RewardsControllerTest is Test {
assertEq(opRewardAsset.balanceOf(address(rewardsController)), 0);
}

function testAllRewards() external {
marketWETH.deposit(1 ether, address(this));
ERC20[] memory rewards = rewardsController.allRewards();

assertEq(address(rewards[0]), address(opRewardAsset));
}

function testEmitClaimRewards() external {
marketWETH.deposit(100 ether, address(this));
marketWETH.borrow(20 ether, address(this), address(this));
Expand Down
6 changes: 6 additions & 0 deletions test/hardhat/19_rewards_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ describe("RewardsController", function () {
expect(claimedBalance).to.be.greaterThan(0);
});

it("THEN allRewards return op address", async () => {
const rewards = await rewardsController.allRewards();

expect(rewards[0]).to.be.eq(op.target);
});

it("AND trying to claim with invalid market THEN the claimable amount is 0", async () => {
const marketOps = [{ market: alice.address, operations: [false, true] }];
const claimableBalance = await rewardsController.claimable(marketOps, alice.address, op.target);
Expand Down