Smart contracts: a brief introduction

 


You should know something about how blockchain works before reading this article.

Contracts are agreements made between two or more parties who agree to adhere to their terms. It usually consists of a detailed document with several clauses and conditions signed by both parties. Law usually allows these contracts to be enforced and they are written in human language.

Throughout our lives, we sign contracts; for example, when we rent or lease a home, we sign a lease agreement. A contract of employment is signed when you accept a job offer. Return policies, which are contracts, are reviewed by employees when you go shopping and want to return a product. Also, financial agreements or insurance policies are contracts.

What are Smart contracts?

Smart contracts are rules-based computer programs that are able to make decisions based on conditions that they are programmed to meet. With smart contracts, you are able to verify and control an agreement automatically. Certain conditions must be met for digital assets to be transferred between parties. A traditional contract is enforced by law, while this code enforces the contract.

As a way to describe smart contracts, I like to think of them like vending machines. In essence, when executing smart contracts, you decide which functions to execute, or what items to obtain from the vending machine. Gas fees may be charged for these operations (which I will explain in a later section of this article), but they may also be free (depending on the type of smart contract it is).

Dapps

Decentralized applications (Dapps) and smart contracts are frequently confused, but this is incorrect. The blockchain is home to smart contracts, which are just pieces of code. A Dapp, however, connects the front end to the back end and employs smart contracts to operate.

Examples of Smart Contracts/Dapps

The voting for elections could be a smart contract example. It could then count the number of votes received by each party and decide who has won. Smart contracts would be immutable, so no one would be able to change them, ensuring their complete trustworthiness.

You may have heard of MakerDAO, which is a great example of a Dapp. Cryptocurrencies function as a means of lending and borrowing money. MakerDAO uses smart contracts and a frontend to communicate with the backend.

A Brief History

Nick Szabo, the cryptographer who first envisioned smart contracts on distributed ledgers, came up with the idea in 1994. He meant that smart contracts were sets of promises written in digital form, and that they included protocols which the parties were obliged to follow. In contrast, until Satoshi Nakamoto introduced Bitcoin in 2008, the term “smart contract” was seldom used.

A lot of today’s tasks are being accomplished using smart contracts, which were popularized by Ethereum later in 2015.

How Smart Contracts Work

Contracts with smart features consist of three components: the parties, the subject of the contract, and the terms and conditions. Whenever a smart contract is uploaded to the blockchain, its address is broadcast to the entire network. This code can then be executed by any node on the network.

The nodes have a history of all smart contracts, a history of all transactions, and the state of each smart contract at any given time.

A Dapp is an interface through which people can interact with a smart contract. Smart contracts are interacted with using the frontend and API. In order to interact with the blockchain, a Dapp must use smart contracts.

This article explains how smart contracts are best suited to the Ethereum platform from a conceptual standpoint.

Turing-Completeness

Ethereum uses the Solidity programming language, while Bitcoin uses Bitcoin Script. The biggest difference between Bitcoin Script and Ethereum’s Solidity is that Bitcoin Script is not Turing-Complete. Ethereum’s Solidity is. Turing-Complete languages allow loops and any logic to be programmable.
A loop is not supported in Bitcoin Script, for obvious reasons – an infinite loop or a loop that demands computational power would slow down the entire blockchain.

As a result, Bitcoin Script does not include loops by design.
Ethereum’s Solidity does support loops, but how does it deal with the problem of a slowing blockchain? This problem was solved in a brilliant way by Vitalik Buterin.

Gas Fee

A gas fee is a fee that is charged to the developer of a smart contract for executing any operation on the Ethereum network. Smart contracts are encouraged to be as efficient as possible in order to pay the lowest possible fee.ites very inefficient code, or, worst-case, an infinite loop in the smart contract, it may slow the network down temporarily, until the developer of the smart contract runs out Gas. By using this structure, loops can be used by Solidity while maintaining a smooth chain.

Gas prices are expressed in Chinese units called Wei. Smart contracts translate every operation into a number of Wei consumed by the developer. One Wei is 10^-18 of Ether, or one quintillionth of one Ether, the cryptocurrency used on the Ethereum blockchain.

Take a look at the spreadsheet below to see how much Wei is consumed by each operation.

Ethereum Virtual Machines

Every computer connected to the network will run a smart contract. Could a smart contract infect computers if it is maliciously written? Wouldn’t it be awesome if they could access your personal files or maybe even turn on your webcam? Where can we find solutions to this problem?

Ethereum Virtual Machines (EVMs) offer an effective solution to this issue. An EVM is installed on your computer when you join the Ethereum network. Everything that runs on the blockchain is completely isolated from the machine that it runs on inside the EVM. It is like a computer inside your computer, and nothing can leave the EVM and be transferred to your actual computer. An EVM is 100% unaware of anything occurring outside of it, which adds to its multiple layers of security, ensuring that everything that happens on it, stays on it.

Benefits/Advantages

There are several advantages to using smart contracts over traditional, physical contracts, as outlined below.
Removes Intermediaries: Direct and transparent relationships between all parties are made possible by smart contracts, which remove the need for intermediaries.  
Efficiency: Automating time-consuming and expensive tasks with smart contracts is made possible by software codes. Software codes also reduce the risk of human error.  
Security: The storage of documents on an immutable ledger in encrypted form makes them highly secure, and the possibility of misuse or tampering is very low. Since the data is arranged in chronological order, tracking the audit trail is extremely simple.  
Decentralization: All the nodes on the network store your data, so if one of them fails, the other node will be able to continue to store it.

Limits/Disadvantages

Despite the many benefits of smart contracts, there are still a few issues to work out.
Lack of International Recognization: Several countries do not recognize decentralized transactions without third parties as legal. Moreover, laws are still not mature enough to recognize decentralized transactions without third parties. This lack of regulation makes its full adoption difficult.

Immutable: An immutable smart contract is one that cannot be changed once it is put on the blockchain. When a smart contract contains a bug or the code needs to be changed in the future, there is less flexibility. Changing the code would require adding a new copy of the smart contract to the blockchain.

New Technology: Despite its relative youth, this technology is still relatively new, and its implications have yet to be fully assessed in several of its applications. As it becomes more mature, it must still gain broader trust.

Applications

In almost every aspect of our lives, smart contracts could be used – in finance and banking, in industries, in real estate, in government, and in the private sector.

Here are a few smart contract applications.

Banking/Insurance

In the banking industry, there are many projects underway that utilize blockchain technology to change the way things are done. These projects, which are mainly Dapps, such as Meld and MakerDAO, will inherently require smart contracts so that third parties will not need to intervene.

In the insurance industry, smart contracts can be used to efficiently check for errors, route payments, and issue payments. In order to do so, they use decentralized, immutable ledgers to keep track of financial transactions, and minimize the likelihood of fraud.

Healthcare

Payment systems for healthcare can be automated with smart contracts. By creating a single view on a blockchain, it can capture all healthcare transactions and bills and make them transparent between the patient, the health insurance company, and the provider. In order to safeguard data privacy and security, these records can be encoded. These records are only accessible by those who are authorized to access them.

Real Estate

Commissions to the real estate agents and legal fees to the lawyers are large parts of the cost of real estate transactions. The use of smart contracts can eliminate these commissions and fees for middlemen.

Smart contracts allow the creation of complex logic that ensures the agreement meets all conditions before transfer of ownership occurs. An immutable ledger will be used to keep track of these transactions so fraud can be minimised.

Management

With smart contracts, humans can make decisions on their behalf in organizations that are decentralized autonomous (DAOs) and built with complex conditional logic. Smart contracts, for instance, could be set up to order a new shipment of raw material from the supplier/manufacturer if raw material supplies fall below a certain threshold.

Smart Contracts: The Future

Our ability to address the challenges outlined above will determine whether smart contracts are adopted in the future. A smart contract template can be easily standardized from simpler and more straightforward conventional contracts. In the meantime, in order to address some of the above problems in more complicated contracts that require frequent changes due to their unique nature and changing circumstances, we may have to stick with conventional methods until we find a solution.

With the maturing of this technology, smart contracts will be used more and more in our day-to-day lives.

Sample Smart Contract

You can see a small mock cryptocurrency code snippet below, courtesy of Solidity Docs. You can see more examples on Solidity Docs.

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;

In line 1, we identify that the code is licensed under GPL Version 3.0, and in line 2, we are specifying the version of Solidity that we’re going to use. We do this because there might be breaking changes in newer Solidity compilers, so we want to keep it at this version.

contract Coin {
    address public minter;
    mapping (address => uint) public balances;

In line 4, we initialize our contract that we name Coin, and in line 5, we initialize an address called minter, but we don’t give it a value yet. Note that it also has the public tag, meaning that this variable is accessible by other contracts. In line 6, we now create a mapping called balances. A mapping is essentially like a key and value finder, so if you input an address into the balances variable, you will get a uint, or unsigned integer, which essentially means it’s not negative.

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

Here, in line 8, we are still inside the contract, and now we’re making an event. An event basically allows users to see what changes have been made to the contract. An event can be emitted, signaling that it has been executed.

    constructor() {
minter = msg.sender;
}

In line 10, we use a constructor. All of the code that’s nested inside a constructor gets executed when the contract is created. In line 11, we finally define our variable, minter, and set its value to msg.sender, which is essentially the address of the account that sent a “message.”

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

In line 13, a function, mint, is being called. This function essentially creates and sends, or mints, money to a specific address. The parameters of the function are the receiver as an address, and the amount being sent as an unsigned integer. The mingt function can only be called by the developer of the smart contract. In line 14, we are requiring msg.sender, the address of the account that initialized the smart contract, to be equal to the address of the minter. Then, by using our balances mapping, we add the amount we wanted to give, to the receiver’s account.

    error InsufficientBalance(uint requested, uint available);

In line 18, we are defining an error. Errors can give information on how and why an action failed. We are passing the parameters requested and available.

    function send(address receiver, uint amount) public {
if (amount > balances[msg.sender])
revert InsufficientBalance({
requested: amount,
available: balances[msg.sender]
});

In line 20, we are making a function called send that sends Coins to the address that called the function. In lines 21–24, we first check if the amount requested to send is greater than the balance of the person who called the function. If the condition turns out to be true, the revert keyword unconditionally stops all the code and gives an error, with the parameters of how much was requested and how much is available.

        balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Sent(msg.sender, receiver, amount);
}
}

Still inside the function, if the previous condition turns out to be false, then we proceed with the next lines. Here, we are subtracting the amount of Coins sent from the sender, and we are adding that same amount to the receiver. Then, we emit the event, Sent, which tells us that the transaction was successful.

*

Post a Comment (0)
Previous Post Next Post