TwentyOne

First Flight #29
Beginner FriendlyGameFiFoundrySolidity
100 EXP
View results
Submission Details
Severity: medium
Valid

The `TwentyOne::call` function never checks if the contract has enough ETH to pay the player in case they win

Description

The TwentyOne::call function is missing a check to confirm it holds at least 2 ETH at the moment the player calls it.

Impact

If the contract holds less than 2 ETH and a player wins when calling the TwentyOne::call function, the player will not receive their rewards.

Proof of Concepts

  1. Contract TwentyOne is deployed

  2. Alice is the first person to call TwentyOne::startGame

  3. Alice calls TwentyOne::call and win

  4. The contract holds 0 ETH and is not able to send Alice her reward

Recommended mitigation

Add a contract balance checker in the function

function call() public {
require(
playersDeck[msg.sender].playersCards.length > 0,
"Game not started"
);
+ require(address(this).balance >= 2 ether, "The contract doesn't have enough balance, call the function later");
uint256 playerHand = playersHand(msg.sender);
// Calculate the dealer's threshold for stopping (between 17 and 21)
uint256 standThreshold = (uint256(
keccak256(
abi.encodePacked(block.timestamp, msg.sender, block.prevrandao)
)
) % 5) + 17;
// Dealer draws cards until their hand reaches or exceeds the threshold
while (dealersHand(msg.sender) < standThreshold) {
uint256 newCard = drawCard(msg.sender);
addCardForDealer(msg.sender, newCard);
}
uint256 dealerHand = dealersHand(msg.sender);
// Determine the winner
if (dealerHand > 21) {
emit PlayerWonTheGame(
"Dealer went bust, players winning hand: ",
playerHand
);
endGame(msg.sender, true);
} else if (playerHand > dealerHand) {
emit PlayerWonTheGame(
"Dealer's hand is lower, players winning hand: ",
playerHand
);
endGame(msg.sender, true);
} else {
emit PlayerLostTheGame(
"Dealer's hand is higher, dealers winning hand: ",
dealerHand
);
endGame(msg.sender, false);
}
}
Updates

Lead Judging Commences

inallhonesty Lead Judge 10 months ago
Submission Judgement Published
Validated
Assigned finding tags:

Insufficient balance for payouts / Lack of Contract Balance Check Before Starting Game

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.