The distributePoints
function in the FjordPoints
contract suffers from a precision loss issue due to the order of operations in its calculations. This can result in an incorrect distribution of points, potentially disadvantaging users.
The FjordPoints
contract is designed to distribute points based on the amount of tokens staked by users. The distributePoints
function is responsible for calculating and distributing these points periodically.
The function calculates the pointsPerToken
by multiplying the number of weeks pending (weeksPending
) by the points to be distributed per epoch (pointsPerEpoch
), and then dividing by the total amount of tokens staked (totalStaked
). However, the current implementation performs the division before the multiplication, which can lead to precision loss due to Solidity's integer division rounding down.
The problematic code is as follows:
In this line, pointsPerEpoch
is multiplied by PRECISION_18
and then divided by totalStaked
, which can result in a loss of precision. This precision loss can accumulate over time, leading to an incorrect calculation of pointsPerToken
.
The precision loss in the distributePoints
function can lead to an incorrect distribution of points among users. This can result in users receiving fewer points than they are entitled to, potentially disadvantaging them. Over time, this can lead to significant discrepancies in the distribution of points, undermining the fairness and accuracy of the points distribution mechanism.
Assume weeksPending
is 2, pointsPerEpoch
is 100 ether, PRECISION_18
is 1e18, and totalStaked
is 1000 ether.
The current implementation calculates pointsPerToken
as:
pointsPerToken = pointsPerToken.add(2 * (100 ether * 1e18 / 1000 ether))
This results in pointsPerToken
being incremented by 2 * 1e20
, which is 2e20
.
The correct calculation should be:
pointsPerToken = pointsPerToken.add((2 * 100 ether * 1e18) / 1000 ether)
This results in pointsPerToken
being incremented by 2e21 / 1000
, which is 2e18
.
Manual review
To fix the precision loss issue, the multiplication should be performed before the division. The corrected code is as follows:
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.