MyCut

AI First Flight #8
Beginner FriendlyFoundry
EXP
View results
Submission Details
Severity: high
Valid

Pot.closePot() permanently locks funds by dividing the claimant remainder by i_players.length instead of claimants.length

Description

Pot.closePot() distributes the remainder after the manager's cut using the wrong denominator. It divides by the total number of registered players (i_players.length) instead of the number of players who actually claimed in time (claimants.length).

// src/Pot.sol — closePot()
function closePot() external onlyOwner {
if (block.timestamp - i_deployedAt < 90 days) {
revert Pot__StillOpenForClaim();
}
if (remainingRewards > 0) {
uint256 managerCut = remainingRewards / managerCutPercent;
i_token.transfer(msg.sender, managerCut);
​
@> uint256 claimantCut = (remainingRewards - managerCut) / i_players.length;
@> for (uint256 i = 0; i < claimants.length; i++) {
_transferReward(claimants[i], claimantCut);
}
}
}

The README states the remainder must be "distributed equally to those who claimed in time". Because the divisor counts every registered player while the loop pays only claimants, the distributed total is strictly smaller than the remainder whenever claimants.length < i_players.length. The undistributed tokens stay in the Pot permanently — neither Pot nor ContestManager exposes any sweep or withdraw function.

Risk

Likelihood: HIGH — partial claiming is the protocol's intended normal flow. The 90-day window exists precisely because not every player claims in time.

Impact: HIGH — permanent loss of funds for honest claimants. With 5 players and 2 claimants, 34 of 100 tokens (34%) are locked forever, and each claimant receives 10 instead of 27.

Severity: HIGH (High impact × High likelihood)

Proof of Concept

The test below deploys the real ContestManager and Pot, funds the pot with 100 tokens for 5 players (20 each), lets only 2 players claim within the window, then advances time past 90 days and closes the contest. It asserts the exact resulting balances, proving that 34 tokens are permanently locked and each claimant is underpaid by 17 tokens.

Steps:

Place as test/PotClaimantCutPoC.t.sol and run:

forge test --match-contract PotClaimantCutPoC -vvv
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
​
import {Test, console} from "lib/forge-std/src/Test.sol";
import {ContestManager} from "../src/ContestManager.sol";
import {Pot} from "../src/Pot.sol";
import {IERC20} from "lib/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
import {ERC20Mock} from "./ERC20Mock.sol";
​
contract PotClaimantCutPoC is Test {
address owner = makeAddr("owner");
address p1 = makeAddr("p1");
address p2 = makeAddr("p2");
address p3 = makeAddr("p3");
address p4 = makeAddr("p4");
address p5 = makeAddr("p5");
​
uint256 constant PER_PLAYER = 20;
uint256 constant TOTAL = 100;
​
ContestManager manager;
ERC20Mock token;
address pot;
​
function setUp() public {
vm.startPrank(owner);
manager = new ContestManager();
token = new ERC20Mock("MOCK", "MCK", owner, 1000);
token.approve(address(manager), 1000);
​
address[] memory players = new address[](5);
uint256[] memory rewards = new uint256[](5);
players[0] = p1; players[1] = p2; players[2] = p3; players[3] = p4; players[4] = p5;
for (uint256 i = 0; i < 5; i++) rewards[i] = PER_PLAYER;
​
pot = manager.createContest(players, rewards, IERC20(address(token)), TOTAL);
manager.fundContest(0);
vm.stopPrank();
}
​
function test_claimantCut_divided_by_all_players_not_claimants() public {
// Only 2 of 5 players claim within the 90-day window
vm.prank(p1);
Pot(pot).claimCut();
vm.prank(p2);
Pot(pot).claimCut();
​
uint256 remaining = Pot(pot).getRemainingRewards();
console.log("remainingRewards after 2 claims:", remaining);
assertEq(remaining, 60);
​
vm.warp(block.timestamp + 90 days);
vm.prank(owner);
manager.closeContest(pot);
​
uint256 potBalance = token.balanceOf(pot);
uint256 p1Balance = token.balanceOf(p1);
uint256 p2Balance = token.balanceOf(p2);
uint256 managerCut = token.balanceOf(address(manager));
​
console.log("pot balance after close:", potBalance);
console.log("p1 balance (20 claim + cut):", p1Balance);
console.log("p2 balance (20 claim + cut):", p2Balance);
console.log("manager contract cut:", managerCut);
​
// managerCut = 60/10 = 6
assertEq(managerCut, 6);
​
// BUG: claimantCut = (60-6)/5 = 10, should be (60-6)/2 = 27
assertEq(p1Balance, 30);
assertEq(p2Balance, 30);
​
// 34 tokens permanently locked in the Pot
assertGt(potBalance, 0, "funds locked in Pot");
assertEq(potBalance, 34);
​
uint256 fairCut = (60 - 6) / 2; // 27
assertTrue(p1Balance != PER_PLAYER + fairCut, "claimant did NOT get fair share");
}
}

Actual output:

remainingRewards after 2 claims: 60
pot balance after close: 34
p1 balance (20 claim + cut): 30
p2 balance (20 claim + cut): 30
manager contract cut: 6
Suite result: ok. 1 passed; 0 failed; 0 skipped

Each claimant received 20 + 10 = 30. The fair share is 20 + 27 = 47. 34 tokens are permanently locked in the Pot.

Recommended Mitigation

Divide the remainder by the number of claimants rather than the total number of players, and guard against an empty claimant set to avoid division by zero. This makes the distributed amount match the README's stated behaviour: the remainder is split equally among those who claimed in time.

- uint256 claimantCut = (remainingRewards - managerCut) / i_players.length;
- for (uint256 i = 0; i < claimants.length; i++) {
- _transferReward(claimants[i], claimantCut);
- }
+ if (claimants.length > 0) {
+ uint256 claimantCut = (remainingRewards - managerCut) / claimants.length;
+ for (uint256 i = 0; i < claimants.length; i++) {
+ _transferReward(claimants[i], claimantCut);
+ }
+ }
Updates

Lead Judging Commences

ai-first-flight-judge Lead Judge 16 minutes ago
Submission Judgement Published
Validated
Assigned finding tags:

[H-02] Incorrect logic in `Pot::closePot` leads to unfair distribution to `claimants`, potentially locking the funds with no way to take that out

## Description in `closePot` function while calclulating the shares for claimaint cut, `i_players.length` is used, instead of `claimants.length`, causing low amount being distributed to claimants. ## Vulnerability Details [2024-08-MyCut/src/Pot.sol at main · Cyfrin/2024-08-MyCut (github.com)](https://github.com/Cyfrin/2024-08-MyCut/blob/main/src/Pot.sol#L57) `Pot::closePot` function is meant to be called once contest passed 90 days, it sends the owner cut to owner and rest is splitted among the users who claimed b/w 90 days period. However, current implementation is wrong.&#x20; It uses total users (i_players.length) instead of the users (claimants.length) who claimed during the duration. This creates an unfair distribution to the participants and some of the funds could be locked in the contract. In worst case scenerio, it could be 90% if nobody has claimed from the protocol during the 90 days duration. ## POC In existing test suite, add following test: ```solidity function testUnfairDistributionInClosePot() public mintAndApproveTokens { // Setup address[] memory testPlayers = new address[](3); testPlayers[0] = makeAddr("player1"); testPlayers[1] = makeAddr("player2"); testPlayers[2] = makeAddr("player3"); uint256[] memory testRewards = new uint256[](3); testRewards[0] = 400; testRewards[1] = 300; testRewards[2] = 300; uint256 testTotalRewards = 1000; // Create and fund the contest vm.startPrank(user); address testContest = ContestManager(conMan).createContest( testPlayers, testRewards, IERC20(ERC20Mock(weth)), testTotalRewards ); ContestManager(conMan).fundContest(0); vm.stopPrank(); // Only player1 claims their reward vm.prank(testPlayers[0]); Pot(testContest).claimCut(); // Fast forward 91 days vm.warp(block.timestamp + 91 days); // Record balances before closing the pot uint256 player1BalanceBefore = ERC20Mock(weth).balanceOf( testPlayers[0] ); // Close the contest vm.prank(user); ContestManager(conMan).closeContest(testContest); // Check balances after closing the pot uint256 player1BalanceAfter = ERC20Mock(weth).balanceOf(testPlayers[0]); // Calculate expected distributions uint256 remainingRewards = 600; // 300 + 300 unclaimed rewards uint256 ownerCut = remainingRewards / 10; // 10% of remaining rewards uint256 distributionPerPlayer = (remainingRewards - ownerCut) / 1; // as only 1 user claimed uint256 fundStucked = ERC20Mock(weth).balanceOf(address(testContest)); // actual results console.log("expected reward:", distributionPerPlayer); console.log( "actual reward:", player1BalanceAfter - player1BalanceBefore ); console.log("Fund stucked:", fundStucked); } ``` then run `forge test --mt testUnfairDistributionInClosePot -vv` in the terminal and it will show following output: ```js [⠊] Compiling... [⠒] Compiling 1 files with Solc 0.8.20 [⠘] Solc 0.8.20 finished in 1.63s Compiler run successful! Ran 1 test for test/TestMyCut.t.sol:TestMyCut [PASS] testUnfairDistributionInClosePot() (gas: 905951) Logs: User Address: 0x6CA6d1e2D5347Bfab1d91e883F1915560e09129D Contest Manager Address 1: 0x7BD1119CEC127eeCDBa5DCA7d1Bd59986f6d7353 Minting tokens to: 0x6CA6d1e2D5347Bfab1d91e883F1915560e09129D Approved tokens to: 0x7BD1119CEC127eeCDBa5DCA7d1Bd59986f6d7353 expected reward: 540 actual reward: 180 Fund stucked: 360 Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 1.58ms (506.33µs CPU time) ``` ## Impact Loss of funds, Unfair distribution b/w users ## Recommendations Fix the functions as shown below: ```diff function closePot() external onlyOwner { if (block.timestamp - i_deployedAt < 90 days) { revert Pot__StillOpenForClaim(); } if (remainingRewards > 0) { uint256 managerCut = remainingRewards / managerCutPercent; i_token.transfer(msg.sender, managerCut); - uint256 claimantCut = (remainingRewards - managerCut) / i_players.length; + uint256 totalClaimants = claimants.length; + if(totalClaimant == 0){ + _transferReward(msg.sender, remainingRewards - managerCut); + } else { + uint256 claimantCut = (remainingRewards - managerCut) / claimants.length; for (uint256 i = 0; i < claimants.length; i++) { _transferReward(claimants[i], claimantCut); } } + } } ```

Support

FAQs

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

Give us feedback!