How to create a “Hello World” smart contract with Solidity

Time:2022-11-21

Recently, I have published several simple introductory articles to help engineers who have just entered the industry.

This article will take you through how to create a smart contract called “Hello World” using the smart contract development language Solidity. This article does not require prior knowledge, and it is easy for beginners to use. Follow the steps below to easily create your first smart contract.

What is a smart contract

Smart contracts are computer programs that are issued and run in a blockchain environment. Since it runs on the blockchain, it does not require a centralized organization or server to operate.

Due to the immutability of the blockchain, once a smart contract is issued, it cannot be updated or modified. But there may be functions written in smart contracts that have the ability to change data. Information can be recorded in one block and deleted in another, and its history is kept and can be audited.

Solidity programming language

Solidity is an object-oriented high-level programming language for implementing smart contracts. It is a [curly braces] programming language, that is, the characters “{” and “}” are used to define statement blocks.

Solidity is designed to run on the Ethereum Virtual Machine (EVM), with syntactic influences from C++, Python, and JavaScript. It is a statically typed language and supports inheritance, libraries, complex user-defined types, and more.

Remix

Remix is ​​a browser-based IDE (Integrated Development Environment) that can be used to write, compile, deploy, and debug Solidity code online. Remix has a blockchain simulator that runs in the browser, an environment called the JavaScriptVM. We’ll use it next.

Create a smart contract

Click the first button on the left activity bar: “File Explorers”

Click the “Create New File” button (Translator’s Note: In the sidebar that opens, the same below).

Name the file: HelloWord.sol.

Solidity files have a “.sol” extension.

Copy and paste the sample code below:


// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
contract HelloWorld {
    function sayHelloWorld() public pure returns (string memory) {
        return "Hello World";
    }
}

Now let’s see what’s in the smart contract.

// SPDX-License-Identifier

“//” indicates that this line is a comment, not code.

The SPDX License List Specification is a common list of licenses used in free and open or collaborative software.

Since Solidity 0.6.8, the SPDX license identifier has been introduced to allow developers to specify the license used by the smart contract.

The SPDX license identifier should be added to the top of the contract code and identified with “//”.

pragma

pragma is used to specify the Solidity version, using semantic versioning. Solidity official website has further introduction.

pragma solidity 0.8.13;

Hello World contract

This code defines a contract called “HelloWorld”.

A contract is a collection of functions and data (its state).

Once deployed, the contract will exist at an address on the Ethereum blockchain, which is further introduced on the Ethereum official website.

sayHelloWorld function

A public function is defined here, and the function of this function is to return the string “Hello World”. keywords in function declarationspureIndicates that the function does not read or modify the blockchain state.

Compile the smart contract

Find the “Solidity compiler” button in the Remix activity bar and click it.

Click the “Compile HelloWorld.sol” button.

You can also check the Enable automatic compilation (auto-compile) option.

Check whether there is a green mark on the compile button. If there is a green mark, the mouse will be put on the button and there will be a prompt message that the compilation is successful.

Deploy smart contracts

In the activity bar, click the “Deploy and run transactions” button.

Since we’re only creating one smart contract right now, the “CONTRACT” drop-down menu in the sidebar will automatically select that contract.

Click the “Deploy” button.

Interact with smart contracts

When the smart contract is deployed in Remix, we can see it in the “Deploy and run transactions” in the activity bar:

  1. Scroll down in the sidebar to find “Deployed Contracts”.
  2. Expand “Hello World”.
  3. Click on the button “sayHelloWorld”.
  4. It will return the message recorded in the contract: “Hello World”.

Congratulations, you have successfully created a “Hello World” smart contract!

Next step

Now that you have created your “Hello World” smart contract using Solidity, there are many other things you can do, such as deploying the contract to the testnet or even the mainnet, changing the message, creating a state variable to store the message, creating a function to Update the message, or save it permanently to the blockchain!

The Chainlink Chinese developer community continues to serve developers, welcome to join the Chainlink developer community through private messages.