Wrong event parameter emitted in Game::declareWinner function
Description
-
Game::declareWinner function
emits wrong param value for prizeAmount
-
Game::declareWinner function
emits an event which contains prizeAmount
param which is pot balance given to winner. Before emitting event pot
amount is assign 0. Eventually, event also emit the 0 prizeAmount
function declareWinner() external gameNotEnded {
require(currentKing != address(0), "Game: No one has claimed the throne yet.");
require(
block.timestamp > lastClaimTime + gracePeriod,
"Game: Grace period has not expired yet."
);
gameEnded = true;
pendingWinnings[currentKing] = pendingWinnings[currentKing] + pot;
pot = 0;
@> emit GameEnded(currentKing, pot, block.timestamp, gameRound);
}
Risk
Likelihood: low
Proof of Concept
add test_declareWinner
in Game.t.sol
test file. Run the command forge test --mt test_declareWinner -vvvv
to check the result.
Below test will depect, declareWinner
function emits event with 0 prize amount
function test_declareWinner() public {
vm.startPrank(user1);
uint256 amountToClaimThrone = 2 ether;
game.claimThrone{value:amountToClaimThrone}();
vm.stopPrank();
uint256 graceTime = game.gracePeriod();
uint256 lastclaimTime = game.lastClaimTime();
vm.roll(block.number + graceTime+lastclaimTime);
vm.warp(block.timestamp + graceTime+lastclaimTime);
vm.startPrank(user2);
game.declareWinner();
vm.stopPrank();
assert(game.gameEnded() == true);
assert(game.pendingWinnings(user1) == 1.9 ether);
}
├─ [0] VM::startPrank(user2: [0x537C8f3d3E18dF5517a58B3fB9D9143697996802])
│ └─ ← [Return]
├─ [48689] Game::declareWinner()
│ ├─ emit GameEnded(winner: user1: [0x29E3b139f4393aDda86303fcdAa35F60Bb7092bF], prizeAmount: 0, timestamp: 86402 [8.64e4], round: 1)
│ └─ ← [Stop]
├─ [0] VM::stopPrank()
Recommended Mitigation
function declareWinner() external gameNotEnded {
require(currentKing != address(0), "Game: No one has claimed the throne yet.");
require(
block.timestamp > lastClaimTime + gracePeriod,
"Game: Grace period has not expired yet."
);
gameEnded = true;
pendingWinnings[currentKing] = pendingWinnings[currentKing] + pot;
+ uint256 _prizeAmount = pot;
pot = 0; // Reset pot after assigning to winner's pending winnings
+ emit GameEnded(currentKing, _prizeAmount, block.timestamp, gameRound);
- emit GameEnded(currentKing, pot, block.timestamp, gameRound);
}