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.
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 positiveuserRewardPerTokenPaid
stored, then_totalSupply
gets back to zero(all tokens has beenwithdraw
),rewardPerToken()
will return zero and those accounts will get negativeearned()
1(causes anuint
underflow).By the way, I think the contract needs further refactoring.