A brief introduction to Smart contracts

Learn what a Smart contract is and how they actually work

The Ethereum network introduced an application layer on top of the already established blockchain use case: digital currency. This application layer is powered by Smart Contracts, allowing decentralized applications to be created (dapps). In this article, we will be taking a look at what smart contracts are and look at a code snippet of a sample contract created with Solidity.

What is a Smart Contract?

Traditionally, a contract is a legal agreement between two or more parties. This agreement is enforced by law and there are consequences for breaching a contract. Agreements like this are based on trust (hoping that involved parties will keep to spelled-out conditions).

In the context of the blockchain, smart contracts define the terms of agreement and these are run as code on the blockchain. They are self-executing meaning that when certain pre-defined conditions are met, the code automatically runs and does not need human intervention. Since they are built on the blockchain, they are decentralized, secure, and accessible and also eliminate the need for "trusted intermediaries" e.g. banks before carrying out complex transactions.

How does a Smart Contract work?

The best way to understand how it works is by illustrating an example.

Let us imagine that Alice owns a land and will be selling it to Bob. In order to facilitate an agreement between the two parties, we build a Smart Contract that contains the terms of the agreement:

When Bob pays 10 Ether, Transfer the Certificate of Land Ownership to Him

Since this agreement is on the blockchain, it is processed by the blockchain and validated as true by all participators in the network hence eliminating the need for "trusted intermediaries" (in this case, a bank, a lawyer, and a broker)

A real-world analogous

Think of a Smart Contract as a vending machine because specific inputs will always have the same output.

  • You decide to buy a can of soda from a vending machine
  • The machine states that it costs $0.5 to purchase a can
  • You insert $0.5 into the machine
  • The machine verifies that you indeed inserted $0.5
  • The machine dispenses your can of soda

In the above scenario, the vending machine will only release the can of soda if the amount inserted matches the price. If all specified parameters are not met, no can of soda will be dispensed.

Vending Machine Solidity Code

In this section, we will be translating the above analogy into Solidity code to have an insight into how Smart Contracts are written.

Solidity is one of the languages used for Smart Contract development. It is an Object-Oriented, high-level language that runs on the Ethereum Virtual Machine (EVM).

// SPDX-License-Identifier: MIT

pragma solidity 0.8.9;

contract VendingMachineContract {

    // This declares all state contract variables
    address public owner;
    mapping (address => uint256) public sodaBalances;

   // When done deploying, set the deployer as contract owner
   // set the max supply of soda to 20
    constructor() {
        owner = msg.sender;
        sodaBalances[address(this)] = 20;
    }

    // Allow the owner to increase the smart contract's soda balance if balance equals 1
    function respawn(uint32 amount) public {
        require(msg.sender == owner, "Only the owner can increase balance.");
        require(sodaBalances[address(this)] == 1, "Soda count not low enough!");
        sodaBalances[address(this)] += amount;
    }

    // Grant permission to anybody who calls the contract to buy soda
    function buySoda(uint256 amount) public payable {
        require(msg.value >= amount * 1 ether, "You must pay at least 1 ETH per can of soda");
        require(sodaBalances[address(this)] >= amount, "Not enough sodas in stock to complete this purchase");
        sodaBalances[address(this)] -= amount;
        sodaBalances[msg.sender] += amount;
    }
}

Conclusion

Smart contracts will continue to help minimize the need for "trusted intermediaries" while leveraging on the decentralized nature of the blockchain. So far, we learned what Smart Contracts are and how they work. We also got to know how Solidity code is written and how agreements look in code.

Thank you for reading this far ๐Ÿ˜Ž

WAGMI ๐Ÿš€

Further Resources