Create your first Smart contract with Remix (Noob friendly)

paulinebanye

pauline-banye

Posted on March 19, 2022

Create your first Smart contract with Remix (Noob friendly)

As the buzz around Web 3 intensifies, the interest in Blockchain technology has skyrocketed. In the tech industry, it's becoming common for developers who have experience coding with Python or JavaScript to start learning Rust, Yul and Solidity - languages used for writing smart contracts and creating decentralized applications (dApps).

This article would focus on the basic steps for creating and compiling a very simple contract but before we get started, here’s a brief overview about smart contracts and dapps.

WHAT ARE SMART CONTRACTS AND DAPPS?

Smart contracts are simply automated computer codes that run on a decentralized blockchain network. They are digital contracts which are executed automatically, without a third party, when the terms of an agreement are fulfilled. For example, you could purchase an NFT virtually and immediately the payment is received, you are in possession of your newly minted NFT without having to go through an agent or third party.

DApps are applications but unlike regular applications, they run on a decentralized peer-to-peer network. You can think of a dApp as an app with a combination of a frontend interface users can interact with and a smart contract as the backend.

What is a decentralized network?

The concept of decentralized networks can be a bit confusing but the easiest way to explain is by comparing it to a centralized network. A centralized network is one which operates off a single server that handles all the data and processing needs. Majority of the apps we use currently are hosted from one central server. If that server goes down, that site or app is down until the server is back up.

However, a decentralized network is the exact opposite. Sites are hosted by multiple independent servers, also called nodes, each with the same exact copy of information. Thus, if there’s a problem with one server, the site would still function without interruptions.
dapps
Now that you have a better idea of what smart contracts are, let's start writing one!

For this tutorial, we’ll create a smart contract using Solidity in 4 steps; writing, compiling, deploying and verification. All these steps can be performed using various software but to make this as simple as possible, all we require are:

  • Remix: an extremely convenient medium for writing smart contracts with solidity. You could either use the browser extension or download the free desktop application.
  • Metamask chrome extension
  • Rinkeyby testnet

WRITING THE SMART CONTRACT WITH SOLIDITY

In this mini tutorial, we will create a simple file that sets a message input and returns that input when a view function is called.

So let's get started!

First, navigate to the remix website on your browser. Remix comes with a default workspace, which you can work with or create another.

You would notice that several files and folders have been created by default, including a contracts folder. Within the contracts folder, create a file with a .sol extension and input the following code.
Remix IDE
Okay, time to deconstruct this code!

The SPDX License identifier refers to the license for the file. It could be open source, business, private etc. Here we used the open source GPL 3.0 license.

Pragma solidity specifies the solidity version for the file. It is a very important requirement as the file would not compile without it.

HelloWorld is the name assigned to this contract and "string public message" defines a state variable for the contract.

State variables are variables which are stored in the smart contract permanently.

  • "string" specifies that the datatype for this variable is a string.
  • "public" is a term in solidity which defines the visibility of the contract. When public is assigned to a function or variable, it means that the function or variable can be called from within and outside the contract.
  • "message" is the name assigned to the variable. Note* the variable can be named anything. For example, foo, bar, house etc.

setMessage and viewMessage are functions. Functions are groups of reusable codes that can be used anywhere in the contract. Function setMessage assigns a value to the variable message while viewMessage simply returns the current message value.

COMPILING THE SMART CONTRACT

Now that we have our codes, we need to compile them into a format, which the Ethereum Virtual Machine (EVM) can read.

An EVM is an isolated virtual sandbox for running Ethereum smart contracts. Basically It's responsible for executing all contracts without access to the internet or other outside influences.

The EVM cannot read high level languages like solidity. For it to understand smart contracts, the codes need to be converted into a machine readable format.

In Blockchain programming, we use a tool called a compiler, which simply interpretes our source codes into opcodes, the instruction/operational language the EVM understands.

Luckily, Remix has a built-in compiler so we don't require another tool to convert our codes before deployment. All we need to do is select the file we intend to compile, specify the compiler version, the language we used for writing our codes (solidity in this case), any other optional parameters and click on the compile button.
compiled successfully
Once our smart contract has compiled successfully, a green tick indicator is visible, then we can move on to the next step - deployment!

💖 💪 🙅 🚩
paulinebanye
pauline-banye

Posted on March 19, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related