How to write an Ethereum smart contract using Solidity

Sahil Sen
September 15, 2020

Overview

This article is intended for developers new to Ethereum development. In this article, we will talk about Solidity and smart contracts, What they are and what role they actually play in the ethereum development with the end goal of writing a smart contract using Solidity.

What is Ethereum?

Before getting started with smart contracts or Solidity let us first get an overview of what Ethereum is: Ethereum is a decentralized open-source blockchain with support for a Turing-complete programming language, Solidity. What we normally call computer programs are called smart contracts in Ethereum. Launched in 2015, Ethereum has been gaining significant popularity the last 5 years. It is a permissionless, public blockchain, which means anyone can use the blockchain, though for every write operation you need to pay ETH (Gas).

What is a Smart Contract?

The smart contract term was coined by Nick szabo in 1997. Smart contracts are nothing but programs that exist on the blockchain. These "smart contracts" can be used by making outside method calls or calls from other smart contracts. These smart contracts execute in the EVM (Ethereum virtual machine). Smart contracts can reduce malicious exceptions, fraudulent losses, and a need for trusted intermediator, when properly written and audited.

Ethereum supports Turing complete smart contracts, which means you can perform almost any type of operation you want. (Remember, you need to pay ETH for every write operation).

What is Solidity?

As we saw that smart contracts are nothing but programs and you need a programming language to write these programs. Ethereum core contributors invented a programming language called Solidity to write smart contracts (aka computer programs that run on the blockchain). Solidity is a high-level, object-oriented language inspired by JavaScript, C++, and Python - it has syntax very similar to JavaScript. There are other blockchains and Ethereum forks that support Solidity - such as Tron. Solidity is not the only language you can use to write smart contracts though. There are other languages that can be used to write smart contracts, like Vyper, the most popular and adopted smart contract language after solidity. 

Your first Smart contract

Now, let's write a simple smart contract. Our contract will allow us to store an unsigned integer and retrieve it. 

pragma solidity >=0.4.0 <0.7.0;
contract SimpleStorage {
    uint storedData;
    function set(uint x) public {
        storedData = x;
    }
    function get() public view returns (uint) {
        return storedData;
    }
}

The code snippet above is a smart contract written in Solidity language. Let’s take a moment to understand what the code we wrote in our smart contract is doing line by line.

Line 1: On the first line we are declaring which Solidity compiler we want to use. For instance, we are targeting any version between ≥ 0.4.0 and <0.7.0 .

Line 2: We are declaring our contract here and naming it as Simplestorage. It is normal practice to use the same filename as the contract name. For example - this contract will be saved in the file name SimpleStorage.sol (.sol is the file extension for solidity smart contracts).

Line 3: We are declaring a uint (Unsigned Integer) variable named storedData, this variable will be used to store data.

Line 4-6: Next, we will add a set function, using which we will change the value of our variable storeData. Here set function is accepting a parameter x whose value, we are storing into storeData. In addition, the function is marked as public which means that the function can be called by anyone.

Line 7-9
: We will add a get function to retrieve the value of storeData variable. This function is marked as view which tells Solidity compiler that this is a read-only function. 

Other than that the get function also has returns (uint), which means that the function will return a uint.

Deploying the Smart contract

After writing a smart contract it needs to be deployed on the ethereum network, we will deploy our smart contract using Remix. There are other ways to deploy smart contracts, but to make it beginner friendly, we will use Remix. Remix is an online web Ethereum IDE. It’s simple and supports many functionalities. So open Remix using this link. Remix has plugins, we need to activate two plugins for compiling and deploying our smart contract which you can see in the image below.

Here, you can see we have activated both plugins

Next, create a file on Remix with the name SimpleStorage.sol and copy/paste our smart contract code.

pragma solidity >=0.4.0 <0.7.0;
contract SimpleStorage {
    uint storedData;
    function set(uint x) public {
        storedData = x;
    }
    function get() public view returns (uint) {
        return storedData;
    }
}

Now, lets compile our smart contract using the Remix plugin.

Click on “Compile SimpleStorage.sol” to compile the contract

Finally, we can now deploy our SimpleStorage smart contract. We will deploy our smart contract on Rinkeby testnet. Blockchains have multiple public networks. One is the main public network, which we usually call mainnet. Others are for testing purposes, which we usually call testnets.

We'll deploy our smart contract using QuikNode to the rinkeby testnet, Signup or sign in to your QuikNode account, click on create node and create a new node on the Rinkeby network as shown in the image below.



Now, go back to remix and select the second plugin which we activated earlier 'DEPLOY & RUN TRANSACTIONS', aside the environment option select Web3 Provider.


A new popup like this will come up


Enter your QuikNode node's URL in the Web3 Provider Endpoint field


Now, select any account under the account section and click on deploy and the console underneath must give an output like this




Conclusion

So we have successfully created and deployed our smart contract written in Solidity to the blockchain, You can refer the Solidity Documentation for more.

Subscribe to our newsletter for more articles and guide on Ethereum. If you have any feedback, please feel free to reach out to us via Twitter and you can always chat with us if you have a question via our community server on Discord, thanks :)
Sahil Sen
Web developer who believes in power of communities and is passionate about community building. Developer evangelist @ QuikNode.

Related articles

All set for your QuikNode?