vacp2p / staking-reward-streamer

MIT License
0 stars 0 forks source link

Rewards Streaming

Rewards Streamer

The rewardIndex is a crucial part of the reward distribution mechanism.

It represents the accumulated rewards per staked token since the beginning of the contract's operation.

Here's how it works:

  1. Initial state: When the contract starts, rewardIndex is 0.
  2. Whenever new rewards are added to the contract (detected in updateRewardIndex()), the rewardIndex increases. The increase is calculated as: rewardIndex += (newRewards * SCALE_FACTOR) / totalStaked This calculation distributes the new rewards evenly across all staked tokens.
  3. Each user has their own userRewardIndex, which represents the global rewardIndex at the time of their last interaction (stake, unstake, or reward claim).
  4. When a user wants to claim rewards, we calculate the difference between the current rewardIndex and the user's userRewardIndex, multiply it by their staked balance, and divide by SCALE_FACTOR
  5. After a user stakes, unstakes, or claims rewards, their userRewardIndex is updated to the current global rewardIndex. This "resets" their reward accumulation for the next period.

Instead of updating each user's rewards every time new rewards are added, we only need to update a single global variable (rewardIndex).

We don't need to assign Rewards to epochs, so we don't need to finalize Rewords for each epoch and each user.

User-specific calculations are done only when a user interacts with the contract.

SCALE_FACTOR is used to maintain precision in calculations.

Rewards Streamer Example

example

Initial setup:

T1: Alice stakes 10 tokens

T2: Bob stakes 30 tokens

T3: 1000 Rewards arrive

New rewardIndex calculation:

Potential Rewards for Alice and Bob:

For Alice:

For Bob:

T4: Alice withdraws her stake and Rewards

Alice's withdrawal:

Update state:

T5: Charlie stakes 30 tokens

T6: Another 1000 Rewards arrive

New rewardIndex calculation:

Rewards for Bob and Charlie:

For Bob:

For Charlie:

If Bob and Charlie were to withdraw now:

Bob's withdrawal:

Charlie's withdrawal:

T7: Final state:

Rewards Streamer with Multiplier Points

TODO