Skip to content

Instantly share code, notes, and snippets.

Created March 8, 2018 20:31
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save anonymous/9bd71a676e6f96f096b14a8afe6e368b to your computer and use it in GitHub Desktop.
Save anonymous/9bd71a676e6f96f096b14a8afe6e368b to your computer and use it in GitHub Desktop.
Created using browser-solidity: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://ethereum.github.io/browser-solidity/#version=soljson-v0.4.21+commit.dfe3193c.js&optimize=false&gist=
pragma solidity ^0.4.10;
contract Ownable {
address owner;
function Ownable() public {
owner = msg.sender;
}
modifier Owned {
require(msg.sender == owner);
_;
}
}
contract Mortal is Ownable {
function kill() public Owned {
selfdestruct(owner);
}
}
contract Casino is Mortal{
uint minBet;
uint houseEdge; //in %
event Won(bool _status, uint _amount);
function Casino(uint _minBet, uint _houseEdge) payable public {
require(_minBet > 0);
require(_houseEdge <= 100);
minBet = _minBet;
houseEdge = _houseEdge;
}
function() public { //fallback
revert();
}
function bet(uint _number) payable public {
require(_number > 0 && _number <= 10);
require(msg.value >= minBet);
uint winningNumber = block.number % 10 + 1;
if (_number == winningNumber) {
uint amountWon = msg.value * (100 - houseEdge)/10;
if(!msg.sender.send(amountWon)) revert();
emit Won(true, amountWon);
} else {
emit Won(false, 0);
}
}
function checkContractBalance() Owned public view returns(uint) {
address _contract = this;
return _contract.balance;
}
}
@L9m
Copy link

L9m commented Mar 19, 2018

image
Is there any way I can fix this?

@ziggahunhow
Copy link

If you are still having this problem, check to make sure that when you are betting you have entered 1 ether in the value field.

@abhishek-onestomedia
Copy link

hi @ziggahunhow

I hope you are doing good.

Firstly thanks for your this wonderful article.

I am just not getting it work not even downloaded stuff not working.Please help me out.

My code:

pragma solidity ^0.4.10;

contract Ownable {
address owner;
constructor() public {
owner = msg.sender;
}

modifier Owned {
require(msg.sender == owner);
_;
}
}

contract Mortal is Ownable {
function kill() public Owned {
selfdestruct(owner);
}
}

contract Casino is Mortal{
uint minBet;
uint houseEdge; //in %

event Won(bool _status, uint _amount);

constructor(uint _minBet, uint _houseEdge) payable public {
require(_minBet > 0);
require(_houseEdge <= 100);
minBet = _minBet;
houseEdge = _houseEdge;
}

function() public { //fallback
revert();
}

function bet(uint _number) payable public {
address _contract = this;
require(_number > 0 && _number <= 10);
require(_contract.balance >= minBet);
uint winningNumber = block.number % 10 + 1;
if (_number == winningNumber) {
uint amountWon = _contract.balance * (100 - houseEdge)/10;
if(!msg.sender.send(amountWon)) revert();
emit Won(true, amountWon);
} else {
emit Won(false, 0);
}
}

function checkContractBalance() Owned public view returns(uint) {
address _contract = this;
return _contract.balance;
}
}

But always having error when i am passing 2 first time.

untitled

Thanks

Please help me out.

@stadja
Copy link

stadja commented Apr 5, 2019

New smart contract to be up to date with solidity and remix:

pragma solidity ^0.5.1;

contract Ownable {
  address payable owner;

  constructor () public {
  //Set owner to who creates the contract
  owner = msg.sender;
  }

  //Access modifier
  modifier Owned {
    require(msg.sender == owner);
    _;
  }
}

contract Mortal is Ownable {
  //Our access modifier is present, only the contract creator can      use this function
  function kill() public Owned {
    selfdestruct(owner);
  }
}

contract Casino is Mortal{
  uint minBet;
  uint houseEdge; //in %

  //true+amount or false+0
  event Won(bool _status, uint _amount);

  constructor(uint _minBet, uint _houseEdge) payable public {
    require(_minBet > 0);
    require(_houseEdge <= 100);
    minBet = _minBet;
    houseEdge = _houseEdge;
  }

  function() external { //fallback
    revert();
  }

  function bet(uint _number) payable public {
    require(_number > 0 && _number <= 10, "Bet must be between 1 and 10");
    require(msg.value >= minBet, "Bet must be bigger than minimum Bet");
    uint winningNumber = block.number % 10 + 1;
    if (_number == winningNumber) {
      uint amountWon = msg.value * (100 - houseEdge)/10;
      if(!msg.sender.send(amountWon)) revert();
      emit Won(true, amountWon);
    } else {
      emit Won(false, 0);
    }
  }

  function checkContractBalance() Owned public view returns(uint) {
    return address(this).balance;
 }

  function checkMinBet() Owned public view returns(uint) {
    return minBet;
 }
}

@krittawatcode
Copy link

krittawatcode commented Nov 17, 2022

here is another update

pragma solidity 0.8.7;

contract Ownable {
    address owner;

    constructor() {
        //Set owner to who creates the contract
        owner = msg.sender;
    }

    //Access modifier
    modifier Owned() {
        require(msg.sender == owner);
        _;
    }
}

contract Mortal is Ownable {
    //Our access modifier is present, only the contract creator can      use this function
    function kill() public Owned {
        selfdestruct(payable(owner));
    }
}

contract Casino is Mortal {
    uint256 minBet;
    uint256 houseEdge; //in %

    event Won(bool _status, uint256 _amount);

    constructor (uint256 _minBet, uint256 _houseEdge) payable {
        require(_minBet > 0);
        require(_houseEdge <= 100);
        minBet = _minBet;
        houseEdge = _houseEdge;
    }


    function bet(uint256 _number) public payable {
        require(_number > 0 && _number <= 10, "Bet must be between 1 and 10");
        require(msg.value >= minBet, "Bet must be bigger than minimum Bet");
        uint256 winningNumber = (block.number % 10) + 1;
        if (_number == winningNumber) {
            uint256 amountWon = (msg.value * (100 - houseEdge)) / 10;
            if (!payable(msg.sender).send(amountWon)) revert();
            emit Won(true, amountWon);
        } else {
            emit Won(false, 0);
        }
    }

    function checkContractBalance() public view Owned returns (uint256) {
        return address(this).balance;
    }

    function checkMinBet() public view Owned returns (uint256) {
        return minBet;
    }


    fallback() external payable {
        //fallback
        revert();
    }

    receive() external payable {
        revert();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment