Structure & Building Blocks of Smart Contract

Harsimran Singh
4 min readFeb 9, 2022

--

By this time, almost all of us have heard about Smart Contracts powered by Blockchain technology in some capacity. If you are one of those inquisitive minds who like to know how things work you would be wondering what’s so unique or smart about these contracts. In this article, I would be going over Structure & building blocks of these smart contracts. I would using Ethereum blockchain and solidity language as this the most widely used tech stack for smart contracts. If you want to follow along you can open web ide Remix.

Before start writing our first smart contract, let’s go over to remix and understand what we need to do there. When you first open up the remix ,it will show you screen like below

License Identifier

In default workspace, go ahead and create file FirstContract.sol(extension of file is .sol as we are writing the code in solidity). Go to the file created, and write the first line `// SPDX-License-Identifier: GPL-3.0` . This line isn’t mandatory but recommended as it tells under which license this Smart Contract adheres to. Once this setup is done, let’s start writing our First Smart Contract

The first thing you need to do in any Smart Contract is to tell which version of solidity your contract will be using to write up the code , which will be done using pragma solidity >=0.7.0 <0.9.0; . In this line , we are tell our code can be compiled for any version ranging from 0.7.0 to 0.9.0.

Solidity Version

Contract Creation — Next Step will be declaring contract FirstContract{} . Technically speaking we are done writing our FirstSmart contract. You can go ahead compile & deploy this contract. As you can see there’s nothing so smart about Smart Contracts , they are just simple files similar to classes (reference to other programming langs). The important thing is what we do inside these contract and how we implement functionalities using other building blocks, which we go over shortly.

Contract Creation

State Variable — Once we have our contract, go ahead and declare string variable myName. State variables values are stored permanently in contract storage. Go ahead and compile the contract to check if everything’s working fine.

State Variable

Functions — Next step will be declaring functions, where we will be setting state variable and fetching it’s value as well. Functions are declared using function keyword, followed by the name , arguments , visibility modifiers and return type.

Functions

Events — Before setting up Events, go ahead and compile your contract to see if everything’s working fine. Events are used to log information on blockchain. Events are declared using event EventName(...params) and used emit EventName(..arguments) .

Events

Once we have this code ready, let’s go and deploy this contract. Once we have deployed our contract, hooray we have created and deployed our first Smart Contract though not on Mainnetyet. We can go and interact with our contract.

First Contract Deployed 👏

This is the first part of Structure & Building blocks of Smart Contract in which we covered contract creation, state variables, functions & events. In next part we will cover modifiers, structs ,enums & errors.

--

--