Skip to content

Algorithm

dexterslabor edited this page Mar 4, 2021 · 1 revision

Farm pool & state updates

Each farm instance has its own pool, that keeps track of vital data such as accumulatedRewardPerShare. Each pool can be updated only once per block - this is ensured by keeping track of lastBlockUpdate.

Entrypoints %deposit, %claim, %withdraw all call an updatePool() function, causing the pool to update with every first operation that 'arrives' to the farm in each block.

If there are no tokens delegated to the farm, which means if farmLpTokenBalance = 0n then pool updates are skipped. Resulting in no rewards being distributed, and only the lastBlockUpdate being increased. If this behavior was not part of the implementation, then rewards would be lost for blocks when no-one is delegating to the farm.

Pool update formulas

- reward

If the calculated reward from the formula below exceeds the plannedRewards, then a rewardRemainder is returned instead.

Calculation of plannedRewards and rewardRemainder can be found in updatePool.religo

(Tezos.level - lastBlockUpdate) * rewardPerBlock

- accumulatedRewardPerShare

accumulatedRewardPerShare + reward / farmTokenBalance

Reward calculation per delegator

delegatorReward = (accumulatedRewardPerShareEnd - accumulatedRewardPerShareStart) * delegatorBalance

accumulatedRewardPerShareEnd = current accumulatedRewardPerShare

accumulatedRewardPerShareStart = stakingStart