Writing our Smart Contract

Before we start writing code like headless chickens we must first analyze which components we need:

We need to know the owner of the contract and have an access modifier (we will leave changing owner out of it for simplicity) The owner of the contract can destroy the contract and retrieve the balance A user can bet a number between 1–10 The owner needs to be able to set a minimum bet amount and house edge on contract creation (not changeable after creating for simplicity)

Steps one and two are very easy, we’ve also added comments so you have no trouble following along. Fire up Remix and let’s get to work (gist link at the end of article):

pragma solidity ^0.4.10; contract Ownable {

address owner;

function Ownable() 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);

}

}

First we create the contract Ownable, it’s constructor function Ownable() will be called upon creation and set the state variable ‘owner’ to the address of the creator. We also define an access modifier that will throw an exception when the caller of a function we attach it to is not the contract owner.

We pass on this functionality into our Mortal contract (Mortal inherits from Ownabe). This has one function that allows the contract owner (access modifier) to destroy the contract and send the remaining funds back to him.

Did you manage to get this far? You’re doing great! We’re almost ready with our contract.

Now for step 3 and 4 we will create our Casino contract:

The first thing we’ll need is a minBet and houseEdge that can be set upon contract creating. This is done by passing parameters to the constructor Casino(). We will make our constructor payable so we can preload our contract with some Ether on deployment. We’ll implement our fallback already as well:

contract Casino is Mortal{

uint minBet;

uint houseEdge; //in % //true+amount or false+0

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();

}

}

This won’t do much yet so next up we’ll add our function to bet a number. This function will generate a random number (not in a secure way!), then calculate and send the amount won. So below your fallback function add the following:

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);

}

}

To generate a random number between 1–10 we take the current block number and take the modulus (division remainder) of the current block number. This will always result in a number between 0–9 so if we add 1 to it we will get a “random” number between 1–10.

Example: if we deploy our contract on remix with the javascript VM in a fresh incognito window and call the bet function after deployment we will always get 2 as winning number. This is because the first block is #1. The modulus of 1 is 1, adding 1 to that gives 2.

** Note that this is not truly random as it is easy to predict the next block number. To learn more about randomness in solidity please check out https://www.youtube.com/watch?v=3wY5PRliphE.

To calculate the amount won we are simply calculating a multiplier:

bet * (100 — houseEdge)/10

if the house edge is 0 our multiplier will be 10; if the house edge is 10% the multiplier will be 9.

Lastly we will add a function for the owner to check the balance of the contract, ideally we would want to add a withdrawal function for the owner as well but we’ll leave that out for now. Below your bet function add the following few lines:

function checkContractBalance() Owned public view returns(uint) {

return address(this).balance

}

Great work! The contract is now ready for testing!