Skip to content

Commit

Permalink
⚡️ rewards: reusing memory pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
santichez committed Apr 21, 2023
1 parent ecd08e8 commit e17f162
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 83 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-news-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@exactly-protocol/protocol": patch
---

⚡️ rewards: reusing memory pointers
128 changes: 64 additions & 64 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: 13294487)
PreviewerTest:testActualTimeBeforeStartDistributionRewards() (gas: 6741390)
PreviewerTest:testEmptyExactly() (gas: 5033079)
PreviewerTest:testActualTimeBeforeStartDistributionRewards() (gas: 6654182)
PreviewerTest:testEmptyExactly() (gas: 4946132)
PreviewerTest:testExactlyReturningInterestRateModelData() (gas: 665268)
PreviewerTest:testFixedAvailableLiquidityProjectingNewFloatingDebt() (gas: 9569199)
PreviewerTest:testFixedPoolsA() (gas: 15067776)
Expand All @@ -152,7 +152,7 @@ PreviewerTest:testFlexibleAvailableLiquidity() (gas: 13524973)
PreviewerTest:testFlexibleBorrowSharesAndAssets() (gas: 3491385)
PreviewerTest:testFloatingAvailableLiquidityProjectingNewFloatingDebt() (gas: 9137700)
PreviewerTest:testFloatingRateAndUtilization() (gas: 935395)
PreviewerTest:testJustUpdatedRewardRatesShouldStillReturnRate() (gas: 6071501)
PreviewerTest:testJustUpdatedRewardRatesShouldStillReturnRate() (gas: 5984181)
PreviewerTest:testMaxBorrowAssetsCapacity() (gas: 2045904)
PreviewerTest:testMaxBorrowAssetsCapacityForAccountWithShortfall() (gas: 7884676)
PreviewerTest:testMaxBorrowAssetsCapacityPerMarket() (gas: 9309731)
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: 6101335)
PreviewerTest:testRewardsRate() (gas: 6860244)
PreviewerTest:testRewardsRateAfterDistributionEnd() (gas: 6231477)
PreviewerTest:testRewardsRateOnlyWithFixedBorrows() (gas: 5988973)
PreviewerTest:testRewardsRateWithMarketWithDifferentDecimals() (gas: 14284234)
PreviewerTest:testReturnRewardAssetUsdPrice() (gas: 6014349)
PreviewerTest:testRewardsRate() (gas: 6772673)
PreviewerTest:testRewardsRateAfterDistributionEnd() (gas: 6143957)
PreviewerTest:testRewardsRateOnlyWithFixedBorrows() (gas: 5901853)
PreviewerTest:testRewardsRateWithMarketWithDifferentDecimals() (gas: 14196663)
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: 1027423)
RewardsControllerTest:testAccrueRewardsWithBadDebtClearingOfFixedBorrow() (gas: 2088721)
RewardsControllerTest:testAccrueRewardsWithRepayOfBorrowBalance() (gas: 1133752)
RewardsControllerTest:testAccrueRewardsWithRepayOfFixedBorrowBalance() (gas: 1251392)
RewardsControllerTest:testAccrueRewardsWithSeizeOfAllDepositShares() (gas: 1313393)
RewardsControllerTest:testAfterDistributionPeriodEnd() (gas: 1524504)
RewardsControllerTest:testAllClaimableUSDCWithAnotherAccountInPool() (gas: 1983232)
RewardsControllerTest:testAllClaimableUSDCWithDeposit() (gas: 1579808)
RewardsControllerTest:testAllClaimableUSDCWithFloatingBorrow() (gas: 1414012)
RewardsControllerTest:testAllClaimableUSDCWithFloatingRefund() (gas: 1321236)
RewardsControllerTest:testAllClaimableUSDCWithFloatingRepay() (gas: 1326566)
RewardsControllerTest:testAllClaimableUSDCWithMint() (gas: 1213773)
RewardsControllerTest:testAllClaimableUSDCWithRedeem() (gas: 1269580)
RewardsControllerTest:testAllClaimableUSDCWithTransfer() (gas: 1909129)
RewardsControllerTest:testAllClaimableUSDCWithTransferFrom() (gas: 1785250)
RewardsControllerTest:testAllClaimableUSDCWithWithdraw() (gas: 1270628)
RewardsControllerTest:testAllClaimableWETH() (gas: 1191762)
RewardsControllerTest:testAllClaimableWithMaturedFixedPool() (gas: 1060351)
RewardsControllerTest:testAllClaimableWithTimeElapsedZero() (gas: 1192509)
RewardsControllerTest:testClaim() (gas: 996018)
RewardsControllerTest:testClaimAll() (gas: 1744265)
RewardsControllerTest:testClaimMarketWithoutRewards() (gas: 939336)
RewardsControllerTest:testClaimWithNotEnabledRewardAsset() (gas: 1032881)
RewardsControllerTest:testConfigSettingNewStartWithOnGoingDistributionShouldNotUpdate() (gas: 366998)
RewardsControllerTest:testConfigWithDistributionNotYetStartedShouldNotFail() (gas: 496227)
RewardsControllerTest:testAccrueRewardsForWholeDistributionPeriod() (gas: 1024050)
RewardsControllerTest:testAccrueRewardsWithBadDebtClearingOfFixedBorrow() (gas: 2087511)
RewardsControllerTest:testAccrueRewardsWithRepayOfBorrowBalance() (gas: 1132703)
RewardsControllerTest:testAccrueRewardsWithRepayOfFixedBorrowBalance() (gas: 1250467)
RewardsControllerTest:testAccrueRewardsWithSeizeOfAllDepositShares() (gas: 1312393)
RewardsControllerTest:testAfterDistributionPeriodEnd() (gas: 1520577)
RewardsControllerTest:testAllClaimableUSDCWithAnotherAccountInPool() (gas: 1980404)
RewardsControllerTest:testAllClaimableUSDCWithDeposit() (gas: 1577898)
RewardsControllerTest:testAllClaimableUSDCWithFloatingBorrow() (gas: 1412656)
RewardsControllerTest:testAllClaimableUSDCWithFloatingRefund() (gas: 1319578)
RewardsControllerTest:testAllClaimableUSDCWithFloatingRepay() (gas: 1324908)
RewardsControllerTest:testAllClaimableUSDCWithMint() (gas: 1212417)
RewardsControllerTest:testAllClaimableUSDCWithRedeem() (gas: 1267798)
RewardsControllerTest:testAllClaimableUSDCWithTransfer() (gas: 1906455)
RewardsControllerTest:testAllClaimableUSDCWithTransferFrom() (gas: 1783130)
RewardsControllerTest:testAllClaimableUSDCWithWithdraw() (gas: 1268846)
RewardsControllerTest:testAllClaimableWETH() (gas: 1189976)
RewardsControllerTest:testAllClaimableWithMaturedFixedPool() (gas: 1058718)
RewardsControllerTest:testAllClaimableWithTimeElapsedZero() (gas: 1187929)
RewardsControllerTest:testClaim() (gas: 992655)
RewardsControllerTest:testClaimAll() (gas: 1738599)
RewardsControllerTest:testClaimMarketWithoutRewards() (gas: 938771)
RewardsControllerTest:testClaimWithNotEnabledRewardAsset() (gas: 1029523)
RewardsControllerTest:testConfigSettingNewStartWithOnGoingDistributionShouldNotUpdate() (gas: 366874)
RewardsControllerTest:testConfigWithDistributionNotYetStartedShouldNotFail() (gas: 496165)
RewardsControllerTest:testConfigWithTransitionFactorHigherOrEqThanCap() (gas: 145279)
RewardsControllerTest:testConfigWithZeroDepositAllocationWeightFactorShouldRevert() (gas: 91039)
RewardsControllerTest:testDifferentDistributionTimeForDifferentRewards() (gas: 1565677)
RewardsControllerTest:testEmitAccrue() (gas: 1021214)
RewardsControllerTest:testEmitClaimRewards() (gas: 914790)
RewardsControllerTest:testDifferentDistributionTimeForDifferentRewards() (gas: 1564460)
RewardsControllerTest:testEmitAccrue() (gas: 1017918)
RewardsControllerTest:testEmitClaimRewards() (gas: 911417)
RewardsControllerTest:testEmitConfigUpdate() (gas: 383168)
RewardsControllerTest:testEmitIndexUpdate() (gas: 1086204)
RewardsControllerTest:testLastUndistributed() (gas: 1538070)
RewardsControllerTest:testOperationAfterDistributionEnded() (gas: 625896)
RewardsControllerTest:testOperationsBeforeDistributionStart() (gas: 1261302)
RewardsControllerTest:testSetDistributionConfigWithDifferentDecimals() (gas: 9760792)
RewardsControllerTest:testEmitIndexUpdate() (gas: 1080136)
RewardsControllerTest:testLastUndistributed() (gas: 1527232)
RewardsControllerTest:testOperationAfterDistributionEnded() (gas: 625495)
RewardsControllerTest:testOperationsBeforeDistributionStart() (gas: 1257041)
RewardsControllerTest:testSetDistributionConfigWithDifferentDecimals() (gas: 9752750)
RewardsControllerTest:testSetDistributionOperationShouldUpdateIndex() (gas: 111881)
RewardsControllerTest:testSetDistributionWithOnGoingMarketOperations() (gas: 967696)
RewardsControllerTest:testSetHigherTotalDistribution() (gas: 1329836)
RewardsControllerTest:testSetLowerAndEqualDistributionPeriodThanCurrentTimestampShouldRevert() (gas: 912593)
RewardsControllerTest:testSetLowerAndEqualTotalDistributionThanReleasedShouldRevert() (gas: 910384)
RewardsControllerTest:testSetLowerDistributionPeriod() (gas: 1554315)
RewardsControllerTest:testSetLowerDistributionPeriodAndLowerTotalDistribution() (gas: 1557084)
RewardsControllerTest:testSetLowerTotalDistribution() (gas: 1329749)
RewardsControllerTest:testSetNewDistributionPeriod() (gas: 2061639)
RewardsControllerTest:testSetNewDistributionPeriodAfterDistributionEnds() (gas: 1158574)
RewardsControllerTest:testSetNewTargetDebt() (gas: 1243441)
RewardsControllerTest:testSetNewTargetDebtAfterDistributionEnds() (gas: 1275063)
RewardsControllerTest:testSetNewTargetDebtWithClaimOnlyAtEnd() (gas: 1128866)
RewardsControllerTest:testSetNewTreasuryFeeShouldImpactAllocation() (gas: 513942)
RewardsControllerTest:testSetTargetDebtMultipleTimes() (gas: 1802451)
RewardsControllerTest:testSetTargetDebtMultipleTimesAfterEnd() (gas: 1807032)
RewardsControllerTest:testSetTotalDistributionMultipleTimes() (gas: 1301699)
RewardsControllerTest:testTriggerHandleBorrowHookBeforeUpdatingFloatingDebt() (gas: 1301985)
RewardsControllerTest:testUpdateConfig() (gas: 1069967)
RewardsControllerTest:testUpdateIndexesWithUtilizationEqualToOne() (gas: 1005374)
RewardsControllerTest:testUpdateIndexesWithUtilizationHigherThanOne() (gas: 1056921)
RewardsControllerTest:testUpdateWithTotalDebtZeroShouldUpdateLastUndistributed() (gas: 426271)
RewardsControllerTest:testUtilizationEqualZero() (gas: 623240)
RewardsControllerTest:testWithTwelveFixedPools() (gas: 4001703)
RewardsControllerTest:testSetDistributionWithOnGoingMarketOperations() (gas: 965869)
RewardsControllerTest:testSetHigherTotalDistribution() (gas: 1323648)
RewardsControllerTest:testSetLowerAndEqualDistributionPeriodThanCurrentTimestampShouldRevert() (gas: 912109)
RewardsControllerTest:testSetLowerAndEqualTotalDistributionThanReleasedShouldRevert() (gas: 909900)
RewardsControllerTest:testSetLowerDistributionPeriod() (gas: 1545155)
RewardsControllerTest:testSetLowerDistributionPeriodAndLowerTotalDistribution() (gas: 1547924)
RewardsControllerTest:testSetLowerTotalDistribution() (gas: 1323561)
RewardsControllerTest:testSetNewDistributionPeriod() (gas: 2049507)
RewardsControllerTest:testSetNewDistributionPeriodAfterDistributionEnds() (gas: 1155358)
RewardsControllerTest:testSetNewTargetDebt() (gas: 1237253)
RewardsControllerTest:testSetNewTargetDebtAfterDistributionEnds() (gas: 1268875)
RewardsControllerTest:testSetNewTargetDebtWithClaimOnlyAtEnd() (gas: 1125650)
RewardsControllerTest:testSetNewTreasuryFeeShouldImpactAllocation() (gas: 513818)
RewardsControllerTest:testSetTargetDebtMultipleTimes() (gas: 1795961)
RewardsControllerTest:testSetTargetDebtMultipleTimesAfterEnd() (gas: 1800542)
RewardsControllerTest:testSetTotalDistributionMultipleTimes() (gas: 1298243)
RewardsControllerTest:testTriggerHandleBorrowHookBeforeUpdatingFloatingDebt() (gas: 1301187)
RewardsControllerTest:testUpdateConfig() (gas: 1066474)
RewardsControllerTest:testUpdateIndexesWithUtilizationEqualToOne() (gas: 1002508)
RewardsControllerTest:testUpdateIndexesWithUtilizationHigherThanOne() (gas: 1054055)
RewardsControllerTest:testUpdateWithTotalDebtZeroShouldUpdateLastUndistributed() (gas: 426023)
RewardsControllerTest:testUtilizationEqualZero() (gas: 622730)
RewardsControllerTest:testWithTwelveFixedPools() (gas: 3993401)
RewardsControllerTest:testWithdrawAllRewardBalance() (gas: 47213)
RewardsControllerTest:testWithdrawOnlyAdminRole() (gas: 84719)
42 changes: 23 additions & 19 deletions contracts/RewardsController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,17 @@ contract RewardsController is Initializable, AccessControlUpgradeable {
uint256 rewardsCount = rewardsList.length;
claimedAmounts = new uint256[](rewardsCount);
for (uint256 i = 0; i < marketOps.length; ) {
Distribution storage dist = distribution[marketOps[i].market];
MarketOperation memory marketOperation = marketOps[i];
Distribution storage dist = distribution[marketOperation.market];
uint256 availableRewards = dist.availableRewardsCount;
for (uint128 r = 0; r < availableRewards; ) {
update(
msg.sender,
marketOps[i].market,
marketOperation.market,
dist.availableRewards[r],
accountBalanceOperations(
marketOps[i].market,
marketOps[i].operations,
marketOperation.market,
marketOperation.operations,
msg.sender,
dist.rewards[dist.availableRewards[r]].start
)
Expand All @@ -125,11 +126,11 @@ contract RewardsController is Initializable, AccessControlUpgradeable {
}
for (uint256 r = 0; r < rewardsCount; ) {
RewardData storage rewardData = dist.rewards[rewardsList[r]];
for (uint256 o = 0; o < marketOps[i].operations.length; ) {
uint256 rewardAmount = rewardData.accounts[msg.sender][marketOps[i].operations[o]].accrued;
for (uint256 o = 0; o < marketOperation.operations.length; ) {
uint256 rewardAmount = rewardData.accounts[msg.sender][marketOperation.operations[o]].accrued;
if (rewardAmount != 0) {
claimedAmounts[r] += rewardAmount;
rewardData.accounts[msg.sender][marketOps[i].operations[o]].accrued = 0;
rewardData.accounts[msg.sender][marketOperation.operations[o]].accrued = 0;
}
unchecked {
++o;
Expand All @@ -144,9 +145,10 @@ contract RewardsController is Initializable, AccessControlUpgradeable {
}
}
for (uint256 r = 0; r < rewardsList.length; ) {
if (claimedAmounts[r] > 0) {
rewardsList[r].safeTransfer(to, claimedAmounts[r]);
emit Claim(msg.sender, rewardsList[r], to, claimedAmounts[r]);
uint256 claimedAmount = claimedAmounts[r];
if (claimedAmount > 0) {
rewardsList[r].safeTransfer(to, claimedAmount);
emit Claim(msg.sender, rewardsList[r], to, claimedAmount);
}
unchecked {
++r;
Expand Down Expand Up @@ -253,7 +255,8 @@ contract RewardsController is Initializable, AccessControlUpgradeable {
ERC20 reward
) public view returns (uint256 unclaimedRewards) {
for (uint256 i = 0; i < marketOps.length; ) {
Distribution storage dist = distribution[marketOps[i].market];
MarketOperation memory marketOperation = marketOps[i];
Distribution storage dist = distribution[marketOperation.market];
RewardData storage rewardData = dist.rewards[reward];
if (dist.availableRewardsCount == 0) {
unchecked {
Expand All @@ -263,8 +266,8 @@ contract RewardsController is Initializable, AccessControlUpgradeable {
}

AccountOperation[] memory ops = accountBalanceOperations(
marketOps[i].market,
marketOps[i].operations,
marketOperation.market,
marketOperation.operations,
account,
rewardData.start
);
Expand All @@ -280,7 +283,7 @@ contract RewardsController is Initializable, AccessControlUpgradeable {
unclaimedRewards += pendingRewards(
account,
reward,
AccountMarketOperation({ market: marketOps[i].market, accountOperations: ops })
AccountMarketOperation({ market: marketOperation.market, accountOperations: ops })
);
}
unchecked {
Expand Down Expand Up @@ -317,20 +320,21 @@ contract RewardsController is Initializable, AccessControlUpgradeable {

mapping(bool => Account) storage operationAccount = rewardData.accounts[account];
for (uint256 i = 0; i < ops.length; ) {
Account storage accountData = operationAccount[ops[i].operation];
AccountOperation memory op = ops[i];
Account storage accountData = operationAccount[op.operation];
uint256 accountIndex = accountData.index;
uint256 newAccountIndex;
if (ops[i].operation) {
if (op.operation) {
newAccountIndex = rewardData.borrowIndex;
} else {
newAccountIndex = rewardData.depositIndex;
}
if (accountIndex != newAccountIndex) {
accountData.index = uint128(newAccountIndex);
if (ops[i].balance != 0) {
uint256 rewardsAccrued = accountRewards(ops[i].balance, newAccountIndex, accountIndex, baseUnit);
if (op.balance != 0) {
uint256 rewardsAccrued = accountRewards(op.balance, newAccountIndex, accountIndex, baseUnit);
accountData.accrued += uint128(rewardsAccrued);
emit Accrue(market, reward, account, ops[i].operation, accountIndex, newAccountIndex, rewardsAccrued);
emit Accrue(market, reward, account, op.operation, accountIndex, newAccountIndex, rewardsAccrued);
}
}
unchecked {
Expand Down

0 comments on commit e17f162

Please sign in to comment.