Solidity: Writing Your First Smart Contract - Step-by-Step Tutorial

·

3 min read

Welcome to Shankar's POV.

Welcome to my beginner's guide on writing your first smart contract! In this step-by-step tutorial, I will take you through creating your smart contract, even if you have little to no prior experience in programming or blockchain technology.

Make sure all of you have installed Remix on your computer or have access to Remix online IDE. If you haven't installed it yet, I recommend watching this blog for reference.

Open Remix IDE and Click on File Explorer in the left corner, create a workspace, and give it a name. The folder that is created comes with a default template that we usually don't need, so you can go ahead and delete it. Instead, create a new folder, and inside it, create a new file called HelloWorld.sol. Remember, the .sol extension is used for Solidity smart contracts.

In Solidity, we use the keyword 'pragma' to define the version of the programming language. Since Solidity is continuously updated with newer versions, it is important to specify the version we are using. In this code, the version should be equal to or greater than 0.8.5 but less than 0.9.0.

The smart contract is defined using the keyword 'contract', and its name is Hello. Inside the contract, we declare a variable named the number of types 'uint256', representing an unsigned integer.

To interact with the variable, we have defined two public functions. The first function is called store, and it is used to update the variable's value. The second function is called get, and it is used to retrieve and return the value of the variable when requested.

// SPDX-License-Identifier: MIT
// Solidity Compiler Version must be greater than or equal to 0.8.5 and less than 0.9.0

pragma solidity ^0.8.5;  

contract Hello {        // Smart contract with the name HelloWorld is created
    uint256 number;   // Declared a state variable number of type uint256 (Unsigned integer with 256 bits)

    // Function to store the variable
    function store(uint256 num) public{
    number = num;
    }

    // Function to retrieve the variable
    function get() public view returns(uint256) {
    return number;  // Returns the variable
    }    

}

After writing your 1st code, compile your Counter contract in the "Solidity compiler" module of the Remix IDE by selecting the compiler version and proceeding to the Compile option.

Then move on to the "Deploy & run transactions" module and select 'Hello. sol' in the contract input field, and click on the "Deploy" button.

The smart contract is successfully deployed, and the status, transaction hash, and all other details are listed in the left plane of the window.

The "store" module allows storing the value, and the corresponding transaction details are displayed, whereas the "get" module retrieves the values and has details given.

In conclusion, we have completed a detailed beginner's guide on writing your first smart contract. Follow for more detailed content. Happy coding!