# Smart Contract Error : Gas required exceeds 300000000

I got the following code from this tutorial :

``````pragma solidity ^0.4.11;

contract Casino {

uint minimumBet;
uint totalBet;
uint numberOfBets;
uint maximumAmountsOfBets = 100;

struct Player {

uint amountBet;
uint numberSelected;
}

function Casino(uint _minimumBet) public{  // Constructor : has the same name as the contract , used to set up
//contract owner

owner = msg.sender;
if(_minimumBet !=0) minimumBet = _minimumBet;

}

//To be for a number btw 1 & 10 both inclusive

function bet(uint number) payable public {

require(checkPlayerExists(msg.sender) );
require(number >= 1 && number <= 10);
require(msg.value >= minimumBet);

playerInfo[msg.sender].amountBet = msg.value;
playerInfo[msg.sender].numberSelected = number;
numberOfBets += 1;
players.push(msg.sender);
totalBet += msg.value;

if(numberOfBets >= maximumAmountsOfBets) generateNumberWinner();

}

function checkPlayerExists(address player) public view returns(bool) {

for( uint i = 0 ; i < players.length; i++){
if(players[i] == player) return true;
}
return false;
}

/*
Generate Winner : Generates a number between 1 & 10
*/

function generateNumberWinner() public{

uint numberGenerated = block.number % 10 + 1; // This isnt secure

distributePrizes(numberGenerated);
}

/*

Distribute Prizes : Sends the correspondng ether to each winner
depending on the total bets
*/

function distributePrizes(uint numberWinner) public {

address[100] memory winners ; // We have to create a temporary in memory array with fixed size

uint count = 0; // This is the count for the array of winners

for(uint i = 0; i < players.length ; i++) {
count ++;
}
delete playerInfo[playerAddress]; // Delete all the players array

}

players.length = 0; //Delete all the players array

uint winnerEtherAmount = totalBet / winners.length; // How much each player gets

for(uint j = 0; j < count; j++) {

if(winners[j] !=address(0)) // Check the address in the fixed array is not empty
winners[j].transfer(winnerEtherAmount);
}

}

/*
Annonymous Fallback function: In case someone sends ether to the
contract so it doesnt get lost
*/
function() payable private{}

/*
Kill function: Used to destroy contract whenever we want. Only owner
has the ability to kill the contract
*/

function kill() private{

if(msg.sender == owner)
selfdestruct(owner);
}
}
``````

It compiles fine, but when I try to `bet` it REMIX throws the following error:

transact to Casino.bet errored: Gas required exceeded limits : 3000000. An important gas estimation may also be a sign of a problem in the contract code. Please check loops and be sure you did not sent value to a non payable function (that's also a reason for a strong gas estimation).

I would appreciate it if someone could point me in the right direction.

• "from a tutorial" <-- If you could link to the tutorial, that might help. Dec 19, 2017 at 15:09

I believe that this line:

``````require(checkPlayerExists(msg.sender));
``````

should actually say the opposite:

``````require(!checkPlayerExists(msg.sender));
``````

The player can't possibly exist the first time they call `bet`, so that `require` is aborting the transaction.

Assuming you're following https://medium.com/@merunasgrincalaitis/the-ultimate-end-to-end-tutorial-to-create-and-deploy-a-fully-descentralized-dapp-in-ethereum-18f0cf6d7e0e, note that they have this line, which is roughly equivalent to what I suggested. (But I believe `require` is more appropriate here than `assert`.)

``````assert(checkPlayerExists(msg.sender) == false);
``````
• Thanks for this. Why can we just use `require(checkPlayerExists(msg.sender) == false);` Dec 19, 2017 at 21:12
• That would do exactly the same thing. Dec 19, 2017 at 21:13
• Thanks!! Lastly, according to the tutorial, I should be able to interact with the contract (i.e. constants `miniumbet`) because they dont cost anything. However, when the contract is deployed, the constants are not shown in the right hand panel (i,e treated like private function) Dec 19, 2017 at 21:18
• Make sure you mark them "public". Dec 19, 2017 at 21:22
• Not sure if it changed, but state variables default to internal per the documentation: solidity.readthedocs.io/en/develop/contracts.html Dec 19, 2017 at 21:33