0

I got the following code from this tutorial :

https://medium.com/@merunasgrincalaitis/the-ultimate-end-to-end-tutorial-to-create-and-deploy-a-fully-descentralized-dapp-in-ethereum-18f0cf6d7e0e

pragma solidity ^0.4.11;

contract Casino {


    uint minimumBet;
    uint totalBet;
    uint numberOfBets;
    uint maximumAmountsOfBets = 100;
    address[] players;

    struct Player {

        uint amountBet;
        uint numberSelected;
    }


    mapping(address => Player) playerInfo;


    address owner; // Long string from metamask


    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++) {
            address playerAddress = players[i];
            if(playerInfo[playerAddress].numberSelected == numberWinner){
                winners[count] = playerAddress;
                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.

  • 2
    "from a tutorial" <-- If you could link to the tutorial, that might help. – user19510 Dec 19 '17 at 15:09
3

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);
  • 1
    Thanks for this. Why can we just use require(checkPlayerExists(msg.sender) == false); – Samuel Dare Dec 19 '17 at 21:12
  • 1
    That would do exactly the same thing. – user19510 Dec 19 '17 at 21:13
  • 1
    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) – Samuel Dare Dec 19 '17 at 21:18
  • 1
    Make sure you mark them "public". – user19510 Dec 19 '17 at 21:22
  • 2
    Not sure if it changed, but state variables default to internal per the documentation: solidity.readthedocs.io/en/develop/contracts.html – user19510 Dec 19 '17 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.