Skip to main content

Hello, Web3!

Solidity is the programming language used for Ethereum smart contracts on the Ethereum Virtual Machine (EVM). In this tutorial, we will use Remix, an IDE (Integrated Development Environment) recommended by Ethereum to write and test Solidity contracts. In just three lines of code, we will create our first Solidity program called "HelloWeb3."

Prerequisites​

Before you begin, make sure you have the following:

  • Basic knowledge of programming concepts and syntax
  • A web browser to access the Remix IDE
  • Access to the Ethereum network or Remix's built-in JavaScript virtual machine (VM)

Step 1: Open Remix IDE​

Go to remix.ethereum.org to open the Remix IDE in your web browser. Once the IDE loads, you will see three buttons on the left menu: File, Compile, and Deploy. We will use these buttons to create and deploy our Solidity contract.

Step 2: Create a Solidity Contract​

Click on the "Create New File" button and name the file "HelloWeb3.sol". In the new file, copy and paste the following three lines of code:

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;

contract HelloWorld {
string public message;

constructor() {
message = "Hello, Web3!";
}
}

Let me explain what each line of the code does:

  • Line 1: This is a comment that specifies the license for the code, which is GPL-3.0. Comments in Solidity start with two forward slashes (//) and are ignored by the compiler.
  • Line 2: This line specifies the version of Solidity to use for the program. The "^" character means that the code can be compiled with a compiler version equal to or greater than 0.8.4 but less than 0.9.0. Solidity statements end with a semicolon (;).
  • Line 4: This line declares a Solidity contract called "HelloWorld".
  • Line 5: This line declares a public string variable called "message".
  • Line 7: This is a constructor function that runs when the contract is deployed to the network. It sets the value of the "message" variable to "Hello, Web3!".

Now, when you deploy this contract and access the "message" variable, you will see the string "Hello, Web3!" displayed.

Step 3: Compile the Contract​

To compile the contract, press the "Compile" button on the left menu, and the Remix compiler will compile the code. If there are no errors, the compiler will produce an output in the "Compilation Details" section.

Step 4: Deploy the Contract​

To deploy the contract, click on the "Deploy & Run Transactions" button on the left menu. By default, Remix will deploy the contract using its JavaScript VM, which acts like a test network. Remix will also allocate a few test accounts with 100 ETH each for you to use. Once you click the "Deploy" button, the contract will be deployed to the VM.

Step 5: Test the Contract​

Once the contract is deployed, click on the "_string" variable to see the message "Hello Web3!" in the right-side panel. Congratulations, you have created your first Solidity contract!

Conclusion​

In this tutorial, we learned the basics of Solidity and how to create a contract using the Remix IDE. Although the contract we created was simple, it introduced us to important Solidity concepts like versioning, comments, and contract structure. As you continue learning Solidity, you will encounter more complex contracts and concepts, but this simple "Hello World" example is an excellent starting point.****