Strategy Reward Distribution
Some strategies harvest tokens that are not sold to the underlying want position. These rewards are sent to the Tree for distribution. The amounts are calculated by evaluating proportional ownership of the pool at the time of harvest. The following description uses FARM as an example:
FARM rewards are sent to the Rewards Tree for subsequent distribution. For each claim cycle, the amount of FARM a user is entitled to is based on:
Amount deposited in the Sett
How long it was deposited for
By tracking the users' Sett deposit and withdraw events (using TheGraph data), you can derive a single value of shareSeconds that represents the overall weight of the users' deposits during the claim cycle.
Event Processing Logic
See a harvest() event on Strategy
Find the last harvest() event (Or, if this is the first one, the timestamp of the first deposit to the Sett)
Starting the next block after the last harvest():
Get a snapshot of state of user deposits at the beginning of this period (You can query the graph at this specific block)
Process events that happen during that time period
User State
For each user, we need to maintain a database of their current deposited amount, and when the last update of this information was. (The last update was the last time the user took a deposit or withdrawal action). We also track their accumulated share seconds during this claim period.
We end up tracking the following data for each user:
address -> {currentDeposited, lastUpdate, shareSeconds}
Process Deposit Action
Calculate the shareSeconds the user has accumulated since their last action
Add this amount to their stored shareSeconds value
Now, add the amount deposited to their currentDeposited
Set the lastUpdate to the timestamp of this deposit
Process Withdrawal Event
Calculate the shareSeconds the user has accumulated since their last action
Add this amount to their stored shareSeconds value
Now, remove the amount withdrawn from their currentDeposited
Set the lastUpdate to the timestamp of this deposit
After event processing
We need to update all users' shareSeconds to the end of the claim period. For each user, process their share seconds from their last action to the end of the claim period, updating shareSeconds and lastUpdate accordingly.
Determining FARM Distribution
The total amount of FARM distributed during a harvest can be found via the FarmHarvest() event. This amount should be distributed among all the users' staked during that time period in accordance with their shareSeconds.
Divide the total FARM by total share seconds, and grant each user FARM in accordance with their individual shareSeconds. Ultimately, we will create a mapping of user -> FARM
This mapping can then be added to other distribution data during that claim period and ultimately added to the tree to distribute the FARM.
See the code for Geyser Staking Rewards for a similar implementation: https://github.com/Badger-Finance/badger-system/blob/master/assistant/rewards/rewards_assistant.py
Last updated
Was this helpful?