codedamn

How to create a Smart Contract in Solidity? Full Guide 2022

How to create a Smart Contract in Solidity? Full Guide 2022

A smart contract is a program written/stored on the Blockchain. And solidity is an object-oriented programming language for implementing smart contracts on various blockchain platforms, most notably, Ethereum.

Let us create our own smart contract from scratch. So if you are new to solidity and don’t know where to start then this blog can be the first step for you in your solidity smart contract journey. In this blog, we will create a lottery smart contract from scratch.

Lottery Smart Contract Algorithm

In our lottery smart contract, there will be two entities –

  1. Manager
  2. Players

Manager’s Task

  1. The manager will deploy the smart contract.
  2. Fetch the balance of the smart contract.
  3. Pick the winner of the lottery. At least three people are required to pick the winner of the lottery.

Player’s Task

  1. Participate in the lottery by paying 0.1 ether.

Building Lottery Smart Contract

To write your smart contract you can use codedamn solidity playground. Specially designed for smart contract developers.

Step 1

This first line of the smart contract will be

//SPDX-License-Identifier: GPL-3.0
Code language: JavaScript (javascript)

This is the license number that you have to give whenever you are creating a smart contract. To know more about it refer SPDX.

Step 2

The second line of the smart contract will be

pragma solidity >=0.5.0 <0.9.0;
Code language: JavaScript (javascript)

Pragmas are instructions to the compiler on how to treat the code. All solidity source code should have a “version pragma” which is a declaration of the version of the solidity compiler this code should use. This helps the code from being incompatible with the future versions of the compiler which may bring changes. The above-mentioned code states that it is compatible with compilers of versions greater than and equal to 0.5.0 but less than version 0.9.0.

Step 3

Then we will have the contract name.

//SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.5.0 <0.9.0; contract Lottery { //Write your code here }
Code language: JavaScript (javascript)

Inside the curly braces, we will write our code.

Step 4

Let’s create the two entities.

address payable[] public players; address public manager;
Code language: JavaScript (javascript)

The first line is the dynamic array of the players who will participate in the lottery.

address – This is a data type that is used to store the address of the players.

payable – This keyword is used as the players will pay ethers to participate in the lottery.

public – This is a visibility specifier to make the variable players accessible to the outside world. So that anyone can access it.

The second line is to store the address of the manager.

Both of the above variables are state variable. State variables are the variables that are declared at the contract level.

Step 5

Let’s create a constructor to store the address of the manager. Constructor is a special function which gets runs automatically when you deploy the smart contract.

constructor() { manager = msg.sender; }
Code language: JavaScript (javascript)

msg.sender contains the address of the user who is calling the constructor( ) by deploying the smart contract.

Step 6

Let’s create a way through which players can pay to participate in the lottery.

receive () payable external { require(msg.value == 0.1 ether,"Pay 0.1 ether only"); players.push(payable(msg.sender)); }
Code language: JavaScript (javascript)

The receive( ) will be called automatically when someone will pay ether to the contract.

Note – The ether will get stored in the contract balance of the smart contract.

external – This is also a visibility specifier just like we have public. Functions having external visibility specifiers are accessible to the outside world but not within the contract.

require(condition) – The require the statement will check the condition. If the condition is true then only the below statements will be executed otherwise it will throw an error – “Pay 0.1 ether only” and the transaction will get reverted back.

msg.value – This is a global variable that stores the ether when players will pay.

msg.sender – This is also a global variable that stores the address of the user who is calling the function. In this case, the user who will pay the ether that user address will be stored in the msg. sender.

payable(msg.sender) – We do this to do an explicit conversion of the msg. sender to the payable.

push() – It is a method of the dynamic array. This is to insert the address of the user in the player’s array.

Step 7

Fetch the balance of the smart contract.

function getBalance() public view returns(uint){ require(msg.sender == manager,”You are not the manager”r); return address(this).balance; }
Code language: JavaScript (javascript)

Whenever we want to create a function in solidity we start with the keyword function and the function name which is getBalance()in this case. They getBalance() can only be called by the manager of the contract that’s as we are using the require statement.

view – We use the view keyword whenever we are reading the state variables.

returns – This is to tell the data type of the variable that we are returning from the function. In this case, we are returning the balance of the contract which is an integer value that’s why returns(uint).

uint means unsigned integer.

address(this).balance – This contains the balance of the contract.

return– A return statement ends the execution of a function and returns control to the calling function. In this case, it will return the balance of the contract.

Step 8

Generate a random number for the lottery. So that we can randomly pick up the winner.

function random() internal view returns(uint) { return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, players.length))); }
Code language: JavaScript (javascript)

Warning – This random() generator is for demonstration purposes only. Don’t use it in the production apps.

Step 9

Pick the winner of the lottery.

function pickWinner() public { require(msg.sender == manager,"You are not the manager); require (players.length >= 3,"There are less than 3 players"); uint r = random(); address payable winner; uint index = r % players.length; winner = players[index]; winner.transfer(getBalance()); players = new address payable[](0); }
Code language: JavaScript (javascript)

So, at first, we are checking whether the function is been called by the manager or not. And the number of players is greater than 3 or not.

Variable r will contain the random number generated by the random(). Then we are performing the modulo % to get the index of the array who is the winner. For people who do not know % it is used to find a remainder. To learn more about this refer to the modulo operator.

And then we simply fetch the winner’s address and transfer the balance of the contract to the player’s account who won the lottery.

In the end, I am again making the player’s array empty by new address payable[](0) so that we can restart the lottery.

Full code:

//SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.5.0 <0.9.0; contract Lottery { address payable[] public players; address public manager; constructor(){ manager = msg.sender; } receive () payable external{ require(msg.value == 0.1 ether); players.push(payable(msg.sender)); } function getBalance() public view returns(uint){ require(msg.sender == manage,”You are not the manager”r); return address(this).balance; } function random() internal view returns(uint){ return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, players.length))); } function pickWinner() public{ require(msg.sender == manager); require (players.length >= 3); uint r = random(); address payable winner; uint index = r % players.length; winner = players[index]; winner.transfer(getBalance()); players = new address payable[](0); } }
Code language: JavaScript (javascript)

Conclusion

I hope by now you are feeling confident about how to create your own smart contract from scratch. If you like this blog and are interested to learn more about solidity and smart contract. Refer to the codedamn web3 learning path.

Learn programming on codedamn

Codedamn is an interactive coding platform with tons of sweet programming courses that can help you land your first coding job. Here's how:

Programming is one of the most in-demand jobs today. Learning to program can change your future. All the best!

Sharing is caring

Did you like what Kshitij wrote? Thank them for their work by sharing it on social media.