Getting started with Smart Contracts
A smart contract is a computer program that automatically executes the terms of a contract when certain conditions are met.
This is lesson 1 of the Smart Contracts 101 free course by Rohas Nagpal.
A smart contract is a computer program that automatically executes the terms of a contract when certain conditions are met.
The code above illustrates a very basic HelloWorld smart contract.
1. What is a smart contract?
A smart contract is a computer program that automatically executes the terms of a contract when certain conditions are met.
These conditions are written into the code of the contract, and when they are met, the contract executes.
These terms can include anything that two or more parties would agree to in a traditional contract e.g. terms of payment, delivery date, quality of a product or service, etc.
Smart contracts are stored on a blockchain. This makes them transparent, secure & immutable.
If you are new to Blockchain technology, read this post: Blockchain Terms & Concepts.
Smart Contracts are primarily used on Ethereum and Ethereum Virtual Machine (EVM) compatible networks like Polygon (MATIC), Optimism (OP), Arbitrum (ARB), BNB Smart Chain (BNB), Avalanche (AVAX), and Gnosis Chain.
If you are new to Ethereum, read this post: Ethereum for Developers.
Example: If the terms of the contract state that payment will be made upon delivery of the product, the smart contract will automatically transfer the funds to the seller's account when the delivery is confirmed.
Smart contracts can interact with other systems & technologies through the use of external data oracles. They provide smart contracts with access to real-world information such as prices, weather conditions, etc.
Smart contracts v traditional contracts
Smart contracts differ from traditional contracts in 3 ways.
Smart contracts are digital contracts that are self-executing and operate based on predefined rules encoded in computer programs. On the other hand, traditional contracts are written in natural language and require legal interpretation and enforcement by a third party.
Smart contracts operate on blockchain technology, which provides a distributed and decentralized ledger that is transparent, immutable, and secure. In contrast, traditional contracts are typically stored in centralized systems that are susceptible to manipulation.
Smart contracts are designed to automate processes & eliminate intermediaries, resulting in faster & more efficient transactions. Traditional contracts, on the other hand, often require manual processing and involve multiple parties, which can result in delays, errors & disputes.
The biggest advantage of Smart Contracts
The biggest advantage of smart contracts is that they are self-executing, which means that there is no need for a third party to enforce the terms of the contract.
This makes smart contracts faster, cheaper & more efficient than traditional contracts.
2. Where are Smart Contracts used?
The most popular use of smart contracts is in finance, especially decentralized finance (DeFi).
If you are new to DeFi, read The Ultimate Guide to DeFi: Part 1.
Smart contracts are also being used for digital identity, legal agreements, and supply chain management sectors.
Example: A smart contract could be programmed to automatically release payment to a supplier once a shipment of goods has been received and verified by the buyer. This would eliminate the need for intermediaries such as banks, and reduce the risk of fraud or disputes.
Example: Smart contracts can be used in the insurance industry to automate claims processing & reduce administrative costs.
3. Lifecycle of a smart contract
The lifecycle of a smart contract can be broadly divided into 6 stages:
Stage 1: Writing the smart contract code
The first step is to write the code defining the contract's logic. This is typically done using a programming language like:
Solidity
Vyper
Rust
Cairo
Bitcoin Script
Ride
Haskell
C#
C++
Java
The main smart contract standards include:
ERC-20, which defines the rules for creating & deploying fungible tokens. ERC-20 tokens are identical and can be exchanged for one another. They are suitable for building digital currencies and tokenizing assets like equity, gold, etc.
ERC-721, which is a technical standard for non-fungible tokens (NFTs). These are unique and cannot be exchanged for other tokens on a one-to-one basis. They are suitable for representing assets such as art & collectibles. Each ERC-721 token has a unique identifier for tracking ownership & provenance of the underlying asset.
ERC-777, which defines a new way to interact with a token contract while remaining backward compatible with ERC-20. It enables efficient token handling, a simplified user experience, and reduces gas usage.
ERC-1155, which defines a standard interface for contracts that manage multiple token types e.g. a combination of fungible & non-fungible tokens.
ERC-4337, which removes the need for users to store complicated seed phrases. It allows for time-based social account recovery, enabling trusted friends or commercial services to assist with account recovery in a secure manner.
OpenZeplin provides a wizard for creating smart contracts using Solidity and Cairo.
Stage 2: Compiling the smart contract
Once the code for the smart contract has been written, it needs to be compiled into bytecode that can be executed by the Ethereum Virtual Machine (EVM). This is typically done using an Integrated Development Environment (IDE) like Remix.
The Remix Project is an extensive set of tools for developing smart contracts.
Stage 3: Testing the smart contract
Before the smart contract is deployed to the blockchain, it should be tested thoroughly to ensure that it works as intended. This typically involves writing unit tests and running them using a testing framework like Mocha.
Stage 4: Deploying the smart contract
Once the smart contract has been written, compiled & tested, it can be deployed to the blockchain (e.g. Ethereum). This involves sending a transaction to the blockchain network that contains the bytecode for the smart contract.
Stage 5: Interacting with the smart contract
Once the smart contract has been deployed to the blockchain, it can be interacted with using a variety of tools & technologies.
This includes web3.js, which is a JavaScript library for interacting with the Ethereum blockchain, or a user interface like the non-custodial wallet MetaMask.
MetaMask, a self-custodial wallet, makes it easy to access blockchain applications.
Stage 6: Updating the smart contract
Even though they are designed to be immutable, a developer can build a secure upgrade mechanism for smart contracts that can be governed by multi-sig wallets or a Decentralized Autonomous Organization (DAO).
What next?
In the next post, you will learn how to write, compile & deploy a Solidity smart contract on the Ethereum network.