
After literally having audited more than100 Masterchef and Staking contracts, it’s time to share my knowledge with you,specifically the reward algorithm.
The goal here is to demystify it byfocusing on one of its core concepts: The accPerToken variable. Understandingthis will illuminate how rewards are calculated and distributed in a way that'sboth fair and efficient.
At the heart of the MasterChef contract isthe accPerToken variable. This variable plays a crucial role in tracking
"the amount of rewards allocated toeach token staked in a pool"
It ensures that rewards are distributedproportionally to the amount of liquidity each participant provides.
To simplify, imagine that Alice has staked100 tokens in a pool. If the reward rate is set to 1e18 tokens per second,after 100 seconds, a total of 100e18 tokens will be distributed amongparticipants as rewards. (Alice is the only participant at this moment)
Here's where accPerToken comes into play:
Time Passed: 100 seconds
Rewards Generated: 1e18 * 100 = 100e18tokens
accPerToken: 100e18 (total rewards) *1e18(multiplier) / 100e18 (total staked tokens)
The calculation inflates accPerToken by1e18 to avoid rounding issues, ensuring precision in reward distribution butthe main logic is to simply just calculate how many rewards each nominal tokenreceives.
When calculating a user's reward, thisinflated value is divided back by 1e18 to get the accurate reward amount:
User Reward = user.amount * accPerToken /1e18
Pool Updates: It's vital to update the pooland increase accPerToken with every balance change or interaction. This ensuresthat the reward distribution remains accurate and up-to-date. If this is notdone accurately, the algorithm will be flawed.
Claiming Rewards: When users change theirbalance (by adding or removing tokens), they must claim their accrued rewards.This step is critical to prevent manipulation of the accPerToken system. Afterclaiming, the user's rewardDebt is updated, which tracks the amount of rewardsalready paid out to ensure correct future payments.
These interactions, together with thecorrectness of the accPerToken variable increase lay the fundamental principleof the Masterchef reward algorithm. If there is any discrepancy, the logic ismost likely flawed.
Understanding the accPerToken variable isessential to grasp how rewards are calculated and distributed using theMasterChef contract.
It ensures that rewards are allocatedfairly based on the amount of liquidity each user provides to the pool.Remember, updating pools and claiming rewards are pivotal actions to maintainthe integrity of the reward distribution mechanism.
I hope this explanation sheds light on theinner workings of the Masterchef reward algorithm. It's designed to demystifythe process and make the concept accessible, especially for newcomers. If youhave further questions or need clarification on any point, feel free to ask!
Attached below is an example from
@TraderJoe_xyz written by @Louis_Mslf
The only difference is that this is usingsome other variable declarations but the logic is 100% the same.
