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

Streamed rewards #74

Merged
merged 5 commits into from
Dec 3, 2024
Merged

Streamed rewards #74

merged 5 commits into from
Dec 3, 2024

Conversation

gravityblast
Copy link
Member

Description

first PR part of #65

Update the current implementation to set a reward for a specific period (like 10K tokens for 1 year) and stream that dynamically.

Checklist

Ensure you completed all of the steps below before submitting your pull request:

  • Added natspec comments?
  • Ran pnpm adorno?
  • Ran pnpm verify?

if (amount == 0) {
revert StakingManager__AmountCannotBeZero();
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be very defensive, I'd suggest we:

  1. Either ensure amount is at least 1e18 or
  2. Ensure rewardPerSecond is not 0

While not super likely to happen, there's a chance to provide amount and duration such that rewardPerSecond will round down to 0

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, just noticed we're no longer calculating rewardsPerSecond here. We should probably still have the check I've mentioned above.

// FIXME: If newTotalWeight > previousTotalWeight, then (previousTotalWeight) / (newTotalWeight) is less than 1.
// Multiplying rewardIndex by a fraction less than 1 reduces its value.
// Users who have not updated their accountRewardIndex may see a reduction in their pending rewards.
// Possible fix: adjust only when newTotalWeight < previousTotalWeight
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gravityblast I've been pondering on this comment for quite a while now and I can't quite understand what the issue here is. Can you give a concrete example that demonstrates the issue you're describing here?

@gravityblast gravityblast force-pushed the streamed-rewards branch 5 times, most recently from 6b1e0a2 to 2108d88 Compare December 3, 2024 09:24

RewardsStreamerMP.Account memory accountInfo = streamer.getAccount(p.account);

assertEq(accountInfo.stakedBalance, p.stakedBalance, "wrong account staked balance");
assertEq(stakingToken.balanceOf(p.account), p.vaultBalance, "wrong vault balance");
assertEq(accountInfo.accountRewardIndex, p.rewardIndex, "wrong account reward index");
// assertEq(accountInfo.accountRewardIndex, p.rewardIndex, "wrong account reward index");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should remove the commented out lines

@@ -241,7 +224,7 @@ contract IntegrationTest is RewardsStreamerMPTest {

// T3
vm.prank(admin);
rewardToken.transfer(address(streamer), 1000e18);
// rewardToken.transfer(address(streamer), 1000e18);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover comment

@@ -391,7 +370,7 @@ contract IntegrationTest is RewardsStreamerMPTest {

// T6
vm.prank(admin);
rewardToken.transfer(address(streamer), 1000e18);
// rewardToken.transfer(address(streamer), 1000e18);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

streamer.setReward(2000e18, 10 days);
// accrued is 1000 from the previous reward and still 0 for the new one
assertEq(streamer.totalRewardsSupply(), 1000e18, "totalRewardsSupply should be 1000");
assertEq(streamer.totalRewardsAccrued(), 1000e18);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, interesting. Looking at these assertions, I expected it to be the other way around:

  • totalRewardsSupply() is what is "checkpointed"
  • totalRewardsAccrued() is "real-time" balance that is accrued until this point in time

Also, the comment says [...] and still 0 for the new one but the 0 isn't reflected here anywhere.
Likely needs rewording?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0x-r4bbit yes so we have:

  • 1000 accrued and saved to storage in totalRewardsAccrued
  • 0 pending
  • totalRewardsSupply is totalRewardsAccrued + pending = 1000

// FIXME: this is needed to update the global state and account MP
// Later we should update the functions to use "real-time" values.
streamer.updateGlobalState();
streamer.updateAccountMP(vaults[alice]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re: the FIXME comment: can't we have the real-time functions be part of this PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm preparing it for the next PR so we can merge this otherwise I have too many conflicts every day :-P

uint256 tolerance = 300; // 300 wei

assertEq(streamer.totalRewardsSupply(), 100e18, "Total rewards supply mismatch");
assertApproxEqAbs(streamer.rewardsBalanceOf(vaults[alice]), 100e18, tolerance);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this due to rounding issues? If yes, @3esmit brought something similar up in #85

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

@0x-r4bbit 0x-r4bbit merged commit b1a4e5a into main Dec 3, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants