function claimThrone() external payable gameNotEnded nonReentrant {
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;
}
vm.prank(player1);
uint256 player1InitialBalance = player1.balance;
console2.log("Player1 initial balance:", player1InitialBalance);
uint256 player1BalanceAfterClaim = player1.balance;
console2.log("Player1 balance after becoming king:", player1BalanceAfterClaim);
uint256 newClaimFee = game.claimFee();
uint256 player1BalanceBeforeDethroned = player1.balance;
vm.prank(player2);
uint256 player1BalanceAfterDethroned = player1.balance;
console2.log("Player1 balance after being dethroned:", player1BalanceAfterDethroned);
assertEq(player1BalanceBeforeDethroned, player1BalanceAfterDethroned);
console2.log("VULNERABILITY: Previous king received NO payout!");
uint256 player1PendingWinnings = game.pendingWinnings(player1);
assertEq(player1PendingWinnings, 0);
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;
+ // Give payout to previous king if one exists
+ if (currentKing != address(0)) {
+ previousKingPayout = (sentAmount * 10) / 100; // 10% to previous king
+ pendingWinnings[currentKing] += previousKingPayout;
+ }
- // Calculate platform fee
- currentPlatformFee = (sentAmount * platformFeePercentage) / 100;
+ // Calculate platform fee from remaining amount
+ uint256 remainingAmount = sentAmount - previousKingPayout;
+ currentPlatformFee = (remainingAmount * platformFeePercentage) / 100;
- // Defensive check to ensure platformFee doesn't exceed available amount after previousKingPayout
- if (currentPlatformFee > (sentAmount - previousKingPayout)) {
- currentPlatformFee = sentAmount - previousKingPayout;
- }
platformFeesBalance = platformFeesBalance + currentPlatformFee;
// Remaining amount goes to the pot
- amountToPot = sentAmount - currentPlatformFee;
+ amountToPot = remainingAmount - currentPlatformFee;
pot = pot + amountToPot;
// Update game state
currentKing = msg.sender;
lastClaimTime = block.timestamp;
playerClaimCount[msg.sender] = playerClaimCount[msg.sender] + 1;
totalClaims = totalClaims + 1;
// Increase the claim fee for the next player
claimFee = claimFee + (claimFee * feeIncreasePercentage) / 100;
emit ThroneClaimed(
msg.sender,
sentAmount,
claimFee,
pot,
block.timestamp
);
}