Smart Contract 101: What is the Structure of a Contract? - Blockchain.News

Smart Contract 101: What is the Structure of a Contract?

A better understanding of the Blockchain Structure could help us learn how to create smart contracts efficiently, here is a simple overview as part of our Learn series.

20201028_what is the sturucture of a contract_feature.jpg

Below is an example of a simple smart contract. Let's observe the structure of the contract and go through each line of its code.

pragma solidity >=0.4.16 <0.8.0;
contract FirstContract{
    string _name;
    function setName(string name) public{
        _name = name;
    function getName() view public returns(string){
        return _name;

The First Line pragma solidity >=0.4.22 <0.6.0; is telling the smart contract compiler which version of solidity compiler code you are using.

At line 2, FirstContract could be replaced by any text, this is the place for you to name or title your contract.

Inside the curly brackets, there is a string variable called _name, this line of code will record a value to be stored on the blockchain

Also, there are two functions: setName() and getName().

setName takes a string input name. Its type of visibility is public, which means it can be called both internally and externally. Inside the function, the parameter name will be replacing the existing value stored by _name. As this function will change data on the blockchain, it will cost ether.

Another function getName, view means the function is read-only. This function returns a string value _name which is the same as the one we stored on the blockchain.

The combination of the above components creates a simple smart contract.

In the next lesson, we will describe the life-cycle of developing smart contracts.

Image source: Shutterstock