Let’s Understand Smart Contracts in Blockchain

And maybe build one…

Moosa Ali
The Capital Platform
4 min readJun 25, 2022


A smart contract is essentially a set of legal rules. Just like a legal contract defines proceedings of a certain transaction or event in legal terms, a smart contract defines what rules transactions will follow on decentralized applications (Dapps). In a smart contract, these rules are present in the form of code, and this code is immediately executed once a smart contract is deployed on a blockchain.

If you don’t understand what a blockchain is, I highly recommend you start with the following article.

Structure of a Smart Contract in Solidity.

Solidity is one of the most common languages which is used in writing smart contracts. It is supported by the most common blockchains such as Ethereum and Solana. Solidity is a relatively new language that specializes in building smart contracts.

A smart contract must have the following contents:

  1. License Identifier
  2. Compiler Version
  3. Contract code

License Identifiers are placed on top of a Solidity smart contract in the form of a comment. These were made compulsory in solidity version 0.6.8. These licenses help specify whether your code is open-source or not and helps tools like Etherscan in the verification of the code. A License Identifier is written like the following:

//SPDX-License-Identifier: MIT

The above statement means that our code has an MIT license. A full list of supported licenses can be found here.

Compiler Version is specified right after a license identifier. Solidity has many versions to date and every smart contract requires you to specify which solidity version is to be used to compile that contract. It is specified using the following statement:

pragma solidity ^0.8.0;

The above statement means the code can be compiled using any version under the 0.8.x version. Choosing the correct version of the solidity compiler is very important, especially when importing contracts from other oracles.

Contract code is where your script is placed. This is where you write the actual code and set all the rules and regulations for your contract. If you are familiar with any Object Oriented Programming language, then you will find Contracts very similar to classes in OOP.

You have different contracts that define different sets of rules for your DApp and you can also import one contract into another (just like inheritance).

A contract looks like the following:

Contract myFirstContract {    // Your code here}

Sample Contract

Just like every other program, you can define functions in smart contracts. These functions are where the rules of the contract are defined. Let's look at the following sample contract. This contract is simply a deposit pool for your cryptocurrency.

//SPDX-License-Identifier: MITpragma solidity ^0.8.0;contract depositPool {   // Mapping to keep track of total amount for each user   mapping(address => uint256) public userToAmount;   function deposit() public payable {       //transfer balance to contract        updateBalanceRecord(msg.value, msg.sender);        }    function updateBalanceRecord(uint256 _amount, address _user) internal {        userToAmount[_user] += _amount;        }    function checkContractBalance() view public returns(uint256) {        //returns the total balance held by the contract        return address(this).balance;    }}

This contract allows you to call the deposit the function which will enable you to deposit some WEI into the contract itself. Furthermore, we have a mapping that helps track which user has deposited how much. We also have a function checkContractBalancethat is used to check the total balance of the smart contract.

This sample contract shows how we can configure functionalities in a smart contract. There are so much more functionalities that you can add to a smart contract e.g. you can control:

  • A minimum amount to deposit.
  • Who can call which functions

You can also add logical functionality in case you want to do some processing on the deposited amount.


Smart Contracts are used to define functionality for your decentralized applications. Within a smart contract, you define rules and functionalities which will steer your application.

This was just a simple introduction to smart contracts which is why I did not go into the details of the functionalities and any advanced concepts. If already understand the basics of blockchain, you can try running this code in the remix IDE.



Moosa Ali
The Capital Platform

Blogger | Data Scientist | Machine Learning Engineer. For more content, visit: www.writersbyte.com. Support me on: ko-fi.com/moosaali9906