Loading...
In this smartbook, we'll discuss how to create a lottery contract:
we create a smart contract and they've two members one is the manager and another one is the participants.
We'll cover the topics:
Define main pragma files with solidity version
pragma solidity >=0.4.5 <0.9.0;
Create a smart contract
contract Lottery {
}
Define some variables like manager address and store address those who participate in this lottery.
contract Lottery {
address public manager;
address payable[] public participants;
}
We create a dynamic array for storing the address of participants people because they've multiple address and these address not specified means anyone can join.
Define constructor
constructor(){
manager = msg.sender;
}
We define those who one the msg.sender means those address deploy this smart contract it's the manager.
Create a modifier name "onlyManager".
modifier onlyManager{
require( msg.sender == manager );
_;
}
Because don't use require statement for checking function only call by the manager.
Create a function "receive".
receive() external payable {
require ( msg.sender != manager, "Manager not send");
require ( msg.value == 1 ether);
participants.push(payable(msg.sender));
}
In this payable function, it's a specific function declared only one time in smart contracts. Transfer some ethers to our manager contract address.
Those address give 1 ether they'll be adding in the dynamic array because this address now participates in this lottery.
Create getBalance function, this function only gives contract balance. And this function only called by owner means only manager.
function getBalance() onlyManager public view returns(uint){
return address(this).balance;
}
Create a function to generate a random number.
function random() onlyManager public view returns(uint){
return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, participants.length)));
}
This function give a random number every time. We use keccak256 algorithm to generate the hashing value. Inside this abi he caller of the function to encode the needed information like function signatures and variable declarations.
And also used some global variables:
Note:- It's not the proper way to find the random value. Because it'll give the same value after too many finds.
Create a function winner. In this function firstly define one payable function because this address accept some ethers.
function Winner() onlyManager public{
address payable winner;
uint index = random() % participants.length;
winner = participants[index];
winner.transfer(getBalance());
participants = new address payable[](0);
}
Also, create another unsigned variable name index. It'll store a reminder of random value modules our total participate length.
In modules, if you divide any denominator you'll give smaller than this value like 298 % 12. Our reminder is minimum to our 12.
0,1,2,3.....12.
It's like our index value. RIGHT!!
Then this value checks the index value in our dynamic array. And declare this index value is the winner. And send our whole contract balance amount to send to the winner's address.
If the function executes successfully then we declare the dynamic value array will be zero. This means if the winner declares and then if you want to play again firstly you want to add 1 ether and then you'll have participated.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.5 <0.9.0;
contract Lottery {
address public manager;
address payable[] public participants;
constructor(){
manager = msg.sender;
}
modifier onlyManager{
require( msg.sender == manager );
_;
}
receive() external payable {
require ( msg.sender != manager, "Manager not send");
require ( msg.value == 1 ether);
participants.push(payable(msg.sender));
}
function getBalance() onlyManager public view returns(uint){
return address(this).balance;
}
function random() onlyManager public view returns(uint){
return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, participants.length)));
}
function Winner() onlyManager public{
address payable winner;
uint index = random() % participants.length;
winner = participants[index];
winner.transfer(getBalance());
participants = new address payable[](0);
}
}