Selecting a winner based on the ticket number
In the previous recipe, we learned how to create a basic decentralized token and the procedure to record a bet. A lottery is more interesting when there is a prize involved. The logic for selecting a winner can be something like verifying the guess against some future block hash or number. We can also add a feature to decide the winning amount based on how accurate the prediction was.
In this recipe, you will learn how to select a winner based on logic in the smart contract. You will also learn how to calculate the winning amount and transfer it to the player.
Getting ready
You need to have a working installation of Ethereum (geth
, Parity
, ganache
, and so on) or the Remix IDE to test this recipe. The contract inherits the basic lottery contract created in the previous recipe. It is recommended to step through that before continuing with this recipe.
The contracts given are for illustration purposes only and cannot be used in real-world systems.