Solidity Hello World

Solidity Hello World

Writing your first smart contract in Solidity and understating the anatomy of a Solidity contract

ยท

4 min read

In Ethereum environment, the smart contracts are written in Solidity.

If you want to learn more about the smart contracts, then you can visit my previous article on smart contract here ๐Ÿ‘‰ What is a Smart Contract.

Best way to understand any concept is by practicing it ๐Ÿ›  .

๐Ÿš€ Let's write our first smart contract in Solidity .

  • Start With IDE : The best suited IDE, to start writing smart contracts in solidity is Remix.

  • In Remix Workplace, create a new file with '.sol' (.sol for solidity) extension under contracts folder.

  • Define License Identifier : Solidity Smart Contracts are deployed on chain, therefore anyone can read the source code. In order to deal with the issues related to Copywrite, solidity compiler encourages defining the license identifier. Since we are writing our hello world smart contract we can use a special value 'UNLICENSED' for SPDX-License-Identifier. If we don't define the license identifier, the program will anyway run with a warning but its a always good practice to include the license identifier at the beginning of a smart contract.
// SPDX-License-Identifier: UNLICENSED
  • Pragmas : Pragma defines the specific complier version targeted for the smart contract code. Since solidity is ever evolving. Therefore, compiling a solidity smart contract written considering a earlier version of complier on a latest version of complier may run in to compatibility issues. So defining the targeted complier version avoids such possible compatibility issues. For our hello world smart contract we will use the latest one i.e. 0.8.10
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.10;
  • Contract : So we have defined our environment, now we will start writing our contract. A contract starts with the 'contract' keyword followed by the name of contract. We will name our contract as 'Hello World' .
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.10;

contract HelloWorld {

}
  • Function : As we know a contract holds both, the data and the logic, the logic of a smart contract is wrapped within a function (i.e. Executable piece of code). A Function starts with the 'function' keyword , followed by the name of the function and curly braces. For our hello world function we will use the name 'SayHello'
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.10;

contract HelloWorld {

    function SayHello() public  {

    }
}

Notice that we've used 'public' modifier here. The public modifier tells the compiler that anyone can access this function.

Other available modifiers that can be used are:- private, internal & external.

Now let's define a variable named 'greet' of 'string' data type.

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.10;

contract HelloWorld {

    function SayHello() public  {
       string greet = "Hello ! ";

    }
}

But this piece of code will run in to compilation error, because we did not define the Data Location of variable greet .

But what is data location in Solidity ๐Ÿ™„ ?

Data location specifies where the value of a variable should be stored.

Solidity provides following types of data locations.

  1. Storage : When a variable is a state variable (store on blockchain)
  2. Memory : When a variable is in memory and it exists while a function is being called
  3. Calldata : It is a special data location that contains function arguments, only available for external functions

Since we are defining and using our variable inside a function we will use the 'memory' data location.

Till now, we know how to write a function and declare a variable in solidity.

Next, we will pass a variable and return a custom greeting message suffixed with the value of passed parameter.

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.10;

contract HelloWorld {

    function SayHello(string memory name) public returns (string memory) {
       string memory  greet = "Hello ! ";
       return string(abi.encodePacked(greet,name));
    }
}

This is how our final program will look like

Let's deconstruct ๐Ÿ•ถ , what we've written above:-

  • returns :- returns denotes, this function returns something
  • (string memory) :- the data type of return value is of type 'string' and it's data location is 'memory' .
  • abi.encodePacked(str1,str2) :- The function abi.encodePacked() concatenates it's input parameters and results in a bytes array in the memory. In our example we have concatenated the greet (local variable) and name(function parameter). Since the output of abi.encodePacked is bytes array we have wrapped it inside string() function.

How to run it on Remix?

  • Go to Compiler tab (On Left)
  • Press 'Compile your_file_name.sol'.
  • You'll be able to see the compilation result
  • If the compilation is successful, you can deploy your contract
  • You can see your contract by expanding the 'Deployed Contract' at bottom.
  • Input the value of 'name'
  • Click on 'SayHello' button and you'll be able to see the output.

Congratulations ! You've successfully written your first program of solidity ๐Ÿ™Œ.

PS : I write about my Web3/Web2 learning experience here, if you loved this, please follow for more such content in future.

This post is created with โค๏ธ by Shivam

ย