Skip to content

Commit

Permalink
馃┕ rewards: adjust calculations' roundings
Browse files Browse the repository at this point in the history
  • Loading branch information
santichez committed Apr 21, 2023
1 parent a27082d commit eef7f82
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 69 deletions.
5 changes: 5 additions & 0 deletions .changeset/ninety-parents-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@exactly-protocol/protocol": patch
---

馃┕ rewards: adjust calculations' roundings
126 changes: 63 additions & 63 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ PreviewerTest:testAccountsWithAccountOnlyDeposit() (gas: 817676)
PreviewerTest:testAccountsWithAccountThatHasBalances() (gas: 1567906)
PreviewerTest:testAccountsWithEmptyAccount() (gas: 669155)
PreviewerTest:testAccountsWithIntermediateOperationsReturningAccurateAmounts() (gas: 13292475)
PreviewerTest:testActualTimeBeforeStartDistributionRewards() (gas: 6625365)
PreviewerTest:testEmptyExactly() (gas: 4918835)
PreviewerTest:testActualTimeBeforeStartDistributionRewards() (gas: 6642904)
PreviewerTest:testEmptyExactly() (gas: 4936259)
PreviewerTest:testExactlyReturningInterestRateModelData() (gas: 665268)
PreviewerTest:testFixedAvailableLiquidityProjectingNewFloatingDebt() (gas: 9567187)
PreviewerTest:testFixedPoolsA() (gas: 15065764)
Expand All @@ -152,7 +152,7 @@ PreviewerTest:testFlexibleAvailableLiquidity() (gas: 13522961)
PreviewerTest:testFlexibleBorrowSharesAndAssets() (gas: 3491385)
PreviewerTest:testFloatingAvailableLiquidityProjectingNewFloatingDebt() (gas: 9135688)
PreviewerTest:testFloatingRateAndUtilization() (gas: 935395)
PreviewerTest:testJustUpdatedRewardRatesShouldStillReturnRate() (gas: 5955831)
PreviewerTest:testJustUpdatedRewardRatesShouldStillReturnRate() (gas: 5973320)
PreviewerTest:testMaxBorrowAssetsCapacity() (gas: 2045904)
PreviewerTest:testMaxBorrowAssetsCapacityForAccountWithShortfall() (gas: 7882657)
PreviewerTest:testMaxBorrowAssetsCapacityPerMarket() (gas: 9307719)
Expand Down Expand Up @@ -198,11 +198,11 @@ PreviewerTest:testPreviewWithdrawAtMaturityWithMaturedMaturity() (gas: 193941)
PreviewerTest:testPreviewWithdrawAtMaturityWithOneUnit() (gas: 202895)
PreviewerTest:testPreviewWithdrawAtMaturityWithSameTimestamp() (gas: 193846)
PreviewerTest:testPreviewWithdrawAtMaturityWithZeroAmount() (gas: 202941)
PreviewerTest:testReturnRewardAssetUsdPrice() (gas: 5986936)
PreviewerTest:testRewardsRate() (gas: 6743017)
PreviewerTest:testRewardsRateAfterDistributionEnd() (gas: 6116309)
PreviewerTest:testRewardsRateOnlyWithFixedBorrows() (gas: 5873854)
PreviewerTest:testRewardsRateWithMarketWithDifferentDecimals() (gas: 14164995)
PreviewerTest:testReturnRewardAssetUsdPrice() (gas: 6004339)
PreviewerTest:testRewardsRate() (gas: 6760518)
PreviewerTest:testRewardsRateAfterDistributionEnd() (gas: 6133837)
PreviewerTest:testRewardsRateOnlyWithFixedBorrows() (gas: 5891320)
PreviewerTest:testRewardsRateWithMarketWithDifferentDecimals() (gas: 14182496)
PriceFeedDoubleTest:testPriceFeedDoubleReturningAccurateDecimals() (gas: 433282)
PriceFeedDoubleTest:testPriceFeedDoubleReturningPrice() (gas: 26143)
PriceFeedDoubleTest:testPriceFeedDoubleWithActualOnChainValues() (gas: 30976)
Expand All @@ -213,65 +213,65 @@ PriceFeedWrapperTest:testPriceFeedWrapperReturningPriceAfterRebase() (gas: 22863
PriceFeedWrapperTest:testPriceFeedWrapperWithActualOnChainValues() (gas: 30295)
PriceFeedWrapperTest:testPriceFeedWrapperWithNegativePriceShouldRevert() (gas: 42290)
PriceFeedWrapperTest:testPriceFeedWrapperWithUsdPriceFeed() (gas: 1054289)
RewardsControllerTest:testAccrueRewardsForWholeDistributionPeriod() (gas: 1022663)
RewardsControllerTest:testAccrueRewardsWithBadDebtClearingOfFixedBorrow() (gas: 2086409)
RewardsControllerTest:testAccrueRewardsWithRepayOfBorrowBalance() (gas: 1131376)
RewardsControllerTest:testAccrueRewardsWithRepayOfFixedBorrowBalance() (gas: 1249140)
RewardsControllerTest:testAccrueRewardsWithSeizeOfAllDepositShares() (gas: 1311417)
RewardsControllerTest:testAfterDistributionPeriodEnd() (gas: 1517940)
RewardsControllerTest:testAllClaimableUSDCWithAnotherAccountInPool() (gas: 1972596)
RewardsControllerTest:testAllClaimableUSDCWithDeposit() (gas: 1572042)
RewardsControllerTest:testAllClaimableUSDCWithFloatingBorrow() (gas: 1408752)
RewardsControllerTest:testAllClaimableUSDCWithFloatingRefund() (gas: 1316376)
RewardsControllerTest:testAllClaimableUSDCWithFloatingRepay() (gas: 1321706)
RewardsControllerTest:testAllClaimableUSDCWithMint() (gas: 1208513)
RewardsControllerTest:testAllClaimableUSDCWithRedeem() (gas: 1264596)
RewardsControllerTest:testAllClaimableUSDCWithTransfer() (gas: 1900542)
RewardsControllerTest:testAllClaimableUSDCWithTransferFrom() (gas: 1778466)
RewardsControllerTest:testAllClaimableUSDCWithWithdraw() (gas: 1265644)
RewardsControllerTest:testAllClaimableWETH() (gas: 1186226)
RewardsControllerTest:testAllClaimableWithMaturedFixedPool() (gas: 1055593)
RewardsControllerTest:testAllClaimableWithTimeElapsedZero() (gas: 1185566)
RewardsControllerTest:testClaim() (gas: 990429)
RewardsControllerTest:testClaimAll() (gas: 1733933)
RewardsControllerTest:testClaimMarketWithoutRewards() (gas: 938771)
RewardsControllerTest:testClaimWithNotEnabledRewardAsset() (gas: 1026672)
RewardsControllerTest:testConfigSettingNewStartWithOnGoingDistributionShouldNotUpdate() (gas: 363872)
RewardsControllerTest:testAccrueRewardsForWholeDistributionPeriod() (gas: 1022708)
RewardsControllerTest:testAccrueRewardsWithBadDebtClearingOfFixedBorrow() (gas: 2086479)
RewardsControllerTest:testAccrueRewardsWithRepayOfBorrowBalance() (gas: 1131503)
RewardsControllerTest:testAccrueRewardsWithRepayOfFixedBorrowBalance() (gas: 1249247)
RewardsControllerTest:testAccrueRewardsWithSeizeOfAllDepositShares() (gas: 1311504)
RewardsControllerTest:testAfterDistributionPeriodEnd() (gas: 1518167)
RewardsControllerTest:testAllClaimableUSDCWithAnotherAccountInPool() (gas: 1972900)
RewardsControllerTest:testAllClaimableUSDCWithDeposit() (gas: 1572198)
RewardsControllerTest:testAllClaimableUSDCWithFloatingBorrow() (gas: 1408856)
RewardsControllerTest:testAllClaimableUSDCWithFloatingRefund() (gas: 1316454)
RewardsControllerTest:testAllClaimableUSDCWithFloatingRepay() (gas: 1321784)
RewardsControllerTest:testAllClaimableUSDCWithMint() (gas: 1208617)
RewardsControllerTest:testAllClaimableUSDCWithRedeem() (gas: 1264674)
RewardsControllerTest:testAllClaimableUSDCWithTransfer() (gas: 1900698)
RewardsControllerTest:testAllClaimableUSDCWithTransferFrom() (gas: 1778596)
RewardsControllerTest:testAllClaimableUSDCWithWithdraw() (gas: 1265722)
RewardsControllerTest:testAllClaimableWETH() (gas: 1186421)
RewardsControllerTest:testAllClaimableWithMaturedFixedPool() (gas: 1055810)
RewardsControllerTest:testAllClaimableWithTimeElapsedZero() (gas: 1185684)
RewardsControllerTest:testClaim() (gas: 990497)
RewardsControllerTest:testClaimAll() (gas: 1734176)
RewardsControllerTest:testClaimMarketWithoutRewards() (gas: 938761)
RewardsControllerTest:testClaimWithNotEnabledRewardAsset() (gas: 1026769)
RewardsControllerTest:testConfigSettingNewStartWithOnGoingDistributionShouldNotUpdate() (gas: 363862)
RewardsControllerTest:testConfigWithDistributionNotYetStartedShouldNotFail() (gas: 495730)
RewardsControllerTest:testConfigWithTransitionFactorHigherOrEqThanCap() (gas: 49747)
RewardsControllerTest:testConfigWithZeroDepositAllocationWeightFactorShouldRevert() (gas: 42868)
RewardsControllerTest:testDifferentDistributionTimeForDifferentRewards() (gas: 1562352)
RewardsControllerTest:testEmitAccrue() (gas: 1016103)
RewardsControllerTest:testEmitClaimRewards() (gas: 910030)
RewardsControllerTest:testDifferentDistributionTimeForDifferentRewards() (gas: 1562361)
RewardsControllerTest:testEmitAccrue() (gas: 1016135)
RewardsControllerTest:testEmitClaimRewards() (gas: 910036)
RewardsControllerTest:testEmitConfigUpdate() (gas: 382702)
RewardsControllerTest:testEmitIndexUpdate() (gas: 1077910)
RewardsControllerTest:testLastUndistributed() (gas: 1524435)
RewardsControllerTest:testOperationAfterDistributionEnded() (gas: 625221)
RewardsControllerTest:testOperationsBeforeDistributionStart() (gas: 1254933)
RewardsControllerTest:testSetDistributionConfigWithDifferentDecimals() (gas: 9745909)
RewardsControllerTest:testEmitIndexUpdate() (gas: 1077922)
RewardsControllerTest:testLastUndistributed() (gas: 1524522)
RewardsControllerTest:testOperationAfterDistributionEnded() (gas: 625263)
RewardsControllerTest:testOperationsBeforeDistributionStart() (gas: 1254958)
RewardsControllerTest:testSetDistributionConfigWithDifferentDecimals() (gas: 9746335)
RewardsControllerTest:testSetDistributionOperationShouldUpdateIndex() (gas: 111648)
RewardsControllerTest:testSetDistributionWithOnGoingMarketOperations() (gas: 965311)
RewardsControllerTest:testSetHigherTotalDistribution() (gas: 1321571)
RewardsControllerTest:testSetLowerAndEqualDistributionPeriodThanCurrentTimestampShouldRevert() (gas: 911152)
RewardsControllerTest:testSetLowerAndEqualTotalDistributionThanReleasedShouldRevert() (gas: 908943)
RewardsControllerTest:testSetLowerDistributionPeriod() (gas: 1542667)
RewardsControllerTest:testSetLowerDistributionPeriodAndLowerTotalDistribution() (gas: 1545436)
RewardsControllerTest:testSetLowerTotalDistribution() (gas: 1321484)
RewardsControllerTest:testSetNewDistributionPeriod() (gas: 2046608)
RewardsControllerTest:testSetNewDistributionPeriodAfterDistributionEnds() (gas: 1151228)
RewardsControllerTest:testSetNewTargetDebt() (gas: 1235527)
RewardsControllerTest:testSetNewTargetDebtAfterDistributionEnds() (gas: 1267653)
RewardsControllerTest:testSetNewTargetDebtWithClaimOnlyAtEnd() (gas: 1124335)
RewardsControllerTest:testSetNewTreasuryFeeShouldImpactAllocation() (gas: 513116)
RewardsControllerTest:testSetTargetDebtMultipleTimes() (gas: 1792778)
RewardsControllerTest:testSetTargetDebtMultipleTimesAfterEnd() (gas: 1798871)
RewardsControllerTest:testSetTotalDistributionMultipleTimes() (gas: 1295822)
RewardsControllerTest:testTriggerHandleBorrowHookBeforeUpdatingFloatingDebt() (gas: 1300639)
RewardsControllerTest:testUpdateConfig() (gas: 1064885)
RewardsControllerTest:testUpdateIndexesWithUtilizationEqualToOne() (gas: 1001746)
RewardsControllerTest:testUpdateIndexesWithUtilizationHigherThanOne() (gas: 1053293)
RewardsControllerTest:testUpdateWithTotalDebtZeroShouldUpdateLastUndistributed() (gas: 426023)
RewardsControllerTest:testUtilizationEqualZero() (gas: 622146)
RewardsControllerTest:testWithTwelveFixedPools() (gas: 3984566)
RewardsControllerTest:testSetDistributionWithOnGoingMarketOperations() (gas: 965366)
RewardsControllerTest:testSetHigherTotalDistribution() (gas: 1321749)
RewardsControllerTest:testSetLowerAndEqualDistributionPeriodThanCurrentTimestampShouldRevert() (gas: 911214)
RewardsControllerTest:testSetLowerAndEqualTotalDistributionThanReleasedShouldRevert() (gas: 909005)
RewardsControllerTest:testSetLowerDistributionPeriod() (gas: 1542888)
RewardsControllerTest:testSetLowerDistributionPeriodAndLowerTotalDistribution() (gas: 1545657)
RewardsControllerTest:testSetLowerTotalDistribution() (gas: 1321662)
RewardsControllerTest:testSetNewDistributionPeriod() (gas: 2046989)
RewardsControllerTest:testSetNewDistributionPeriodAfterDistributionEnds() (gas: 1151363)
RewardsControllerTest:testSetNewTargetDebt() (gas: 1235591)
RewardsControllerTest:testSetNewTargetDebtAfterDistributionEnds() (gas: 1267824)
RewardsControllerTest:testSetNewTargetDebtWithClaimOnlyAtEnd() (gas: 1124381)
RewardsControllerTest:testSetNewTreasuryFeeShouldImpactAllocation() (gas: 513142)
RewardsControllerTest:testSetTargetDebtMultipleTimes() (gas: 1792982)
RewardsControllerTest:testSetTargetDebtMultipleTimesAfterEnd() (gas: 1799114)
RewardsControllerTest:testSetTotalDistributionMultipleTimes() (gas: 1295927)
RewardsControllerTest:testTriggerHandleBorrowHookBeforeUpdatingFloatingDebt() (gas: 1300567)
RewardsControllerTest:testUpdateConfig() (gas: 1064881)
RewardsControllerTest:testUpdateIndexesWithUtilizationEqualToOne() (gas: 1001729)
RewardsControllerTest:testUpdateIndexesWithUtilizationHigherThanOne() (gas: 1053276)
RewardsControllerTest:testUpdateWithTotalDebtZeroShouldUpdateLastUndistributed() (gas: 425993)
RewardsControllerTest:testUtilizationEqualZero() (gas: 622192)
RewardsControllerTest:testWithTwelveFixedPools() (gas: 3984848)
RewardsControllerTest:testWithdrawAllRewardBalance() (gas: 47213)
RewardsControllerTest:testWithdrawOnlyAdminRole() (gas: 84719)
14 changes: 8 additions & 6 deletions contracts/RewardsController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -493,21 +493,23 @@ contract RewardsController is Initializable, AccessControlUpgradeable {
uint256 releaseRate = rewardData.releaseRate;
uint256 lastUndistributed = rewardData.lastUndistributed;
t.period = t.end - t.start;
uint256 distributionFactor = t.period > 0 ? rewardData.undistributedFactor.mulWadDown(target) / t.period : 0;
uint256 distributionFactor = t.period > 0
? rewardData.undistributedFactor.mulDivDown(target, t.period * 1e18)
: 0;
if (block.timestamp <= t.end) {
if (distributionFactor > 0) {
uint256 exponential = uint256((-int256(distributionFactor * deltaTime)).expWad());
newUndistributed =
lastUndistributed.mulWadDown(exponential) +
releaseRate.mulDivDown(1e18 - target, distributionFactor).mulWadDown(1e18 - exponential);
releaseRate.mulDivDown(1e18 - target, distributionFactor).mulWadUp(1e18 - exponential);
} else {
newUndistributed = lastUndistributed + releaseRate.mulWadDown(1e18 - target) * deltaTime;
}
rewards = uint256(int256(releaseRate * deltaTime) - (int256(newUndistributed) - int256(lastUndistributed)));
} else if (rewardData.lastUpdate > t.end) {
newUndistributed =
lastUndistributed -
lastUndistributed.mulWadDown(1e18 - uint256((-int256(distributionFactor * deltaTime)).expWad()));
lastUndistributed.mulWadUp(1e18 - uint256((-int256(distributionFactor * deltaTime)).expWad()));
rewards = uint256(-(int256(newUndistributed) - int256(lastUndistributed)));
} else {
uint256 exponential;
Expand All @@ -516,12 +518,12 @@ contract RewardsController is Initializable, AccessControlUpgradeable {
exponential = uint256((-int256(distributionFactor * deltaTime)).expWad());
newUndistributed =
lastUndistributed.mulWadDown(exponential) +
releaseRate.mulDivDown(1e18 - target, distributionFactor).mulWadDown(1e18 - exponential);
releaseRate.mulDivDown(1e18 - target, distributionFactor).mulWadUp(1e18 - exponential);
} else {
newUndistributed = lastUndistributed + releaseRate.mulWadDown(1e18 - target) * deltaTime;
}
exponential = uint256((-int256(distributionFactor * (block.timestamp - t.end))).expWad());
newUndistributed = newUndistributed - newUndistributed.mulWadDown(1e18 - exponential);
newUndistributed = newUndistributed - newUndistributed.mulWadUp(1e18 - exponential);
rewards = uint256(int256(releaseRate * deltaTime) - (int256(newUndistributed) - int256(lastUndistributed)));
}
if (rewards == 0) return (rewardData.borrowIndex, rewardData.depositIndex, newUndistributed);
Expand All @@ -546,7 +548,7 @@ contract RewardsController is Initializable, AccessControlUpgradeable {
.compensationFactor
.mulWadDown(
market.interestRateModel().floatingRate(v.utilization).mulWadDown(
1e18 - v.utilization.mulWadDown(1e18 - market.treasuryFeeRate())
1e18 - v.utilization.mulWadUp(1e18 - market.treasuryFeeRate())
) + v.borrowAllocationWeightFactor
)
.mulWadDown(1e18 - v.sigmoid);
Expand Down

0 comments on commit eef7f82

Please sign in to comment.