this article shows how to write a Hello World smart contract program in Solidity.
You have to write the basic first program Hello World to learn any new language. It explains the basic syntax and How to compile and run the program.
If you are a beginner to blockchain coding, Follow the below steps to Write your first Hello World Smart Contract.
- Write a Simple First Application Solidity
- Compile Solidity
- Deploy and Run the program
Solidity is a high-level programming language to write a smart contract. A smart contract is an independent piece of code that runs on Ethereum Virtual Machines. It can be used to run in public Blockchain(Ethereum) and private blockchain(Hyperledger).
Solidity high-level code is compiled into low-level machine code using a solidity compiler. Machine code is nothing but byte code that runs on Ethereum virtual machine
I will write a separate post about for
- Solidity Compiler
- Ethereum Virtual Machine
Now, we use Remix IDE, a Browser-based IDE to create a contract code in solidity. It provides to write, compile and deploy solidity smart contracts
Solidity Hello World First program
In Remix IDE, Create a new file name helloworld.sol. Copy the below code to this file.
pragma solidity ^0.5.0;
contract helloWorld {
constructor() public{
}
function print () public pure returns (string) {
return 'Hello World! First Simple Smart Contract';
}
}
Output:
0: string: Hello World! First Simple Smart Contract
This program contains three parts
- Solidity version
The first line of the program is a solidity version or pragma to use in a program. It tells the solidity compiler version to compile source files
pragma solidity ^0.5.0;
pragma tells the source file is compiled with the 0.5.0 version. ^0.5.0
indicates the version 0.5.0 version or more version.
Here is a syntax
pragma solidity ([^;]+) ;
- Contract class
It is a class in Object oriented program languages such as Java and typescript.
Contains default
constructor
, called during instance creation of a contract.
constructor() public{
}
This is always called only once.
It is a contract defined and it contains a piece of code executed in the blockchain network.
- Function return a string
the function is declared with the name print and visibility is public which means access is available to everyone in the blockchain returns(string) says this function returns the data type of a string.
function print () public pure returns (string) {
How to Run solidity First Contract program
call to helloWorld.renderHelloWorld [call] from:0xca35b7d915458ef540ade6068dfe2f44e8fa733c, to:helloWorld.renderHelloWorld(), data:942ae…ae0a7, return: { “0”: “string: helloWorld” }
Write and Run the code
You can use one of the below approaches to run a program.
Online IDE: It provides a browser-based editor to write, compile, and run contracts.
- Remix IDE
: Browser-based IDE contains following features
- Writing contracts
- Compile the contract
- Deploy contracts to the local Ethereum fake blockchain network
- Run the contract
- beautification of contract code
- Linter
- Remix IDE
: Browser-based IDE contains following features
Eth Fiddle Online solidity version of Fiddle
Solidity Editors
Different IDE and Source code editors provide plugins or package to enhance
- Atom
- Visual studio code
- Intelli with solidity plugin - Install Solidity plugin to have beautify and linter code.
- Eclipse IDE
- VIM Editor
- Sublime