Cyfrin / solidity-by-example.github.io

Solidity By Example
https://solidity-by-example.org/
MIT License
614 stars 201 forks source link

An error in StakingRewards causes rewards to be negative #208

Closed azige closed 2 years ago

azige commented 2 years ago

https://github.com/solidity-by-example/solidity-by-example.github.io/blob/9f515c4e1603092f2b2e5c8f0b5397ecc2e2d176/src/pages/defi/staking-rewards/StakingRewards.sol#L25

This line in StakingRewards should be return rewardPerTokenStored;.

Once _totalSupply becomes non-zero and some accounts has positive userRewardPerTokenPaid stored, then _totalSupply gets back to zero(all tokens has been withdraw), rewardPerToken() will return zero and those accounts will get negative earned()1(causes an uint underflow).

By the way, I think the contract needs further refactoring.

azige commented 2 years ago

https://github.com/Synthetixio/synthetix/blob/d1ad8b4687e72883139ad86225b879783475979d/contracts/StakingRewards.sol#L63

I found the original contract just correctly implemented.

t4sk commented 2 years ago

Thanks. It'f fixed now