function claimThrone() external payable gameNotEnded nonReentrant {
require(msg.value >= claimFee, "Game: Insufficient ETH sent to claim the throne.");
@> require(msg.sender == currentKing, "Game: You are already the king. No need to re-claim.");
uint256 sentAmount = msg.value;
uint256 previousKingPayout = 0;
uint256 currentPlatformFee = 0;
uint256 amountToPot = 0;
currentPlatformFee = (sentAmount * platformFeePercentage) / 100;
if (currentPlatformFee > (sentAmount - previousKingPayout)) {
currentPlatformFee = sentAmount - previousKingPayout;
}
platformFeesBalance = platformFeesBalance + currentPlatformFee;
amountToPot = sentAmount - currentPlatformFee;
pot = pot + amountToPot;
currentKing = msg.sender;
lastClaimTime = block.timestamp;
playerClaimCount[msg.sender] = playerClaimCount[msg.sender] + 1;
totalClaims = totalClaims + 1;
claimFee = claimFee + (claimFee * feeIncreasePercentage) / 100;
emit ThroneClaimed(msg.sender, sentAmount, claimFee, pot, block.timestamp);
}
1. The game is deployed and the current king is address(0) which I verify with my assert statement.
2. I hoax as address(0) and call the claimThrone function which goes through
3. I assert that address(0) is still the king.
4. I prank as player 1, call claimThrone. The function reverts showing this error:
[Revert] Game: You are already the king. No need to re-claim.
function test_AddressZeroCanCallClaimThroneAgainAndOtherPlayersCannotJoin() public {
assert(game.currentKing() == address(0));
address currentKingAtInitalDeployment = game.currentKing();
hoax(address(0), 10 ether);
game.claimThrone{value: INITIAL_CLAIM_FEE}();
address currentKingPostClaimThrone = game.currentKing();
assert(currentKingAtInitalDeployment == currentKingPostClaimThrone);
vm.prank(player1);
uint256 currentClaimFee = game.claimFee();
vm.expectRevert();
game.claimThrone{value: currentClaimFee}();
}
Traces:
[185157] GameTest::test_AddressZeroCanCallClaimThroneAgainAndOtherPlayersCannotJoin()
├─ [2618] Game::currentKing() [staticcall]
│ └─ ← [Return] 0x0000000000000000000000000000000000000000
├─ [618] Game::currentKing() [staticcall]
│ └─ ← [Return] 0x0000000000000000000000000000000000000000
├─ [0] VM::deal(0x0000000000000000000000000000000000000000, 10000000000000000000 [1e19])
│ └─ ← [Return]
├─ [0] VM::prank(0x0000000000000000000000000000000000000000)
│ └─ ← [Return]
├─ [130721] Game::claimThrone{value: 100000000000000000}()
│ ├─ emit ThroneClaimed(newKing: 0x0000000000000000000000000000000000000000, claimAmount: 100000000000000000 [1e17], newClaimFee: 110000000000000000 [1.1e17], newPot: 95000000000000000 [9.5e16], timestamp: 1)
│ └─ ← [Stop]
├─ [618] Game::currentKing() [staticcall]
│ └─ ← [Return] 0x0000000000000000000000000000000000000000
├─ [0] VM::prank(player1: [0x7026B763CBE7d4E72049EA67E89326432a50ef84])
│ └─ ← [Return]
├─ [514] Game::claimFee() [staticcall]
│ └─ ← [Return] 110000000000000000 [1.1e17]
├─ [0] VM::expectRevert(custom error 0xf4844814)
│ └─ ← [Return]
├─ [21259] Game::claimThrone{value: 110000000000000000}()
│ └─ ← [Revert] Game: You are already the king. No need to re-claim.
└─ ← [Stop]