How to make your first smart contract using Solidity

Blockchain has become a renowned known technology, It’s defined as a decentralized ledger, Distributed Ledger Technology (DLT). It is trustworthy due to the transparency of the transactions.

Before jumping to writing code, let’s first understand these concepts:

What is Solidity?

Solidity is a statically typed language. It looks like JavaScript and is the most widely used smart contract programming language.

What is a smart contract?

A smart contract establishes the terms of an agreement like any other traditional contract. The only difference is that this smart contract is executed as a code which runs on a blockchain like Ethereum.

To execute these contracts we need to pay fees called “gas”.

Once this contract is deployed, it can’t be changed.

Write your first smart contract

To follow along, we can use any code editor we like, for example, “VScode”. Alternatively, we can use a browser-based development for the smart contract “Remix IDE”.

Create a new (.sol) file named FirstContract.sol. The “.sol” file extension stands for Solidity.

Pragma version

Now open that file and write the code below inside it:

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.6.0 <0.9.0;

The first line is a license identifier.

The second line defines the pragma version so Solidity understands which version our contract is in.

This means we are defining the version to be between 0.6.0 and 0.9.0 or equal to 0.6.0.

The contract keyword

Now, let’s define our contract with the following:

pragma solidity >=0.6.0 <0.9.0;

contract FirstContract {
  
}

Here, we define the contract and name it “FirstContract” :

contract FirstContract {}

Just like the class keyword in Python, we have a contract keyword in Solidity.

Variables in Solidity

Solidity supports three types of variables:

State variables: These are variables whose values are permanently stored in contract storage.

Local variables: These are variables whose values are present till the function is executing.

Global variables: These are special variables that exist in the global namespace used to get information about the blockchain.

Let’s create a state variable called count and initialize it to 0.

pragma solidity >=0.6.0 <0.9.0;

contract FirstContract {
  uint256 count = 0; //state variable
}

The line uint256 count = 0; declares a state variable called the count of type uint256 or uint (unsigned integer of 256 bits).

Note: We can use the publicpublic key keyword to get access to a variable outside the smart contract.

The struct keyword in Solidity

Sometimes we need a more complex data type. For this, Solidity provides struct. This allows us to create more complicated data types that have multiple properties.

We will create a struct Person with two properties:

pragma solidity >=0.6.0 <0.9.0;

contract FirstContract {
  uint256 count = 0; //state variable

   struct Person {
        uint256 count;
        string name;
    }
}

The line of the struct Person is a custom-defined type that can group several variables like uint256 count; and string name;.

Arrays in Solidity

When we want a collection of something, we can use an array.

Types of arrays in Solidity:

  • Fixed arrays
  • Dynamic arrays

Let’s create an array of the “Person” struct, and name it person.

pragma solidity >=0.6.0 <0.9.0;

contract FirstContract {
  uint256 count = 0; //state variable

// defining a struct 
   struct Person {
        uint256 count;
        string name;
    }

// array of Person  
    Person[] public person;
}

Person[] public person; declares an array of dynamic size with the type of Person.

Note: When we declare an array as public, Solidity automatically creates a getter method for it.

Functions in Solidity

We create a function— addPerson— which takes two parameters: a string and a uint. It adds a person with a count number and a name to the array.

pragma solidity >=0.6.0 <0.9.0;

contract FirstContract {
  uint256 count = 0; //state variable

// defining a struct 
   struct Person {
        uint256 count;
        string name;
    }

// array of Person  
    Person[] public person;


// function declaration
   function addPerson(string memory _name, uint256 _count) public {

        person.push(Person(_count, _name));
        
    }
}

The function addPerson(string memory _name, uint256 _count)line declares a function named addPerson with two parameters and their types.

The person.push(Person(_count, _name)); adds a person with a count value and a name to the person array created before.

The memorypassing an argument by value keyword means that we are passing the first argument by value so that the Solidity compiler creates a new copy of the parameter’s value and passes it to our function.

Congratulations, we just wrote our first smart contract.

Example

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.6.0 <0.9.0;
contract FirstContract {
uint256 count = 0; //state variable
// defining a struct
struct Person {
uint256 count;
string name;
}
// array of Person
Person[] public person;
// function declaration
function addPerson(string memory _name, uint256 _count) public {
person.push(Person(_count, _name));
}
}

Code explanation

Line 1: This is the license identifier.

Line 2: This line tells Solidity which version our contract is in.

Line 4: We define the contract and name it FirstContract.

Line 5: We declare a state variable called count of the uint256 type and then pass it a value of 0.

Line 8: This is a more complex data type: struct Person with two properties.

Line 9: We declare a state variable named count of the uint256 type.

Line 10: We declare a state variable named name of the string type.

Line 14: An array of dynamic size with the Person type.

Line 17: A function named addPerson with two parameters and their types.

Line 18: The push method adds a person with a _count value and a _name to the person array created before.

Output

The output of the code above is a binary string that represents the added person.

Free Resources