Calculation of Tree Rewards
This page is an overview of how Badger tree rewards are distributed between users.

## Previous Method for Calculating Tree Rewards

Rewards bot was taking snapshots for each user's balance for each vault on the last block of running rewards cycle.
Let's say user has some stake in some token in one of Badger's vaults...
user_balance_in_sett = 100
The total balance of all other user balances in the vault is something like...
totals = 10000
At this point, let's identify what is user's share of the vaults total balance...
user_share = user_balance_in_sett / totals == 0.01
Then there is the amount of rewards that has to be distributed for this particular vault...
amount_to_distribute = 12345
Concluding everything from the above, our particular user will get 1% of the rewards pool...
user_reward_amount = amount_to_distribute * user_share
With this method, users who deposited their funds at the end of the cycle, end up receiving a full cycles worth of rewards as though they had assets deposited the whole time. This is disproportionate to what they are in fact entitled to.

## Introducing Tree TWAP rewards

To solve this problem, we are now taking multiple snapshots during the rewards cycle and calculating the TWAP price out of all balances.
The amount of snapshots we are going to take during cycle will be 3 but this can change and evolve over time.
NUMBER_OF_SNAPSHOTS = 3

## Examples of How This Will Work

### Example 1:

User has deposited funds at the beginning of the cycle and their balance doesn't change throughout. Totals also remains the same, assuming all users have not done anything with their funds.
Again:
user_balance_in_sett = 100
totals = 10000
We take 3 snapshots + the snapshot at the start cycle block and sum up each user's balances. As user had the same balance each time we take a snapshot, we will multiply user balance by 4...
user_balance = user_balance_in_sett * (NUMBER_OF_SNAPSHOTS + 1)
As each user balance is summed up multiple times, totals will also be summed...
totals = totals * (NUMBER_OF_SNAPSHOTS + 1)
That same action is done to identify the user's share of the total vault balance...
twap_user_share = user_balance / totals == 0.01
User reward percentage will be 1% again but it was verified that user had their funds locked through the entire cycle.
Below is a more complex example.

### Example 2:

User has deposited funds at the last block of currently running rewards cycle...
user_balance_in_sett = 100
totals = 10000
Taking multiple snapshots again, user didn't have any balance at any snapshot taken during cycle except the last one...
user_balance = 0 + 0 + 0 + user_balance_in_sett
totals = 10000 + 10000 + 10000 + (totals + user_balance_in_sett)
Let's check how that changes user's share of the rewards...
twap_user_share = user_balance / totals == 0.00249
In this case, user's reward share is 0.25%.
amount_to_distribute = 12345
user_reward_amount = amount_to_distribute * 0.00249
This method gives more rewards to users who had their funds deposited the entire cycle, making this distribution more fair.

### Example 3:

Let's imagine that user had their funds in one of vaults but decided to withdraw right after the first cycle block...
user_balance_in_sett = 100
totals = 10000
totals_without_user_balance = totals - user_balance_in_sett
user_balance = user_balance_in_sett + 0 + 0 + 0
totals = (
totals + totals_without_user_balance + totals_without_user_balance
+ totals_without_user_balance
)
In this case, user who withdrew their funds after the first cycle block receives only 0.25% of the rewards share...
twap_user_share = user_balance / totals == 0.0025
This gives users who had their funds in sett at the beginning of the cycle a fraction of rewards, instead of giving them nothing as was the process before.