Lottery Smart Contract on CSC -Part2

By akohad Dec5,2022

[ad_1]

Hey Hey Hey!

ZeroXlive is here for Coinex Smart Chain Academy 😃

in this tutorial we want to write lottery smart contract using Solidity on Coinex Smart Chain. stay with me 😉

Entering the contestant

Here any user that calls this function and sends the fees amount along with this call is registered for the lottery. We first check that the amount being sent(msg.value) is exactly equal to the fees and then we add the address of the function caller(msg.sender) into the contestants array. This function has the payable keyword since the smart contract is receiving funds in this function.

function enter() public payable  {         require(msg.value == fees, "Enter fees amount only as value");         contestants.push(payable(msg.sender));              }

Checking the prize money

The get balance just like the getFees function gets us the amount of funds present in the smart contract. The this keyword is something to look at here and it always refers to the contract that is being used in. We typecast it into address type and return the balance.

function getBalance() public view returns (uint) {
return address(this).balance;
}

Declaring the result

The logic for this would be to generate a random range in the starting and ending index of the contestants array and declare him as the winner. However there is one problem. There is no true random number generator in solidity. Blockchain being a state complete machine does not support truly random numbers. Thus we have to use Pseudo random number generating algorithms. This problems can be solved by use of Oracles. In this implementation we will use a pseudo random number generating algorithm. We hash the block generation timestamp and the creator’s address and then modulo it with the length of the array. Solidity provides us with keecak256 hashing algorithm which we use for hashing. Keeping this function private or public does not make much of difference but since we are doing this for learning it has been kept public. Once the random number is generated we transfer the amount present in our contract to that address and then we reset the array as the lottery is now over and a new lottery can be started again.

the full code should look like this:

//SPDX-License-Identifier: MIT                              pragma solidity ^0.8.0;                                                           contract Lottery{                                 address public creator;                                 address payable[] public contestants;                                 uint fees= 1 ether;                                                                  constructor(){                                     creator = msg.sender;                                 }                                                               function enter() public payable{                                     require(msg.value == fees, "Enter fees amount only as value");                                     contestants.push(payable(msg.sender));                                                                      }                                                               function getRandomNumber() public view returns(uint){                                     return uint(keccak256(abi.encodePacked(creator, block.timestamp)));                                 }                                                               function selectWinner() public onlyOwner{                                     uint winningIndex = getRandomNumber() % contestants.length;                                     contestants[winningIndex].transfer(address(this).balance);                                     contestants=new address payable[](0);                                 }                                 function getBalance() public view returns (uint) {                                     return address(this).balance;                                 }                                 function setFees(uint _fees) public onlyOwner{                                     fees=_fees;                                 }                                 function getFees() public view returns (uint){                                     return fees;                                 }                                 modifier onlyOwner() {                                   require(msg.sender == creator);                                   _;                                 }                             }

Raw: https://pastebin.pl/view/raw/b0631418

There we go we have our Lottery smart contract ready. Now what you need to do is try it and test it by yourself on remix. The IDE is very intuitive to use and by playing around and testing the features you will learn even more.A few common mistakes one could make are:-

  1. While entering the lottery make sure to set the value exactly equal to the fees and make sure the unit is set to Ether. By default it is set to Wei.
  2. Make sure to call the onlyOwner functions using the creator address. That will give you an error.
  3. While entering arguments like that of fees , again the default is set to Wei and you will have to add the required number of zeroes, remember (1ETH=1⁰¹⁸Wei).

New to trading? Try crypto trading bots or copy trading

Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News

Also, Read

[ad_2]

Source link

By akohad

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *