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:
- Remix, the Solidity IDE here.
- Metamask wallet with 2 accounts that have some ETH in them.
- 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.
Make sure that you have selected the correct account in Metamask that plays the role of the house. Deploy the contract with 1 ETH.
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.
Make sure that you are now selecting the account that plays the role of the punter.
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].
Notice in Etherscan that the Lottery Smart Contract now has 2 ETH, 1 from the house and 1 from the punter.
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.
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.
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.
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