TwentyOne

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

TwentyOne::endGame() function does not have a check for available funds.

Summary

TwentyOne::endGame() function does not have a check for available funds.

Vulnerability Details

The endGame() function doesnot have a check for available funds.

function endGame(address player, bool playerWon) public {
//-->> no check for if funds are avaialbe or not
delete playersDeck[player].playersCards;
delete dealersDeck[player].dealersCards;
delete availableCards[player];
if (playerWon) {
payable(player).transfer(2 ether);
emit FeeWithdrawn(player, 2 ether);
}
}

Impact

The function will break and revert as the funds are not available.

Proof of Code

Here are two tests

  1. Without funding the contract(Does not PASS)

  2. With funding the contract(PASSES)

function testendgamerevertsIfNotFunded() public {
vm.startPrank(player1); // Start acting as player1
// Start the game with 1 ether bet
twentyOne.startGame{value: 1 ether}();
bool playerWon = true;
vm.expectRevert();
twentyOne.endGame(player1, playerWon);
vm.stopPrank();
}
function testendgame() public {
// Example player1 address
// Fund player1 with enough Ether
vm.deal(player1, 10 ether);
// Fund the contract with enough Ether for payouts
vm.deal(address(twentyOne), 10 ether);
// Start acting as player1
vm.startPrank(player1);
uint256 initialBalance = player1.balance;
// Start the game with 1 ether bet
twentyOne.startGame{value: 1 ether}();
// Simulate a winning scenario
bool playerWon = true;
// Call endGame to conclude the game
twentyOne.endGame(player1, playerWon);
// Assert that player1's balance increased (2 ether win minus 1 ether bet)
assert(player1.balance == initialBalance + 1 ether);
// Stop acting as player1
vm.stopPrank();
}

Tools Used

Manual Review

Recommendations

Add a check for the contract's balance to be greater or equal to the total pool of the game.

function endGame(address player, bool playerWon) public {
+ require(address(this).balance >= 2 ether, "Insufficient contract balance");
delete playersDeck[player].playersCards;
delete dealersDeck[player].dealersCards;
delete availableCards[player];
if (playerWon) {
payable(player).transfer(2 ether);
emit FeeWithdrawn(player, 2 ether);
}
}
Updates

Lead Judging Commences

inallhonesty Lead Judge 11 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.