Lack of proper scaling for a small decimals token in the Staking#update()
, which lead to precision loss and miscalculation of rewards earned.
Within the Staking#constructor()
, the owner (deployer) can assign any ERC20 token into the _token
as a staking token (TKN
) like this:
https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Staking.sol#L31-L32
Within the Staking#update()
, the _ratio
would be calculated based on the _diff
of WETH and the totalSupply
of staking token (TKN
) like this:
https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Staking.sol#L68
However, within the _ratio
calculation at the line of Staking.sol#L68 above, if the staking token (TKN
) would be a token, which is a small decimals token (i.e. USDC, which is 1e6
decimals token), the _ratio
(_diff * 1e18 / totalSupply
) would be 1e12
times larger than the actual amount.
This lead to the precision loss.
Scenario).
Here is a scenario of the calculation of the _ratio
when USDC, which is 1e6
decimals token, would be used as a staking token (TKN
).
totalSupply
of TKN
would be 1000 USDC.
_diff
of WETH would be 1 WETH.
The calculation result of _ratio
is supposed to be 1 * 1e15
.
However, as you can see above, the calculation result of the _ratio
(1 * 1e27
) was 1e12
times larger than the expected-result (1 * 1e15
).
This lead to miscalculation of rewards earned.
This vulnerability lead to precision loss and miscalculation of rewards earned due to precision loss.
Foundry
Within the Staking#update()
, consider adding if-block and code to scale the staking token (TKN
) properly (if the staking token (TKN
) is not 1e18
decimals token) like this:
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.