Before diving into solidity, let's understand where solidity originated from and what solidity means, and why it should be used?

I recommend reading this article to have a theoretical understanding of blockchain and Ethereum before diving in. 

In this article, we are going to learn the following things.


Solidity is an object-oriented, high-level language(Human readable) for implementing smart contracts. 

But what are smart contracts?

Smart contracts are a piece of code uploaded to the blockchain. The code will contain public functions which can be called by anyone connected to the blockchain from their computer. 

The blockchain is distributed in nature. It means there will be more than one server(a network of computers) that will be running your code, verifying each other making sure no one cheats in the network. 

The distributed nature of blockchain makes it decentralized(No single point of control). The control is distributed among the participating nodes. This decentralized nature allows people to develop decentralized applications what are so-called DApps.

There are a lot of advantages and a change in developing tactics these DApps bring in. One of them is the immutability of smart contracts. Once deployed to Ethereum, they cannot be changed. This is to enforce the trust factor that the code is deployed the same way to everyone.

This also means the developer has to test the code for all corner cases before deploying to the blockchain. This changes the traditional way of shipping buggy code first and improving it later.

Solidity Features 

  • Statically typed - The type of variable declared is known at compile time. 
  • Inheritance - Contracts can inherit from other contracts
  • Function Modifiers, State Variable Accessors, Fallback Functions, Enums 
are among some which we will discuss in great detail with examples. 

Using Solidity: The easy way

The local installation of Solidity is a long process where we have to install node, npm, ganache-cli or testrpc for our private blockchain, solcjs for compiling our solidity code, and web3js for interaction. 

So, we are going to take the easy way of using remix IDE which is an online IDE and we will use two different kinds of test networks to deploy our code.

Installing Remix via docker

Don't worry, this is the only big step which we are going to do. 

If you don't have docker, install it from here. Trust me docker will save a lot of valuable time which we are going to lose in the installation process and configuration. 

Once you have installed docker and running it, execute the below command. 

docker pull remixproject/remix-ide:latest
 This command will pull the latest version of IDE into your system. Now let's run it. Execute the below command to run it.

docker run -p 8080:80 remixproject/remix-ide:latest
Now your IDE is running. Go to http://localhost:8080 and you can use you Remix instance.  You should be able to see a screen like below.

Your First Program in Solidity

Let's create a simple contract that lets you store your name on the blockchain and retrieve it. 

The contract will look like below. 

Let's understand the code line by line. 

The first line specifies which version of the compiler that code should be interpreted in. This is done with the help of the pragma directive.

Why we need to specify the version?

The only constant in software is change. So, if something new adds up in future versions, the existing code should run without breaking. This is to prevent future issues. This can be specified in two ways.

pragma solidity ^0.8.4;
This line tells the compiler to use the 0.8.4 version. The ^ symbol prevents the code from being compiled from a higher version.

pragma solidity >=0.7.0 <0.9.0

This tells only the compiler greater than or equal to 0.7.0 and less than 0.9.0 can compile the code. 

In the next line, the contract is defined by the contract keyword and it is similar to how the classes are defined.  A solidity contract is a collection of code(its functions) and data(state variables) and it resides at a specific address on the Ethereum blockchain.

Inside the contract, we define a state variable to store our name as a string.  All the variables are by default storage type which means they will be occupying space inside the contract when deployed on the blockchain. 

Note that Solidity is statically typed which means we need to define the type of variable while writing the contract and it will be resolved at compile time. 

In the next lines, we defined two functions, one for taking the input and storing the name and the other for retrieving the stored name from the blockchain. 

Note that both are defined as scope public which means anyone on the chain can call the functions. 

We will discuss all the language-specific constructs with detailed examples in the next set of articles.

For now,  let's compile the contract and deploy it. 

Compiling the contract

In the left vertical tab, click on the solidity compiler. You will see the below screen. 

Click on the compile NameStorage.sol. It should show the green tick mark as above. 
Note that the compiler version should be between 0.7.0 and 0.9.0. I have selected 0.8.1 here.

Once compiled, the compiler would generate a byte code that is machine-understandable like below 

Now, let's deploy it to the blockchain. 

Deploying the contract

Next, head to the deploy and run transactions tab. You should be able to see the below screen.

The menu can be overwhelming but let's focus on what we need to do. There will be a lot of test account with 100 (fake/test) Ether. 

Click on deploy and the contract will be deployed to the blockchain and a contract address will be returned as below. 

Our contract is deployed at address 0xa131AD247055FD2e2aA8b156A11bdEc81b9eAD95. 
Now, if you observe our balance will be 99.999 something. 

For deploying on the blockchain, some Ether has to be spent which is known as gas.  

Storing Our Name

In the left bottom, type your name in the textbox and click on store. This would store our name on the blockchain and it would cost us some gas as well as shown below.


Now click on the retrieve, to get the name stored on the contract. If you observe this properly, this operation does not cost us any gas. 

Only write operations cost us gas as the same should be replicated to thousands of servers. 

Remember, our functions are defined as public, so anyone on the blockchain can call them. 
You can select a different account from the accounts at the left top corner and call retrieve. 

It would give you output as 'pranay'. As we discussed, this operation would not cost us any gas. It would be 100 ether. 

Now, store another name from the same account and retrieve it like below. 

If you observe, our old name doesn't exist anymore. It was over-written by new value and it can be done by anyone. 

In the next articles, we will see how to restrict writing to contracts.

Connecting Metamask with Remix IDE

Install Metamask chrome extension from here. Once installed, create a password, store the passphrase in a safe location. Do not share it with anyone.

Once you get to the main screen, select Ropsten test network. You can also deploy to Ethereum mainnet but it will cost you actual money in ether. 

The ether in my account is test (fake) Ether. You can send test Ether to your account from metamask faucet

Deploying on Ropsten Test environment

Come back to Remix IDE and go to deploy and run the transactions tab and select Injected Web3 as Environment. 

Once you selected injected web3, remix IDE will try to connect with Metamask wallet like above. 
Click Next and Connect. 

You should be able to see your Metamask account in the deploy tab and also your test ether in your account. 

Now Click on deploy since it is already compiled. You will see a notification from Metamask like below for confirmation. 

Click confirm and the contract will be deployed to the test blockchain Ropsten network.

On the terminal in 'Deploy and Run Transactions' tab, you will see the transaction details like below. 

Once you click on the URL of Ropsten displayed, it will display more information about the transaction.

You can verify on Metamask too about the transaction and can see gas used on contract deployment. 

Now, you can go back to Remix IDE and store your name in the contract and retrieve it as we did earlier. 

In the next few blog posts, we will learn solidity language constructs by building a Non-fungible token. 

If you like the post, consider supporting us by sending some Ether to the below address. 


Keep Experimenting 🔎 

Keep Learning 🚀


Post a Comment

Previous Post Next Post