GameEnded
Event Emits Incorrect Zero Value for Prize AmountThe root cause is a logical error in the ordering of operations. The state variable pot
is reset to zero before it is used in the emit
statement, leading to the emission of stale, incorrect data.
The declareWinner()
function emits the GameEnded
event with incorrect data. Specifically, it logs the prizeAmount
as 0 in every game round. This occurs because the function first transfers the winnings from the pot
to the pendingWinnings
mapping and immediately sets the pot
state variable to 0. Only after the pot
has been cleared is the GameEnded
event emitted, using the now-zero pot
variable as the prize amount argument.
Likelihood:
This issue will occur every time declareWinner()
is successfully called, without exception.
Impact:
This bug does not lead to a direct loss of user funds, as the winner's prize is correctly credited to their pendingWinnings
balance. However, it severely damages the contract's transparency and usability. The on-chain event log, which is a critical source of truth for all external applications, becomes permanently incorrect. Any front-end, data analytics platform, or block explorer relying on these events will misleadingly display that every winner won a prize of 0 ETH. This erodes user trust and breaks off-chain functionality.
This can be verified with a Foundry test that inspects emitted events. The test would simulate a game, advance time, declare a winner, and check the emitted GameEnded
event's parameters.
A player calls claimThrone()
with 0.1 ETH. The pot
becomes 0.095 ETH (after platform fees).
The test uses vm.warp()
to expire the grace period.
The test calls declareWinner()
.
The test uses vm.expectEmit
to check the GameEnded
event.
Store the prize amount in a local memory variable before clearing the pot
state variable. Use this local variable for both crediting the winner and emitting the event to ensure data integrity.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.