[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 😉
Smart Contract
A smart contract is a computer program or a transaction protocol that is intended to automatically execute, control or document legally-relevant events and actions according to the terms of a contract or an agreement. The objectives of smart contracts are the reduction of need for trusted intermediators, arbitration costs, and fraud losses, as well as the reduction of malicious and accidental exceptions. Smart contracts are commonly associated with cryptocurrencies, and the smart contracts introduced by Ethereum are generally considered a fundamental building block for decentralized finance (DeFi) and NFT applications.
Vending machines are mentioned as the oldest piece of technology equivalent to smart contract implementation. The original Ethereum white paper by Vitalik Buterin in 2014 describes the Bitcoin protocol as a weak version of the smart contract concept as originally defined by Nick Szabo, and proposes a stronger version based on the Solidity language, which is Turing complete. Since Bitcoin, various cryptocurrencies support scripting languages which allow for more advanced smart contracts between untrusted parties.
The objective of this tutorial is to get you well-versed with the basic concepts of solidity and smart contract writing and get you started with your very first smart contract. At the end of this blog you will have your own Lottery smart contract up and running.
lottery smart contract
We are building a lottery smart contract where we have two main entities, the creator and the contestants. The contestants pay an entry fees that will be stored in the smart contract and enter the lottery. The creator can set the fees, change the fees, and decide when to declare the result of the lottery. When the creator ends the lottery automatically the entire amount stored in the smart contract will be transferred to the winner of the lottery, who will be selected randomly from the list of contestants.Unlike in a traditional lottery system where the creator holds the money of the contestants, in a blockchain based lottery system the funds are held in the smart contract and can only be relieved from there on ending the election.
For this tutorial we will have a very simple environment since we are only coding the smart contract and testing it manually and not writing automated tests. We will use a simple web IDE called Remix. It is one of the most used tools by early developers. it is very beginner friendly and simple.
Development
Finally the good part has arrived. First line of every solidity smart contract is a comment mentioning the standard and license used. The next line shows the version of solidity being used for this contract.Next we define the contract by using the key-word contract followed by name of the contract. All our code related to this contract will go here.
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract Lottery{}
Variable and Constructor
Next we define the variables and the constructor that we will be using. In our scope, the only variables we have to define will be the owner, and the array of contestants. We also define the default fees of the lottery here which can be changed by the owner at any time. Now coming to the constructor, it is the piece of code that runs only once, that is when the contract is deployed. When we deploy the smart contract we want the person deploying the contract to be the creator of the lottery. The term msg.sender signifies the address from which the fees to deploy the contract was paid.
address public creator;
address payable[] public contestants;
uint fees= 1 ether;constructor(){
creator = msg.sender;
}
The addresses of the contestants are of type payable which means that the smart contract will be able to pay these addresses. This is so because any address can win the lottery and the smart contract should be able to pay to that address.
Modifiers in Solidity
Next we write the functions that we will be using in this contract. Most of these functions can be called by anyone but a few functions like ending the election or changing setting the fees should be called only by the creator. For ease of coding and not repeating the require condition again and again solidity has modifiers. They are used to add prerequisites to functions. As coding practice we generally put our modifiers at the end of the contracts.
modifier onlyOwner() {
require(msg.sender == creator);
_;
}
Writing Functions
Next we write the basic functions like the get fees and set fees function. The logic for this is pretty straight forward, the get fees function calls the fees variable and returns the value of fees. Since the function only views the value and does not perform any transaction, no fees is charged and we add the view keyword. For set fees we change the value of fees variable and update it. Since only the creator can do this we add our modifier here.
function setFees(uint _fees) public onlyOwner { fees=_fees; } function getFees() public view returns (uint) { return fees; }
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