The RewardFactor serves as a single reference point which we use to calculate a depositors token allocation.
Each FeederPool keeps track of a single variable which we call the RewardFactor. This RewardFactor serves as a central reference point for calculating the number of tokens owed to each depositor at any given moment. The RewardFactor is a cumulative record of tokens earned per unit of deposit since the inception of the FeederPool. With each transaction, the RewardFactor is incremented by the number of tokens earned per unit of deposit, since the last transaction. The RewardFactor after N transactions, for FeederPool i is shown below.
RewardFactor for FeederPool i at transaction N
The RewardFactor cannot be decreased since all components are positive. It will increase more rapidly when TokensPerSecond is increased or the amount of deposits in the FeederPool decreases. This is because more tokens are being earned across a smaller number of deposits.
The DepositorRewardFactor represents a weighted average blend of the RewardFactor in the FeederPool when the depositor makes their deposits. By comparing this value with the current RewardFactor in the FeederPool we can establish how many tokens a depositor has earned.
Each FeederPool keeps track of a DepositorRewardFactor for every depositor in that FeederPool. When a depositor makes their first deposit into FeederPool, the DepositorRewardFactor is set to the current RewardFactor of that FeederPool. Any subsequent deposits by that depositor in that FeederPool adjust the DepositorRewardFactor using the weighted-average of the current RewardFactor and the current DepositorRewardFactor.
Update formula for a increase in deposit
In the example below a depositor makes two deposits and a full withdrawal from a FeederPool. These transactions are consecutive, and 1 day apart. When the first deposit is made the RewardFactor is 3000, and Total Deposits are 800. As this is the first deposit (for this depositor), the DepositorRewardFactor is set at 3000 for the depositor. When the second deposit is made, 1 day has passed, so the TokenRewardFactor is updated to 3086.40. The DepositorRewardFactor is recalculated using the weighted average of the previous DepositorRewardFactor and the current TokenRewardFactor. When the withdrawal is made, 1 day has passed, so the RewardFactor is updated to 3158.40.
When the withdrawal is made, the depositor is entitled to 46,080 tokens (=400 · (3158.40 − 3043.20). We can check this is the amount the depositor is entitled to by looking at their relative share of deposits in the FeederPool and multiplying it by the number of tokens issued in the period. (200/1000 · 86400) + (400/1200 · 86400) = 46,080.