Smart Contracts simplified

Alvin Mutebi
3 min readMar 4, 2023

--

By now you at some point heard of the Ethereum blockchain, and if yes, you encountered at some point a term known as Smart contracts. Today we shall focus on an in-depth understanding of smart contracts and let's dive into it.

What are smart contracts?

Smart contracts are simply self-executing programs which are deployed on the Ethereum network and can only be executed when a certain set condition is met. These contracts aren't governed by anyone and are confirmed and verified by miners. They are coded in a language known as Solidity.

Benefits of Smart Contracts

Speed, efficiency and accuracy

Once a condition is met, the contract is executed immediately. Because smart contracts are digital and automated, there’s no paperwork to process and no time spent reconciling errors that often result from manually filling in documents.

Trust and Transaparcey

Because there’s no third party involved, and because encrypted records of transactions are shared across participants, there’s no need to question whether information has been altered for personal benefit.

Security

Blockchain transaction records are encrypted, which makes them very hard to hack. Moreover, because each record is connected to the previous records on a distributed ledger, hackers would have to alter the entire chain to change a single record.

Cost Saving

Smart contracts remove the need for intermediaries to handle transactions and, by extension, their associated time delays and fees.

Types of Smart Contracts

Token Contracts

Token contracts aid in the creation of new crypto tokens on the other Ethereum network. These define the rules or nature of how a token should be created, transferred and destroyed for example ERC-20 contacts tokens are mostly used for tokens, ERC-721 contracts for None-Fungible tokens (NFT)s and ERC-1155 contracts for both fungible and non-fungible tokens.

Decentralized Finance Contracts (DeFi)

These contacts are used to create decentralized financial applications on the Ethereum network. These contacts enable the users to trade, borrow, lend, add liquidity to some tokens, and earn interest without middlemen, for example, Uniswap a decentralized exchange, Aave a decentralized lending platform, and Compound a decentralized borrowing platform.

Governance Contracts

These are contacts that are used in decentralized autonomous organizations (DAO)s on the Ethereum network. These define the rules of how decision-making will be in the organization such as voting, and how proposals are submitted for example, MakerDAO is a decentralized organization which controls DAI and Aragon is a platform for creating DAOs.

Identity Contracts

These contracts aid in the creation of digital identities on the Ethereum network and allow users to prove their identity without the need for a central authority. for example, Serto which is used to create and manage decentralized identifiers (DIDs) and Civic.

Here is a simple code snippet of a smart contract for a vending machine and how the entire logic would flow.

Note: Solidity Code.

pragma solidity 0.8.7;

contract VendingMachine {

// Declare state variables of the contract
address public owner;
mapping (address => uint) public cupcakeBalances;

// When 'VendingMachine' contract is deployed:
// 1. set the deploying address as the owner of the contract
// 2. set the deployed smart contract's cupcake balance to 100
constructor() {
owner = msg.sender;
cupcakeBalances[address(this)] = 100;
}

// Allow the owner to increase the smart contract's cupcake balance
function refill(uint amount) public {
require(msg.sender == owner, "Only the owner can refill.");
cupcakeBalances[address(this)] += amount;
}

// Allow anyone to purchase cupcakes
function purchase(uint amount) public payable {
require(msg.value >= amount * 1 ether, "You must pay at least 1 ETH per cupcake");
require(cupcakeBalances[address(this)] >= amount, "Not enough cupcakes in stock to complete this purchase");
cupcakeBalances[address(this)] -= amount;
cupcakeBalances[msg.sender] += amount;
}
}

In conclusion, smart contracts are an exciting application of blockchain technology that has the potential to revolutionize many industries. They provide speed, efficiency, accuracy, security, transparency, and cost savings.

References

Blochain For Everyone:https://medium.com/@alvinmutebi/blockchain-for-everyone-2a9d88ba97bb

Common terms on the Ethereum blockchain:https://medium.com/@shammahbenjamin/common-terms-on-the-ethereum-blockchain-9c3d40afa28d

Introduction to smart contracts: https://ethereum.org/en/developers/docs/smart-contracts/

--

--

Alvin Mutebi

Software Engineer, Python, JavaScript, Typescript, Solidity, Certified Amazon Cloud Practitioner.