People often ask me "How did you learn how to hack?" The answer: by reading. This page is a collection of the blog posts and other articles that I have accumulated over the years of my journey. Enjoy!
MasterChef contract. By understanding this, we will be able to understand most in-moment math handling and most staking contracts. blocksSinceLastReward: The amount of blocks since the reward information has been updated. rewardTokensPerBlock: The amount of tokens that should be given per block.stakerShare: The percentage of tokens of the whole staked. Since this is Solidity without floats, there is using fixed point arithmetic.blocksSinceLastReward * rewardTokensPerBlock * stakerShare(BlockARewards / TotalTokensBlockA) + (BlockBRewards / TotalTokensBlockB) + ... (BlockNRewards / TotalTokensBlockN)
StakerAShareOnNtoM = StakerATokensOnNtoM / TotalTokensOnNtoM. This means that we're calculating the tokens we had over the timeframe of blocks and dividing this by the total tokens in this timeframe. This gives us the total shares of a user over a specific timeframe. RewardsPerShare instead of calculating the rewards per block for every user. This is because the RewardsPerShare is the same for every user. To reward a single user, we calculate the amount of shares for a given user. AccumulatedRewardsPerShare that is stored for the last time a user performed some operation. Using accumulatedRewardsPerShare - previousAccumulatedRewardsPerShare allows us to calculate the proper amount for this.