Let’s Understand Smart Contracts in Blockchain

Understanding smart contracts

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 want to understand blockchain, you can start below.

How does Blockchain technology work in Web3? – WritersByte

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

Solidity smart contract code preview

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
Code language: Scala (scala)

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;
Code language: CSS (css)

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 }
Code language: PHP (php)

Sample Contract

Smart contract for a simple deposit wallet

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: MIT pragma 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; } }
Code language: JavaScript (javascript)

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.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

How does Blockchain technology work in Web3?
Understand blockchain technology

How does Blockchain technology work in Web3?

Understand Blockchain, Blockchain mining, Proof of Work, Proof of Stake and

Memory Management and Garbage Collection in Python
macro shot photo of a computer RAM

Memory Management and Garbage Collection in Python

Python is a high-level interpreted, dynamically typed language

You May Also Like