89DEVs

Hello World in Solidity

To get started in Solidity, we write a simple contract that prints "Hello World". To make it as easy as possible for us, we'll use the Remix online IDE, so no compiler has to be installed on the computer. We then compile the Hello World Contract on Remix and deploy it to a sandbox blockchain in the browser. First, let's see the complete contract, then we'll go through it line by line and finally we compile and deploy it.


// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

contract HelloWorld {
    string public greet = "Hello World! :)";
}


The HelloWorld contract is very simple and consists of only a few lines of code. It is perfect as our first contract in the Solidity programming language. We'll discuss the code now line by line.

// SPDX-License-Identifier: MIT

The SPDX License Identifier is used to specify the license of our contract. This is necessary because the source code of a smart contract is usually public. In this example, the license is specified as an MIT license, which means that the code is open-source and can be used by others. However, the author of the code is not liable for damages and the license can not be removed. If we don't want our code to be used by other blockchain developers, we can use UNLICENSED as SPDX License Identifier. It is important to define this line of code because otherwise, the compiler will raise a Warning. It is best practice to specify the license in the first line of code.

pragma solidity ^0.8.7;

The second line of code defines the compiler version that works with our code. This is important if breaking changes are introduced into Solidity. The next version with breaking changes will be version 0.9.0. We have specified the accepted compiler version as 0.8.7 and above but below 0.9.0 (because of the breaking changes that might be introduced). Learn more about pragma in Solidity. To define the exact version we can write the version without the Caret sign (^), the code will then compile only on the defined version.

contract HelloWorld {}

The next line creates a new contract named HelloWorld. A contract is similar to classes in other programming languages. It is essentially a collection of functions and data. We'll learn more about contracts later.

string public greet = "Hello World! :)";

Within the contract, on the next line, we declare a variable. The variable has the name greet, the data type string and the visibility public. The string literal "Hello World! :)" is assigned to the variable. The visibility is public, so we can access the variable after deploying our Hello World contract to our sandbox blockchain.

Now it's finally time to compile and deploy the contract. Let's head over to Remix and create our HelloWorld contract. 

First, we create a new file in the File Explorer by clicking on the little icon under our default_workspace.

create a new File in Remix

Then, we name the new file HelloWorld.sol ... this can be renamed later. The ending .sol indicates that our code is written in the Solidity programming language.

rename the new File in Remix

Next, we copy our code from above into the editor and it is automatically saved by Remix.

paste the code into the Remix editor

Finally, we switch to the Solidity Compiler to compile our code. There are options to select the compiler version, the language (Solidity and Yul are supported by remix) and the EVM Version. We can select any compiler version above 0.8.7 to compile the code. After we click Compile HelloWorld.sol the code is compiled and hopefully no errors and warnings are shown. 

In the file explorer a new folder build-info is created that includes some json files, we'll learn about this later. A useful shortcut to compile code in Remix is to use the STRG+S on the keyboard.

compile the Solidity Code in Remix

Now we can switch to the next Tab and Deploy and Run our HelloWorld contract. All that needs to be done, is to click the orange Deploy button to deploy our contract to a sandbox blockchain.

deploy and run the solidity contract in remix

After the contract is deployed it is shown under Deployed Contracts.

see the deployed contract in remix

We can open the deployed contract by clicking on it and there we can see our variable greet. A click on the grey button and the value of greet is returned. As expected its value is the string Hello World! :)

get the variable value of deployed contract in remix