Solidity for Beginners: Creating Smart Contracts
By: Pranay Bathini
Tags: Solidity, Smart Contracts, Metamask, Cryptocurrency, Blockchain

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.