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 {
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
Without funding the contract(Does not PASS)
With funding the contract(PASSES)
function testendgamerevertsIfNotFunded() public {
vm.startPrank(player1);
twentyOne.startGame{value: 1 ether}();
bool playerWon = true;
vm.expectRevert();
twentyOne.endGame(player1, playerWon);
vm.stopPrank();
}
function testendgame() public {
vm.deal(player1, 10 ether);
vm.deal(address(twentyOne), 10 ether);
vm.startPrank(player1);
uint256 initialBalance = player1.balance;
twentyOne.startGame{value: 1 ether}();
bool playerWon = true;
twentyOne.endGame(player1, playerWon);
assert(player1.balance == initialBalance + 1 ether);
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);
}
}