wallpaper access

How to Create a Lottery Smart Contract with Solidity

By Scofield | LearnHub_Africa | 26 May 2022


Creating a smart contract using solidity is one of the most amazing and fun parts of coding the fact that solidity is easy and user-friendly plus the world of Blockchain is bursting with new technology.

I explained how to build a simple lottery contract that allows a player to stake ETH and the pool is shuffled and any random player to win the round and as soon as a winner emerges a new pool begins

First, we will be using Remix. remix is an online editor that allows us to write smart contracts and debug them really amazing tool.

create a file name with the extension of .sol which is an extension of remix, then we begin our contract.

//SPDX-License-Identifier: MIT 
pragma solidity ^0.8.0;

The first line is a generic License while the second line tells the version of solidity we are using notice the ^ sign which means the exact version.

contract Lottery{ 
address public manager;
address[] public players;

The first line of code we write is to create a contract which is followed by the name of the contract, we create two main variables which are the manager who calls the contract and the players who stake in the lottery.

The manager will determine when the winner is picked but does not have any say or influence in who becomes the winner as we will talk about that later.

Notice we have [] square braces on the players variable, this is an array that allows multiple players to stake ETH in our contract

function lottery() public { 
___manager = msg.sender;

We create a function next that only allows the manager to call the lottery with his own address, we will see how it plays out in the coming lines.

This line means the manager must be equal to msg.sender which is a function of solidity this means not everyone who stakes can call the winner function, only the manager can.

function enter() public payable{ 
____requir e(msg.value > .01 ether);
____players.push(msg.sender);

We create a new function that allows each player to enter the lottery and then we put a require statement that they can only participate if they pay .01 ether and above, if the player puts anything lower he is sent an error message.

function random() private view returns(uint){ return uint ____(keccak256(abi.encode(block.timestamp, players)));

We create the next function that we call random and make it private this is the function that we create to make the contract randomly select a winner using the following.

Private and not public which you must have noticed we have used quite frequently, this does not make your contract secure but only limit the people that call that particular function

We used an Unsigned integer unit which is also the same thing as uint256, we also have signed integers like int8 and others.

The next line returns kaccak256 which is the same thing as Sha3 which is used hashing passwords, abi.code allows us to pass hash int our player and the timestamp of the moment the contract was called by the manager.

Note the randomness approach varies and you choose to try different methods but let's stick to this for educational purposes.

function pickWinner() public restricted{ 
uint index = random() % players.length; /
payable (players[index]).transfer(address(this).balance);
players = new address[](0); }

Notice we pick our winner we call a restricted argument and we will see that in our modifiers soon, note a modifier is a function we use to run a particular code before the contract runs others.

We call a player that was picked in our random function and we use the payable which is a function of solidity that is called when we want to pay.

We transfer the ETH directly to that contract address because you remember we collected their address in our array above.

modifier restricted(){
require(msg.sender == manager);
_;

We said the modifier is used to run a line of code that we do not want to be repetitive in our contract so we say run this on this function first and if the caller meets the objective _; run the other line.

Wow, we got to the end of our contract, you have successfully created your first Lottery contract.

In my next write-up, I will be showing how to implement the contract inside a React website and how we can call it using Web3.

You can find the full code on my GitHub

How do you rate this article?


1

0

Scofield
Scofield

Smart Contract/Solidity || Python || Reactjs || Every other thing is just hobbies


LearnHub_Africa
LearnHub_Africa

The emergence of tech has created the need for constant updates on new technology and their functions and this is what we are about, from CSS. Cybersecurity, Web3, Data Science etc. We have you covered.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.