Hello World With Solidity

What Is Solidity?

Solidity is a high-level object-oriented programming language which is primarily used to write smart contracts.

A Smart contract, on the other hand, is just solidity code that is used to interact with the Ethereum blockchain or any Ethereum virtual machine(EVM) compatible blockchains.

IDE Setup

To create our hello world program we will need just a code editor, and for this article, we would be using the Remix online integrated development environment (IDE) which can be accessed here.

The starting interface should look like the screenshot below if you're using remix for the first time.

Building The Hello World Smart Contract

After setting up remix, we right-click on the contracts folder shown in the file explorer to create a new solidity file, like all other programming languages, solidity files also have an extension which is .sol, so creating a hello-world file would look like this hello-world.sol, if that was successful we should have a new solidity file created.

Defining The Solidity Compiler

Firstly to create a smart contract, we must first define the compiler to be used for it, and the way we do that is by using the pragma solidity, followed by the solidity version to use. For this article, we would be using version 0.8.7. The syntax is shown below in the snippet.

pragma solidiy 0.8.7;

Creating The Smart Contract

Next, we create our smart contract using the contract keyword, followed by the smart contract name. The contract keyword is similar although not the same as the class keyword we have in other programming languages like javascript.

pragma solidity 0.8.7;

contract HelloWorld {}

Creating the sayHello function

Now that we created the contract, we would need to create a function within the contract that returns the string Hello World 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:

  • public - any contract can call it.

  • private - can only be called by the contract that defines it.

  • external - can only be called by other contracts.

  • internal - can be called by defining a contract and any contract which inherits the defining contract.

Functions in solidity can also be declared as:

  • pure - does not read or modify state variables.

  • view - reads but does not modify state variables.

  • payable - receives ether.

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 created the program, we would need to compile it first before deploying it to an EVM environment. To compile it, we click on the solidity compiler button on the sidebar.

Then we click the Compile hello-world.sol button to generate the smart contract bytecode which is what gets deployed to the blockchain. You might get some warning here but as long as there are no errors, the code would compile successfully.

Deploying The Smart Contract

Once the smart contract compiles successfully then we click the Deploy tab which is directly below the compile tab.

Ensure the environment says Remix VM, and then whatever location you use doesn't matter. Then you can hit the deploy button in the screenshot above which deploys the smart contract. If successfully deployed you should see a green transaction in the terminal and also your deployed contract shows up in the Deployed Contracts section like the screenshot below.

You can now interact with the deployed contract by clicking the sayHello function which outputs the Hello World string.

Wrapping Up

I hope you have been able to learn something and also follow along and build the hello world program.

We were able to learn how to:

  • Define the solidity version to use.

  • Create a smart contract.

  • Create a function in a smart contract using the different types of visibilities.

  • Compile a smart contract using remix

  • Deploy a smart contract using remix

Watch out for more helpful contents on solidity from me. Don't forget to like and follow to get more articles on soldity.

Cheers 🎉.