Lottery as a Smart Contract: The Execution

This is part 2 of the “Lottery as a Smart Contract” series. In part 1, I described the business logic behind the Lottery Smart Contract. Here, I will demonstrate how this contract is executed. To run this, you will need:

  1. Remix, the Solidity IDE here.
  2. Metamask wallet with 2 accounts that have some ETH in them.
  3. The Lottery Smart Contract codes here.

The learning outcomes of this series of posts is to demonstrate the use of State and the calling of an external JSON web service using the Oraclize library. Ambitious readers, feel free to turn this into a business, although I wouldn’t try myself. The legitimacy of this contract is in question as I have explained in part 1.

Execution

Create a new contract in Remix. Copy and paste Lottery.sol into your code window. Select “Settings” in the tab to change the version of the Solidity compiler compiling this contract to 0.4.19 because this is the version of Solidity compiler that this contract was developed for.

Lottery_Execution1_0.png

Make sure that you have selected the correct account in Metamask that plays the role of the house. Deploy the contract with 1 ETH.

Lottery_Execution2_0.png

The contract will be created. Check on Etherscan to ensure that the Lottery contract has a balance of 1 ETH. It should, since you were the one who created it and added 1 ETH to it.

Lottery_Execution3_0.png

Make sure that you are now selecting the account that plays the role of the punter.

Lottery_Execution4.png

Copy and paste the address of the Contract into the “At Address” text field. This allows the punter to bet with the house, using the Lottery Smart Contract that he deployed. Assuming that the punter bets 5, enter 5 into the “bet” text field. Add 1 ETH to the contract. Then click [bet].

Lottery_Execution5_0.png

Notice in Etherscan that the Lottery Smart Contract now has 2 ETH, 1 from the house and 1 from the punter.

Lottery_Execution6_0.png

Back at the contract, click [betNumber] to find out what number the punter has bet on. It says 5. Now press [house] and [better]. Notice that the addresses of both parties are displayed.

Lottery_Execution7_0.png

We are now ready to reveal the winning number! Make sure that you are now using the house’s account since only the house can generate the winning random number. If you used the punter’s account, the Smart Contract will refuse to execute the function. Press [release].

Notice that 2 transactions are generated. The first transaction executes the release function. The second transaction makes an Oraclize query to query an external random number generating JSON web service. 

Note that Oraclize will cost ETH to execute. In Oraclize’s business model, the first web service call is always free. Subsequent calls entails a fee to the company.

Also notice that the balance of the contract is now 0, since ETH has been disbursed between the punter and the house.

Lottery_Execution8_0.png

Let’s check who won. It looks like the house did. He has 1 more ETH than before he created the Lottery Smart Contract and the punter has 1 less.

 

Lottery_Execution9_0.png

What’s Next.

In the next part of this series, I will walk through some parts of the Lottery Smart Contract codes that will be of interest to the reader. 

Stay tuned.

Photo by Jonathan Petersson on Unsplash