Writing Your First Smart Contract Using Solidity!

Writing Your First Smart Contract Using Solidity!

ยท

3 min read

What Is Solidity?

Solidity is a High-level Object Oriented Programming Language that is primarily used for writing Smart Contracts.

A Smart Contract is a code that is useful for interacting with the Ethereum Blockchain or any other Ethereum Virtual Machine (EVM) compatible Blockchain.

Remix Setup

For writing Solidity Code, we can use various IDEs but for this specific tutorial, we'll use Remix, which is an Online Integrated Development Environment. To access it click here.

Writing Our First Contract

After setting up the Remix IDE, we right-click on the contracts folder shown in the file explorer to create a new solidity file. The file extension of the solidity file is .sol.

In this program, we'll write hello world code. So, we'll name our file as helloWorld.sol

  • Defining Solidity Compiler

To create a Smart Contract, we must define it to the compiler. The way to do it is by writing pragma solidity, followed by this we write the solidity version. In this program, we'll be using version 0.8.7.

Syntax:

pragma solidity 0.8.7;
  • Creating The Smart Contract

After this, we'll create our smart contract using the contract keyword, followed by the contract name.

pragma solidity 0.8.7;

contract HelloWorld {}
  • Creating The SayHello Function

After creating the Smart Contract, we'll write a function in it that returns the Hello World string when it is called.

pragma solidity 0.8.7;

contract HelloWorld {
    function sayHello() public pure returns(string memory){
            return "Hello World"; 
    }
}

Functions in solidity can have the following visibility:

  1. public : Can be called by contract.

  2. private : Can be called by the contract that defines it.

  3. external : Can be controlled by other contracts.

  4. internal : Can be controlled by defining a contract and any contract which inherits the defining contract.

Functions in solidity can also be declared as:

  1. pure : Doesn't read or modify the state variables.

  2. view : Reads but doesn't modify the state variables.

  3. payable : Used for receiving ethers.

The sayHello returns a string with a memory keyword which means the string is only available during the function execution.

  • Compiling The Smart Contract

Now that we have written the program, we will have to compile it before deploying it to the EVM environment. To compile it, click on the Solidity Compiler Button on the sidebar.

Then we click on the Compile helloWorld.sol button to generate the Smart Contract Bytecode which is used for deploying the Contract to the Blockchain.

  • Deploying The Smart Contract

Once the Smart Contract compiles successfully then we click on the Deploy tab, which is directly below the Compile Tab.

Make sure that the environment says Remix VM (London). Then click on the Deploy button to deploy the Smart Contract. If your contract is successfully deployed then you'll be able to see a Green Check Mark in the terminal. Also, the deployed contract gets showed up in the Deployed Contracts section.

After all of this, you can interact with your deployed contract by clicking the sayHello function which returns the output as Hello World string.

I hope that you must have found this article quite helpful. If yes, then do give a read to some of my other articles!

Who knows you might become a great programmer ๐Ÿค”!

ย