Handling users with mapping: a simple lottery example

In the previous tutorials we learnt the basics of smart contracts programming. It is now time to use this to build a real life example: a lottery.

The purpose of our lottery will be that multiple users (participants) will be able to send money to participate in a lottery. The more money the user will send, the higher chance he will have to win all the funds. When the owner of the lottery will decide to close the lottery, a winner will be chose and the total bets will be transferred to the winner.

To store each players bet, we’ll see a new data types which are mapping . A mapping  bind a key to a value. To declare you have to both specify the type of the key and the value. For example here we will store the money that belongs to an address:

Unfortunately there is no way to iterate the values of a mapping  if the indexes are not linear and we know the number of records. So for iterating along our bets we will need to store separately the number of people who placed bets and the list of the address of the players in another mapping.

So we’ll store 3 variables plus the address of the owner:

We’ll then make the Bet function. Same as normal accounts, smart contracts can contain Ethereum. Our function Bet needs to have the the payable modifier. We’ll talk more about modifiers in the next tutorial but it simply make it possible to send Ethereum to the contract when the function is called. The amount of sent Ether will be stored in msg.value.

So when the function is called, we’ll first check if some Ethereum was sent: msg.value > 0 . Then we will store the amount sent in the usersBet mapping. If the bet fro this user was equal to 0, we increment our nbUsers and store the address of the player so we can iterate through all the players when ending the lottery.

The last part of our basic lottery system will be to pick a winner. Our function EndLottery()  must be only accessible by the owner of the lottery. To keep it simple we’ll pick a random number between 0 and the total amount of Ethereum played. We’ll then iterate through the players and check who won. When a player is found, we’ll simply destroy the contract passing as an argument the winner’s address. He will receive all the Ethereum  that are in the contract.

A special note to say that we used a really simple way to get or winningNumber, in real life applications – especially when handling money you need to use a better way for getting real random number.

Here is all the contract code put together:

 

So let’s deploy our contract and play with it. We’ll send Ethereum using the bet function with our two accounts. As the Bet function has the payable  modifier you’ll see we can add Ether when calling the function.

 

After sending your Ethers, you’ll see that the contract now holds it.

 

If you call the EndLottery() function from the account owner, the lottery gains will be transferred to the winner and the contract will be close.

 

In this tutorial we used the payable modifier to make it possible to send Ethereum to our smart contract. In the next tutorial we’ll learn more about modifiers in Solidity.

 

Solidity Hello World with Ethereum Wallet

come to the classic Helloworld Solidity contract class using Ethereum Wallet. In this class we’ll see how to write a simple contract and deploy it on the block chain. We’ll also see how to interact with our contract by sending and reading data.

The syntax for a contract in Solidity is really similar to a class in oriented object programming languages. A contract have functions we can call and variables we can store and read.

Our Counter contract will store the number of times it has been called and make the value available for everyone to read on the blockchain.

 

To publish our contract on the blockchain, open Ethereum Wallet and go to My contracts.

Click Deploy a new contract.

In the code text area past our Counter contract code.

On the right select the contract you want to publish: our Counter contract.

Enter your password and press Send transaction. The gas price is the amount of Ethereum that will be required to publish your contract to the block chain.

You can see the counter value is equal to 0. On the blockchain, it does not cost anything to read a value, that’s why you can see the value displayed here.

Now if you execute our increment function guess what will happen? Our counter value will be equal to 1. That may take some time as the execution of the code must be written in the blockchain when the next block is mined.

If you execute the increment function another time you’ll see the counter value changing!

Congratulations, you published and interacted with your first contract. In the next tutorial we will see how we can improve our contract so only the owner (you) can interact with it.

Install Ethereum Wallet

If you don’t have a clue about what is Ethereum or a smart contract, you should first read what is Ethereum.

The fastest way to get started with Ethereum smart contract programing is Ethereum Wallet. It’s a native application available for Windows, MacOSX and Linux. You can download it from the Github project.

 

Before anything, Ethereum Wallet needs to connect to the block chain and synchronize.

 

 

Once the sync is done choose the test network. On the test network any transaction will be done with test Ethereum so it does not cost you anything to develop. Keep in mind that while you are on the test network your contracts won’t be accessible from the normal Ethereum network.

 

Then you have to choose a password and don’t forget it. You don’t have to worry too much as the password will only affect the test network.

 

Once this is done you should see the Ethereum Wallet main screen displaying your account address and current balance which should be 0.

 

On the test network you can easily get Ethereum by selecting in the menu Start mining.

 

Let the app run a little and you’ll see Ethereum in your account. It can takes around an hour to get your Ethereum. In the future, you will need a small amount of Ethereum to deploy your smart contracts to the blockchain and interact with them. You can now start writing your first smart contract.