MyCut

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

Tokens are trapped in `Pot.sol` forever because no function allow to withdraw them

Impact: High
Likelihood: High

Root + Impact

Description

  • Pot.sol contract can be funded several times and that donations are not accounted in remainingRewards, so tokens just stay in a contract.

  • Along with this, Pot.sol balance increases now when Pot::closePot() is called - there is incorrect calculation of claimantCut, which leads to that some tokens stay in Pot.sol

Here is Pot::closePot() function, where tokens are transferred to msg.sender which is a ContestManager.sol:

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);
}
}

Risk

Likelihood:

  • The issue occurs every time a contest is closed by an owner or the Pot is funded severul times via ContestManager::fundContest() function.

Impact:

  • Tokens in Pot.sol cannot be withdrawn and are trapped in the protocol forever.

Proof of Concept

There is no way to withdraw funds from Pot.sol. Tokens stay in Pot.sol after a contest is closed. Here is a test that proves that Pot.sol balance is not zero when contest is closed:

function test_tokensStayInPot_whenContestIsClosed() public mintAndApproveTokens {
vm.startPrank(user);
rewards = [500, 500];
totalRewards = 1000;
contest = ContestManager(conMan).createContest(players, rewards, IERC20(ERC20Mock(weth)), totalRewards);
uint256 potBalBefore = ERC20Mock(weth).balanceOf(contest);
console.log("potBalBefore ", potBalBefore.toString());
ContestManager(conMan).fundContest(0); // + 1000
vm.stopPrank();
uint256 potBalBeforeClaim = ERC20Mock(weth).balanceOf(contest);
console.log("potBalBeforeClaim ", potBalBeforeClaim.toString());
vm.startPrank(player1);
Pot(contest).claimCut(); // -500
vm.stopPrank();
uint256 potBalAfterClaim = ERC20Mock(weth).balanceOf(contest);
console.log("potBalAfterClaim ", potBalAfterClaim.toString());
vm.prank(user);
ContestManager(conMan).fundContest(0); // + 1000
uint256 potBalAfterFund2 = ERC20Mock(weth).balanceOf(contest);
console.log("potBalAfterFund2 ", potBalAfterFund2.toString());
vm.warp(91 days);
vm.startPrank(user);
ContestManager(conMan).closeContest(contest); // (500 - 50) / 2 = 225, i.e. -225 as claimantCut
vm.stopPrank();
uint256 expectedPotBalanceAfter = 1225;
uint256 potBalAfter = ERC20Mock(weth).balanceOf(contest);
console.log("potBalAfter ", potBalAfter.toString());
assertEq(potBalBefore, 0);
assertEq(expectedPotBalanceAfter, potBalAfter);
assertGt(potBalAfter, potBalBefore);
}

Recommended Mitigation

  1. Add a withdraw function to Pot.sol and withdraw tokens to ContestManager.sol:

+event FundsWithdrawn(address indexed to, uint256 indexed amount);
+function withdraw() public onlyOwner {
+ token.safeTransfer(msg.sender, token.balanceOf(address(this)));
+ emit FundsWithdrawn(msg.sender, token.balanceOf(address(this)));
+ }
  1. Also, it may be acceptable to account additionally transferred funds in Pot::remainingRewards and fix incorrect claimantCut calculation:

-uint256 private remainingRewards;
constructor(address[] memory players, uint256[] memory rewards, IERC20 token, uint256 totalRewards) {
i_players = players;
i_rewards = rewards;
i_token = token;
i_totalRewards = totalRewards;
- remainingRewards = totalRewards;
i_deployedAt = block.timestamp;
// i_token.transfer(address(this), i_totalRewards);
for (uint256 i = 0; i < i_players.length; i++) {
playersToRewards[i_players[i]] = i_rewards[i];
}
}
function closePot() external onlyOwner {
if (block.timestamp - i_deployedAt < 90 days) {
revert Pot__StillOpenForClaim();
}
+ uint256 remainingRewards = i_token.balanceOf(address(this));
if (remainingRewards > 0) {
uint256 managerCut = remainingRewards / managerCutPercent;
i_token.transfer(msg.sender, managerCut);
- uint256 claimantCut = (remainingRewards - managerCut) / i_players.length;
+ 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 about 2 hours 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!