Introduction
10 minutes of reading
We'll be starting our journey using a simple smart contract written in Solidity so as to give you a quick glance of the core building blocks of a Solidity smart contract. This smart contract can be used to store an integer on the blockchain.
First of all, we need an IDE for writing Solidity contracts and we'll be using Remix, which is a popular browser-based IDE specialized for writing smart contracts on Ethereum.
You'll be seeing some folders and files in the file explorers, they might not be exactly the same because Remix is updated quite frequently. But the only folder you need to care about is the contracts folder. Hover your mouse over, right click on it and select New File to create a new file.
Creating new file in the contracts folder
Name the file as Storage.sol, all your Solidity scripts end with .sol.
Storage.sol location
You can omit all other files in your workspace for now, they are just sample files generated by Remix, most likely you won't need them. Now, paste the following content in Storage.sol.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// SPDX-License-Identifier: GPL-3.0
 
pragma solidity >=0.7.0 <0.9.0;

contract Storage {
  
    uint256 number;

    function store(uint256 num) public {
        number = num;
    }

    function retrieve() public view returns (uint256){
        return number;
    }
}
In most smart contract scripts, there is a SPDX License Identifier on top which specifies the kind of license that your script has. That's because trust in smart contracts can be better established if their source code is available. Since making source code available always touches on legal problems with regards to copyright, the Solidity compiler encourages the use of machine-readable SPDX license identifiers. Every source file should start with a comment indicating its license and the compiler will include the supplied string in the bytecode metadata.
// SPDX-License-Identifier: MIT
If you do not want to specify a license or if the source code is not open-source, you can use the special value UNLICENSED.
// SPDX-License-Identifier: UNLICENSED
In line 3, there is a special keyword called pragma in Solidity, this keyword is used to enable certain compiler features or checks. A pragma directive is always local to a source file, so you have to add the pragma to all your files if you want to enable it in your whole project. If you import another file, the pragma from that file does not apply to the importing file.
Because solidity is a constantly updating language, every Solidity file should specify the Solidity version to ensure the contract behaves the way it's intended. The line below asks the compiler to use any of the Solidity versions larger than or equal to 0.6.0 but less than 0.9.0.
pragma solidity >=0.7.0 <0.9.0;
This specifies the exact version 0.6.0.
pragma solidity 0.6.0;
This specifies any versions above 0.6.0 and below 0.7.0.
pragma solidity ^0.6.0;
From line 5 to 16, it defines a contract called Storage, the contract keyword is similar to the class keyword in other programming languages, but rather than defining a blueprint of a class object, it defines a blueprint of a smart contract which will be instantiated when the contract is deployed on the Ethereum network. It's not required to name your contract the same as the name of your script but it's recommended to do so.
contract Storage {
 
    uint256 number;

    function store(uint256 num) public {
        number = num;
    }

    function retrieve() public view returns (uint256){
       return number;
   }
}
At line 7, we declared a state variable, which is a variable whose value is permanently stored in the contract. If you are coming from other programming languages you could consider a state variable as an instance variable, so each state variable belongs to an instance of the contract.
    uint256 number;
From line 9 to 11, we declared a public function called store which takes in an unsigned 256-bit integer called num as its argument. The public keyword means it is a function that can be used anywhere.
    function store(uint256 num) public {
        number = num;
    }
From line 13 to 15, we again declared a public function, but this time it doesn't take in any argument and it returns an unsigned 256-bit integer. The view keyword tells the compiler that this is a special kind of function called a view function, that means this function doesn't modify any state variables in the contract, it only retrieves the value of the state variable.
    function retrieve() public view returns (uint256){
        return number;
    }
Now we understand how this contract works, we'll deploy it on a blockchain so that an instance of this smart contract will be created and we can interact with it.
In the left panel, choose the second tab which says Solidity compilerwhen you hover on top. Select a compiler which matches the compiler version you specified in the pragma. In this case, we are choosing 0.8.13.
For convenience, you can check the Auto compile option, in this case, whenever you make changes to the scripts within the contracts folder they will get compiled automatically. You can also compile the scripts by holding Ctrl + X on Windows or Cmd + X on Mac machines. Alternatively, you can just click on the blue button which says Compile [filename].
The optimization option could be checked if you want to reduce the cost of deploying your smart contract. The higher optimization level you instruct the compiler to perform, the lower the cost of deployment, but the higher the cost of interacting with the smart contract after it's deployed on the blockchain. If you are unfamiliar with the concepts we just said here, it's highly recommended for you to go through our blockchain course. In short, you need to pay for deploying a smart contract on a blockchain or modifying the state variables in a contract after it's deployed, these fees are called the transaction fees and the currency you are using to pay on the Ethereum blockchain is called ether.
Last but not least, make sure you are selecting the contract that you want to compile.
Storage.sol location
Once we have got the contract compiled, we can deploy it. We'll be deploying the contract on a virtual machine, i.e. a simulated blockchain. Make sure you have chosen JavaScript VM as your environment. Remix has provided you with a lot of testing accounts, each of them contains testing ethers for you to pay for the transaction fee and manipulate. The account that you have selected will be paying the transaction fee for deploying the smart contract so you should be seeing the amount of ethers it owns decrease after you click the Deploy button. Before clicking the Deploy button, be sure you are selecting the compiled contract that you want to deploy, in this case, it's our Storage contract. After selecting the right contract, you can proceed to press the Deploy button.
Remix Deploy and Run Transactions
After the deployment has succeeded, you will see an output message of the virtual machine in the console which specifies the details of this transaction. The console is located at the bottom of Remix.
Remix Deploy and Run Transactions
For example, you can see the transaction fee above is 126577 wei, since 1 ether = 1018 wei, we can see that by adding the transaction fee to our current balance, it adds up perfectly to 100 ethers, which is our original balance of the account.
account balance after deploying a contract
Now, our contract is deployed, we can start playing with it. In the Deploy & run transactions tab, you should be able to see all the contracts deployed by you. Each transaction is deployed at a specific address on the blockchain, as you can see below, the contract we just deployed is located at 0xd9145CCE52D386f254917e481eB44e9943F39138 (yours will likely be different). Remix provides us with an easy-to-use user interface to call the functions included in this contract. Below, we have a store function, which is a state-changing function (indicated by the color orange). State-changing functions are functions that make changes to the state of the blockchain therefore you need to pay a gas fee to call them. retrieve is a view function (indicated by the color blue). View functions are functions that return the value of the state variables in the blockchain and you don't need to pay a gas fee to call them.
deployed smart contracts
You can call the retrieve function on this contract by clicking on the blue retrieve button, it will return the value 0 because that's the default value of an integer if you didn't initialize it when you deploy the contract.
deployed smart contracts
To call the store function, you can specify the new value and then press the orange store button.
deployed smart contracts
Once the function call has completed, you'll see that you account balance has decreased again, that's because you have just called a state-changing function
deployed smart contracts
Now if you call the retrieve function again, it'll show the new value 99.
deployed smart contracts
Congratulations! You have now understood the basics of a Solidity smart contract and how to compile and deploy it using Remix.