Your resource for web content, online publishing
and the distribution of digital products.
S M T W T F S
 
 
 
 
 
1
 
2
 
3
 
4
 
5
 
6
 
7
 
8
 
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 

Could XRP Ever Reach $1,000? 5 Key Developments That Could Catalyze a Meteoric Rise

DATE POSTED:November 11, 2024
A detailed guide on the building block of smart contractAN IMAGE CONTAINING THE POST TITLE AND ILLUSTRATION OF A SMART CONTRACTphoto by author

Hi, have you been trying to understand smart contracts better? You might have considered smart contracts a very complex concept to understand. Smart contracts are not as complex as they look (although with the correct explanation). In a much simpler term, smart contracts are self-executing programs that help facilitate the verification, control, and/or execution of a transaction.

In this post, I will make the concept of smart contracts easy to understand. This post will provide everything you need to know about smart contracts, from a proper explanation of how they work and how you can write them.

What are smart contracts?

Smart contracts are self-executing computer programs that run when certain criteria or rules of an agreement are met. Smart contract serve as fundermental building blocks for creating decentralized application and web3 technologies.

Think of this scenario, you are having a business transaction with someone, and both of you agree on terms. For that transaction to go on smoothly, both you and the person need to complete your parts of the agreement.

This same thing is applicable in a smart contract. Just as contracts and agreements regulate our personal and professional lives, that is how they influence the blockchain industry. But in this case, we use smart contracts to ensure that the required conditions are fulfilled before blockchain transactions are completed.

Smart contracts play undeniably crucial roles in the blockchain industry. They ensure that blockchain transactions are secure, safe, and functional. Moreover, smart contracts ensure that third-party entities are not included in completing a blockchain transaction.

Smart contracts run blockchain transactions automatically allowing all participants in the transaction to have a clear view of the outcome without delay. This makes blockchain transactions traceable and irreversible.

With the aid of smart contracts, two anonymous parties can perform trusted transactions without involving a central authority. This concept contributes to the blockchain system being decentralized and not controlled by a central entity, like banks.

The idea of a smart contract was first introduced in 1994 by Nick Szabo, who described it as “a computerized transaction protocol, that executes the terms of a contract”.

However, the implementation of smart contracts faced a major setback because blockchain technology was only introduced in 2009 when Bitcoin was launched. Then in 2015, the Ethereum protocol was launched and this brought the whole idea of smart contracts to reality.

There are so many reasons for the introduction of smart contracts. However, the major reason was to make blockchain transactions as decentralized as possible. Eliminating the need for third-party interference.

smart contracts are self executing programs that automate the execution of blockchain transactions when certain criteria are met.smart contracts are written with codes and they do not contain legal documentations.smart contracts was introduced to eliminate the need for external parties in running transactions.An image of a man and a woman writing a contract and a text describing smart contractHistory of smart contracts

In 1994, an American computer scientist known as Nick Szabo proposed the smart contract idea.

In his book “Smart Contracts: Building Blocks for Digital Free Marketshe described smart contracts as “computerized transaction protocols that execute the terms of an agreement”.

His idea was that smart contracts would be able to record contracts and agreements in the form of computer codes. This will allow the activation and execution of actual transactions automatically without the help of third-party interference.

Nick's work led to the proposal of the first virtual currency the “Bit Gold”. However, the idea of smart contracts was not fully implemented until 2009 when the first blockchain network (Bitcoin) was introduced.

Although the launch of Bitcoin has been linked to Nick on several occasions, which he denied, it is notable that Bitcoin played an important role in the implementation of smart contracts in real-life applications.

The Bitcoin network uses smart contracts to establish a set of conditions that need to be satisfied before users can transfer tokens between themselves. These conditions include requiring a private key that matches the sender’s address.

As blockchain technology continued to evolve and new programmatic conditions were added, smart contracts took a major step further. In 2013, the Ethereum whitepaper was published introducing the Ethereum network which changed the whole idea of smart contracts to what we have today.

On the launch of Ethereum in 2015, it introduced something called a programmable smart contract. So instead of having a single smart contract application, the Ethereum smart contract proposed a way that many independent smart contracts can run at the same time effectively on a single blockchain.

This made smart contracts more practical and adaptable. So developers can simply write their smart contracts and deploy them on the Ethereum blockchain.

How do smart contracts work?

Smart contracts are computer programs that operate on a virtual machine or a distributed ledger (Blockchain). Smart contracts work by following a set of if/when….then statements. The contracts are written as codes and deployed on a blockchain to execute certain actions when pre-agreed conditions are satisfied.

For a smart contract to work, there are a lot of stipulations and terms to be agreed upon. To establish these terms participants have to follow these steps;

step 1 Agreement: The parties involved in the transaction must first of all agree on the terms and conditions. They must determine how the smart contract will operate including things like required criteria. Examples of these conditions are payment authentication and authorization, to check if the payment is from the right source and if there is enough balance to make such a transaction.

step 2 Contract Creation: With agreement reached by participants of the blockchain transaction, a developer can now start writing the smart contract. The developer needs to be fully informed of the desired response to certain events, like what happens if there is an insufficient balance.

Then the developer uses a programming language (Solidity) to create and test the logic. It is important to test the security of the smart contract before deployment. So the smart contract code needs to be tested by a security team.

step 3 Deployment: After creation, the smart contract is then deployed to a blockchain network. This will make it active on the network and transactions can now be executed from here.

step 4 Execution: The smart contract is designed to listen to events in the form of if/when….then statements. These events come from a cryptographically secure source known as a “Decentralized Oracle Network (DON)”. When these events are triggered, the smart contract is activated and transactions like funds transfer are executed.

step 5 Recording: The result of every smart contract transaction is recorded in the blockchain network. The blockchain network verifies the transaction and logs the concluded data. This data is available to everyone with access to the transaction and it is not reversible.

The actions that can be carried out with a smart contract include the transfer of funds, registration and voting for a candidate, and issuing of tickets among other things.

After the completion of the transaction, the blockchain is updated.

How does the Ethereum smart contract work?

Smart contract execution on the Ethereum blockchain network is not free. You are expected to spend a few ethers (Ethereum native tokens) to deploy and run smart contracts on the Ethereum blockchain network.

The Ethereum blockchain network has two distinct layers; the mainnet which is used for real transactions and requires real ethers and the testnet which is used for testing purposes and uses test ethers for transactions.

Smart contracts built on the Ethereum blockchain network are executed on an Ethereum Virtual Machine (EVM) which is a runtime engine for the Ethereum blockchain.

The language used by developers to write smart contracts on the Ethereum blockchain is called Solidity. After writing the smart contracts the developer needs to compile the smart contracts before they are deployed. This is because the EVM only understands byte codes and smart contracts are written in high-level language.

Smart contracts on the Ethereum blockchain work the same way any smart contract will work. They automatically execute transactions when certain conditions are satisfied and they can be used for different purposes as well.

Why was smart contract proposed?

Smart contracts were proposed to eliminate the need for third-party interference in the execution of transactions. This was the primary idea. But aside from this, smart contracts were also introduced for other reasons as well.

Smart contracts were introduced to make the process of transaction execution faster and reduce cost. This is possible as it makes transaction execution as automatic as possible when the required conditions are satisfied.

Smart contract was also introduced to make transactions transparent. This is evident as smart contract transactions are made open and accessible to all participants with access. Also, smart contract transactions cannot be modified after they are complete.

Smart contract was also introduced to prevent fraud. When a transaction is completed it is stored permanently in the blockchain network and the blockchain is updated. This makes the transaction irreversible.

Smart contracts also ensure the security of transaction execution. This is possible as the decision of transaction execution is decentralized and cannot be uttered by parties or individuals.

Smart contracts were proposed to eliminate the need for third-party interference in the execution of transactions. This was the primary idea.But aside from this smart contracts were also introduced for other reasons as well. like security of transaction, fraud prevention and transaction transparency.What are the major parts of smart contracts?

The parts of a smart contract may vary depending on the blockchain network and how the smart contract was programmed. Imagine a legal agreement. Although all agreements have the same goal, which is to ensure the completion of a transaction. Different agreements have different structures.

The same thing applies to smart contracts. But in general, all smart contracts must have the following parts;

State Variables: A state is the current status of the smart contract and it changes as participants interact with the smart contract. State variables are containers that hold information about the smart contract’s state. A state variable can hold data such as items in a supply chain, users' account balances, and other details.

Functions: Functions are operations that can be performed by a smart contract. Functions are what participants rely on to update the state of the smart contract. Smart contract functions are triggered by events.

Events: Smart contract events are messages that a smart contract sends out to trigger functions or log into the blockchain to provide a transparent record of transactions. Events in a smart contract are triggered when participants initiate a transaction.

Modifiers or rules: These are conditions that must be satisfied in the smart contract before a transaction is completed. Rules and modifiers control who can perform a transaction, what type of transaction can be performed, and so on.

These are the four major parts of every smart contract. A developer will define each of these parts with code while writing the smart contract.

The parts of a smart contract may vary depending on the blockchain network and how the smart contract was programmed.But every smart contract must have a state variable, functions, events and conditions.Some smart contracts might have sections that contain basic information like contract names.What are smart contracts used for?

Smart contracts can be applied in different areas of real-life transactions. Below are some examples of smart contract applications in real life:

Supply chain: This is one of the simplest use cases of smart contracts. In the supply chain, smart contracts are used to ensure that transactions such as purchase and delivery of goods are completed quickly.

For example, a supplier can create a smart contract for the supply of raw materials to manufacturers. This smart contract might include conditions like executing a transaction when a manufacturer places an order for raw material and disbursing of funds when the manufacturer receives their goods.

This will encourage transparency between the manufacturer and supplier.

Voting and Election: Another perfect application of smart contracts is during an election. Smart contracts minimize the likelihood of vote manipulation. Centralized voting systems face a good number of challenges when it comes to counting and tracking votes.

Crowdfunding or Fundraiser: With smart contracts (like the Ethereum-based smart contracts) you can create and issue your digital tokens. You can leverage this to get people to contribute towards your ideas by purchasing your tokens and holding till a specified goal is achieved.

Real Estate: In real estate management, the purchase of properties can be very slow. This is because ownership transfer involves a lot of processes with too many back and forth. But with smart contracts, agreements can be written that after the buyer pays, the title of the property will be handed over to them reducing delay.

Health care: Smart contracts ensure data accuracy in clinical matters. For example, before patient's data are recorded they must meet certain requirements. This will ensure the integrity of the patient's data.

Legal entities: A smart contract ensures that legal binding between two parties is not broken. It ensures the complete commitment by all parties in a legal agreement.

Music and entertainment: In the entertainment industry smart contracts are useful tools to ensure that royalties are properly distributed to the right creators.

What are the benefits of smart contracts?

Here are the benefits of smart contracts:

Error prevention: Traditional ways of creating contracts are mostly prone to errors and human mistakes. There might be loopholes like the omission of agreement terms and so on. This can lead to issues like misinterpretation. These errors can be avoided with smart contracts.

Decentralization: Smart contracts are executed without the help of third parties. There is no central source of execution. This ensures that the execution of the transaction is very secure.

Zero trust mechanism: Smart contracts do not need to trust participants in a transaction for it to be completed. A transaction can be completed only if the required conditions are satisfied. This ensures that all participant in the transaction completes their parts.

Backup: Whenever you make a transaction. The smart contract records the transaction’s data on the blockchain network permanently. This encourages future usage and references.

What are the top 6 smart contract development tools today?

Smart contract development requires bringing tools together that will let you write your code. These are the top tools for writing smart contracts in 2024.

Integrated Development Environment(IDE): An Integrated Development Environment (IDE) is a tool that allows us to write and compile codes. The post will make use of Remix IDE, a web-based IDE. You can access the Remix IDE by visiting this link. You do not have to install it on your machine, you can easily use it right from the browser.

The interface of the Remix IDE is similar to that of Visual Studio code. It has a main panel where you write and edit codes. It also has a terminal that allows you to run script commands and a side panel that highlights all the files in your smart contract program.

Crypto wallet: To use the Ethereum blockchain to create smart contracts you have to spend some gas fees. This is where you need a crypto wallet. The most popular crypto wallet for testing Ethereum smart contracts is the meta mask wallet. This post will introduce you to the Metamask wallet and how you can use it.

Package Manager: A package manager is a tool that enables developers like us to add and keep track of dependencies in our development projects. One of the most popular package managers is the Node Package Manager (NPM) and that is what we use in building smart contracts with Solidity.

Frameworks: Frameworks provide developers with an environment to solve problems quickly. This is very important to accelerate our work and make it faster. The best smart contract writing frameworks in 2024 are Hardhat and Truffles.

Smart Contract Stimulator: Smart contract stimulators are important tools that allow you to test your smart contract codes in real time. There are good numbers of smart contract stimulators like Goerli and Ganache.

What are the steps involved in creating a smart contract?

When writing smart contracts the first thing you need to do is to set up your development environment.

In this section, I am going to give a step-by-step guide on how to set up your development environment. We will start with setting up metamask

How do I install Metamask?:

Installation of meta mask extension in your web browser involves 3 simple steps;

step 1: Visit the metamask download page here. This page will automatically detect your browser and update the install button with the link to a specific extension for your browser.

Click on the link and you will be directed to the extension store where you can download the extension.

Step 2: Add the extension to your browser. At this point you should be on a screen with a button that says “Add to Chrome” or whatever browser you are using.

Click on the button, this will pop up a page where you will be asked to add the extension or cancel the entire process.

Click on the button that says “Add Extension”. This will download the Metamask extension

Step 3: Now you have to create a new wallet or log in an already existing wallet.

If you create a new wallet, read the terms of use and tick the “I agree” box if you are comfortable with the terms of use. Then you can go ahead and click on the “Create a new wallet” button.

If you want Metamask to gather basic information about how you use their platform and personalize it for you over time. You can agree with that as well.

Now you will be asked to create a strong password. After that, you will be given a 12-word recovery phrase.

You need to keep these phrases secure because it is what anyone including you can use to access your Metamask wallet in the future.

But if you want to log in to an already existing wallet, you will have to provide the 12-word recovery phrase for that wallet.

How do I write a smart contract?

To write a smart contract you need an Integrated Development Environment (IDE) that can compile and deploy your smart contracts. This section will show you step-by-step how to write and deploy a smart contract with Remix IDE.

Step 1 Visit the Remix IDE website here

step 2 Connect your Remix IDE with your GitHub account by Clicking on the “sign to GitHub” button.

Step 3: Click the “Login with GitHub” button and follow the instructions under the “GitHub setup” section.

Step 4 You have to load a repository from your GitHub account. Go under the Remotes section click the “Load from Github” button, select a repository, and follow the other instructions that follow.

By default, you will be on the master branch. But if you want to create other branches, go under the BRANCHES section and create a new branch. You toggle between the two branches by turning off the branch that you do not need.

step 5 Create a file in your workspace by Clicking on the “File Explorer” icon at the top and then clicking on the “Create a new file” icon. Give the file a name of your choice but add an .sol extension. For instance democontract.sol

Now you can open your smart contract file in the main panel to start editing. The first thing you need to do is to specify the exact compiler version required to compile the smart contract with the pragma solidity directive.

// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

The code above will ensure that the smart contract is compiled with the Solidity compiler version 0.8.26. This is because Solidity syntax and functionality may differ between versions, and not specifying the Solidity compiler version might lead to compatibility issues.

P.S.: You might get an error in the version declaration. This is because Remix IDE defines a specific version of the Solidity compiler in the pragma directive. To solve this error you have to go under the Solidity compiler and select the version 0.8.26

Step 6 You have to create a send event that will be called when a user tries to send out tokens from the account

event send(address from, address to, uint amount);

Step 7 Declare the contract using the contract keyword followed by the name of the contract and a pair of braces that will wrap the contract functions and state variables.

contract MintCoin{

}

So the first state variable you will declare inside the contract braces is the minter’s address variable, and the second variable will be the balance


address public minter;
mapping (address => uint) public balance;

From the code above you declared a public variable named minter that will store the Ethereum address of the minter. You also defined a mapping named balance. This mapping will be used to store and track the token balances of different addresses.

Now we have to add a constructor to our smart contract. This constructor will be responsible for minting new coins.

constructor() {
minter = msg.sender;
}

After that, we have to create a function to allow user’s mint new coins.

function mint(address receiver, uint amount) public {
require(msg.sender == minter);
require(amount < 1000);
balance[receiver] += amount;
}

From the code above you created a function called mint and inside this function, you added the necessary conditions before users can mint tokens.

The first condition is that the user is eligible to mint the token, and then the user can only mint tokens less than 1000. If these conditions are met, then you update the user balance with the minted amount.

Next, we have to create a function that will allow users to send tokens to other users.

function sendCoin(address receiver, uint amount) public {
require(amount <= balance[msg.sender], "Insufficient Balance");
balance[msg.sender] -= amount;
balance[receiver] += amount;
emit send(msg.sender, receiver, amount);
}

Note that you already have an send event that will trigger the sendCoin function. So you have to give this function a different name from the event.

In this function, we defined the required conditions, which is the balance of the user must not be less than the amount they want to send. And then the amount to be sent will be deducted from the sender’s account and added to the receiver’s account. Then the send event will be emitted to trigger the sendCoin function

Now the next function you have to create is the balances. This function will allow you to get the balance of each user after each transaction.

function balances(address _account) external view returns (uint) {
return balance[_account];
}

So this is a complete smart contract code that will allow users to mint and send coins. And also get their account balances. Below are the complete codes put together:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;
contract MintCoin{
address public minter;
mapping (address => uint) public balance;

event send(address from, address to, uint amount);

constructor() {
minter = msg.sender;
}

function mint(address receiver, uint amount) public {
require(msg.sender == minter);
require(amount < 1000);
balance[receiver] += amount;
}

function sendCoin(address receiver, uint amount) public {
require(amount <= balance[msg.sender], "Insufficient Balance");
balance[msg.sender] -= amount;
balance[receiver] += amount;
emit send(msg.sender, receiver, amount);
}

function balances(address _account) external view returns (uint) {
return balance[_account];
}
}

After writing the smart contract codes you have to compile them. So go under the Solidity Compiler tab, and click on the blue button that says “compile yourfilename.sol”.

This will compile your smart contract code and if there are errors you will see them, otherwise you will see a green tick on the ‘Solidity compiler’ icon.

So now we can go ahead and deploy and test your smart contract.

To achieve this, you need to go to the Deploy and Run transaction tab and click on the Deploy button

Make sure you are on the Remix VM environment. After deploying your smart contract you can access it under the Deployed contracts. There you will find all the functions in your smart contracts.

To test the smart contract, you can start by minting tokens. Click on the minter button. This will give you the address of the minter.

Then you have to copy that address and paste it into the mint function alongside the amount you want to mint. But make sure it is below 1000 tokens because remember it is part of the conditions of minting. Click on the mint button and you will be able the mint the tokens.

You can confirm this by checking the amount in the address. Copy the address again, and pass it to the balance function. Click on the balance button, you will see the amount you minted.

You can also send tokens to other users, by scrolling up to the provided Ethereum addresses and copying a new address. Then paste this new address into the sendCoin function, along side the amount you want to send.

Now check the balance of the new address, and you will see that the receiver has received the specified token. You can also check the balance of the sender’s address and you will notice that the sender's token has been reduced.

With this, you were able to create a basic smart contract, to mint tokens and send tokens. This smart contract has 4 major parts which are functions (the mint, sendCoin, and balances functions), state variables(the minter and balance variables), an event (the send event), and various conditions

Now you can push your smart contract code to Github, by clicking on the “Git” icon and creating a commit under the “source control” dropdown.

After creating your first commit, you can now push your changes to Github, by going under the commands dropdown and clicking on push button

So if you check your GitHub repository you will see your smart contract codes.

Conclusion

Smart contracts are essential tools in decentralized applications and blockchain development. They ensure the integrity of transaction execution and most importantly they make sure that transaction execution does not require third-party interference. In this post, I explained what smart contracts are, and I gave a brief history of how the smart contract idea came about. I explained the 4 major parts of a smart contract and also gave you the benefits and use cases of a smart contract. I wrapped this post up with an example guide of how to write smart contracts with Remix IDE and how to install and set up the Metamask extension. I am open to answering any of your questions and collaborating with you on your projects. Thanks for reading

What are smart contracts? was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.