Beginner FriendlyFoundry
100 EXP
View results
Submission Details
Severity: high
Valid

`totalRewards` is not divided between the `totalVotesFor` but between all the votes

Summary

The contract stipulates that the rewards will only be shared between those who voted For and not between all the voters.

Vulnerability Details

solidity
function _distributeRewards() private {
uint256 totalVotesFor = s_votersFor.length;
uint256 totalVotesAgainst = s_votersAgainst.length;
uint256 totalVotes = totalVotesFor + totalVotesAgainst;
uint256 totalRewards = address(this).balance;
if (totalVotesAgainst >= totalVotesFor) {
_sendEth(s_creator, totalRewards);
}
else {
@=> uint256 rewardPerVoter = totalRewards / totalVotes;
for (uint256 i; i < totalVotesFor; ++i) {
if (i == totalVotesFor - 1) {
@=> rewardPerVoter = Math.mulDiv(totalRewards, 1, totalVotes, Math.Rounding.Ceil);
}
_sendEth(s_votersFor[i], rewardPerVoter);
}
}
}
the `totalRewards` are divided between the `totalVotes` but shared between the `totalVotesFor`. however, the contract stipulates that rewards must be distributed only to the `For` voters.
## Impact
The voters who voted `For` will receive less Eth than they are supposed to if not every voters voted `For`, so the contract will keep some of the rewards depending on who voted `against`.
## Tools Used
-Foundry
## Recommendations
Divide the `totalRewards` between the `totalVotesFor`.
diff
function _distributeRewards() private {
     uint256 totalVotesFor = s_votersFor.length;
     uint256 totalVotesAgainst = s_votersAgainst.length;
     uint256 totalVotes = totalVotesFor + totalVotesAgainst;

    uint256 totalRewards = address(this).balance;
    if (totalVotesAgainst >= totalVotesFor) {
        _sendEth(s_creator, totalRewards);
    }
    else {
-     uint256 rewardPerVoter = totalRewards / totalVotes;
  • uint256 rewardPerVoter = totalRewards / totalVotesFor;
    
       for (uint256 i; i < totalVotesFor; ++i) {
           if (i == totalVotesFor - 1) {
    
-             rewardPerVoter = Math.mulDiv(totalRewards, 1, totalVotes, Math.Rounding.Ceil);
  •         rewardPerVoter = Math.mulDiv(totalRewards, 1, totalVotesFor, Math.Rounding.Ceil);
           }
           _sendEth(s_votersFor[i], rewardPerVoter);
       }
    

    }
    }

Updates

Lead Judging Commences

0xnevi Lead Judge over 1 year ago
Submission Judgement Published
Validated
Assigned finding tags:

VotingBooth._distributeRewards(): Incorrect computation of rewardPerVoter

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.