function fundContest(uint256 index) public onlyOwner {
Pot pot = Pot(contests[index]);
IERC20 token = pot.getToken();
uint256 totalRewards = contestToTotalRewards[address(pot)];
if (token.balanceOf(msg.sender) < totalRewards) {
revert ContestManager__InsufficientFunds();
}
token.transferFrom(msg.sender, address(pot), totalRewards);
}
function testInsufficientAllowanceCreatesUnfundedContest() public {
address[] memory players = new address[](2);
players[0] = alice;
players[1] = bob;
uint256[] memory rewards = new uint256[](2);
rewards[0] = 5000;
rewards[1] = 5000;
vm.startPrank(owner);
address pot = contestManager.createContest(
players,
rewards,
usdt,
10000
);
assertEq(usdt.balanceOf(owner), 100000);
usdt.approve(address(contestManager), 1000);
contestManager.fundContest(0);
vm.stopPrank();
assertEq(usdt.balanceOf(pot), 0);
assertEq(contestManager.getContestRemainingRewards(pot), 10000);
vm.prank(alice);
Pot(pot).claimCut();
assertEq(usdt.balanceOf(alice), 0);
vm.prank(alice);
vm.expectRevert(Pot__RewardNotFound.selector);
Pot(pot).claimCut();
}
- remove this code// ContestManager.sol
pragma solidity ^0.8.20;
import {Pot} from "./Pot.sol";
import {Ownable} from "lib/openzeppelin-contracts/contracts/access/Ownable.sol";
import {IERC20} from "lib/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
+ import {SafeERC20} from "lib/openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
contract ContestManager is Ownable {
+ using SafeERC20 for IERC20;
function fundContest(uint256 index) public onlyOwner {
Pot pot = Pot(contests[index]);
IERC20 token = pot.getToken();
uint256 totalRewards = contestToTotalRewards[address(pot)];
- if (token.balanceOf(msg.sender) < totalRewards) {
- revert ContestManager__InsufficientFunds();
- }
+ // Check both balance AND allowance
+ require(
+ token.balanceOf(msg.sender) >= totalRewards,
+ "Insufficient balance"
+ );
+ require(
+ token.allowance(msg.sender, address(this)) >= totalRewards,
+ "Insufficient allowance"
+ );
- token.transferFrom(msg.sender, address(pot), totalRewards);
+ // Use SafeERC20 (reverts on false return)
+ token.safeTransferFrom(msg.sender, address(pot), totalRewards);
+
+ // Verify pot actually received the funds
+ require(
+ token.balanceOf(address(pot)) >= totalRewards,
+ "Pot funding verification failed"
+ );
+
+ emit ContestFunded(address(pot), totalRewards);
}
}
+ add this code