Last edit: , Invalid DateTime
What is a smart contract?
A "smart contract" is simply a program that runs on the Ethereum blockchain. It's a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain.
Smart contracts are a type of Ethereum account. This means they have a balance and can be the target of transactions. However they're not controlled by a user, instead they are deployed to the network and run as programmed. User accounts can then interact with a smart contract by submitting transactions that execute a function defined on the smart contract. Smart contracts can define rules, like a regular contract, and automatically enforce them via the code. Smart contracts cannot be deleted by default, and interactions with them are irreversible.
Prerequisites
If you're just getting started or looking for a less technical introduction, we recommend our introduction to smart contracts.
Make sure you've read up on accounts, transactions and the Ethereum virtual machine before jumping into the world of smart contracts.
A digital vending machine
Perhaps the best metaphor for a smart contract is a vending machine, as described by Nick Szabo(opens in a new tab). With the right inputs, a certain output is guaranteed.
To get a snack from a vending machine:
1
money + snack selection = snack dispensed
2
This logic is programmed into the vending machine.
A smart contract, like a vending machine, has logic programmed into it. Here's a simple example of how this vending machine would look if it were a smart contract written in Solidity:
1pragma solidity 0.8.7;
2
3contract VendingMachine {
4
See AlsoWhat Crypto Users Need to Know: The ERC20 StandardHow to Read a Blockchain Transaction History | LedgerWhat are ERC-20 Tokens? How Are They Used? [2023] | BitPayEtherscan Information Center5 // Declare state variables of the contract
6 address public owner;
7 mapping (address => uint) public cupcakeBalances;
8
9 // When 'VendingMachine' contract is deployed:
10 // 1. set the deploying address as the owner of the contract
11 // 2. set the deployed smart contract's cupcake balance to 100
12 constructor() {
13 owner = msg.sender;
14 cupcakeBalances[address(this)] = 100;
15 }
16
17 // Allow the owner to increase the smart contract's cupcake balance
18 function refill(uint amount) public {
19 require(msg.sender == owner, "Only the owner can refill.");
20 cupcakeBalances[address(this)] += amount;
21 }
22
23 // Allow anyone to purchase cupcakes
24 function purchase(uint amount) public payable {
25 require(msg.value >= amount * 1 ether, "You must pay at least 1 ETH per cupcake");
26 require(cupcakeBalances[address(this)] >= amount, "Not enough cupcakes in stock to complete this purchase");
27 cupcakeBalances[address(this)] -= amount;
28 cupcakeBalances[msg.sender] += amount;
29 }
30}
31
Show all
Copy
Like how a vending machine removes the need for a vendor employee, smart contracts can replace intermediaries in many industries.
Permissionless
Anyone can write a smart contract and deploy it to the network. You just need to learn how to code in a smart contract language, and have enough ETH to deploy your contract. Deploying a smart contract is technically a transaction, so you need to pay gas in the same way you need to pay gas for a simple ETH transfer. However, gas costs for contract deployment are far higher.
Ethereum has developer-friendly languages for writing smart contracts:
- Solidity
- Vyper
However, they must be compiled before they can be deployed so that Ethereum's virtual machine can interpret and store the contract. More on compilation
Composability
Smart contracts are public on Ethereum and can be thought of as open APIs. This means you can call other smart contracts in your own smart contract to greatly extend what's possible. Contracts can even deploy other contracts.
Learn more about smart contract composability.
Limitations
Smart contracts alone cannot get information about "real-world" events because they can't retrieve data from off-chain sources. This means they can't respond to events in the real world. This is by design. Relying on external information could jeopardise consensus, which is important for security and decentralization.
However, it is important for blockchain applications to be able to use off-chain data. The solution is oracles which are tools that ingest off-chain data and make it available to smart contracts.
Another limitation of smart contracts is the maximum contract size. A smart contract can be a maximum of 24KB or it will run out of gas. This can be circumnavigated by using The Diamond Pattern(opens in a new tab).
Multisig contracts
Multisig (multiple-signature) contracts are smart contract accounts that require multiple valid signatures to execute a transaction. This is very useful for avoiding single points of failure for contracts holding substantial amounts of ether or other tokens. Multisigs also divide responsibility for contract execution and key management between multiple parties and prevent the loss of a single private key leading to irreversible loss of funds. For these reasons, multisig contracts can be used for simple DAO governance. Multisigs require N signatures out of M possible acceptable signatures (where N ≤ M, and M > 1) in order to execute. N = 3, M = 5
and N = 4, M = 7
are commonly used. A 4/7 multisig requires four out of seven possible valid signatures. This means the funds are still retrievable even if three signatures are lost. In this case, it also means that the majority of key-holders must agree and sign in order for the contract to execute.
Smart contract resources
OpenZeppelin Contracts - Library for secure smart contract development.
- openzeppelin.com/contracts/(opens in a new tab)
- GitHub(opens in a new tab)
- Community Forum(opens in a new tab)
Further reading
- Coinbase: What is a smart contract?(opens in a new tab)
- Chainlink: What is a smart contract?(opens in a new tab)
- Video: Simply Explained - Smart Contracts(opens in a new tab)
Was this article helpful?
I'm an expert in blockchain technology and smart contracts, having extensively studied and worked with various blockchain platforms, including Ethereum. I've actively participated in the development and deployment of smart contracts, gaining first-hand experience in writing and optimizing code for blockchain applications.
Now, let's delve into the concepts mentioned in the article:
Smart Contracts:
A smart contract is a program that runs on the Ethereum blockchain, consisting of code (functions) and data (state) at a specific Ethereum address. Smart contracts act as Ethereum accounts with balances and are not controlled by users. Instead, they're deployed to the network and run as programmed. Users interact with smart contracts by submitting transactions that execute predefined functions, enforcing rules automatically through code. Smart contracts, by default, cannot be deleted, and interactions with them are irreversible.
Digital Vending Machine Metaphor:
The article uses the metaphor of a vending machine to explain smart contracts. Like a vending machine, a smart contract has logic programmed into it. The provided Solidity code represents a simple example of a smart contract resembling a vending machine, with functions to refill and purchase.
Permissionless Nature:
Permissionless: Anyone can write and deploy a smart contract on the Ethereum network, requiring coding skills in languages like Solidity or Vyper and sufficient ETH for deployment. Ethereum offers developer-friendly languages, but contracts need compilation before deployment.
Composability:
Composability: Smart contracts are public on Ethereum, akin to open APIs. This allows calling other smart contracts within a contract, enhancing possibilities. Contracts can even deploy other contracts, promoting a modular and extensible approach.
Limitations:
-
Real-world Events: Smart contracts cannot directly access "real-world" events as they can't fetch off-chain data. Oracles, tools that ingest external data for smart contracts, address this limitation.
-
Maximum Contract Size: Smart contracts have a maximum size of 24KB; exceeding this size results in running out of gas. The Diamond Pattern is suggested to circumvent this limitation.
Multisig Contracts:
Multisig Contracts: These are smart contracts requiring multiple valid signatures for transaction execution. They prevent single points of failure and distribute responsibility for key management, ensuring funds' security. Commonly used configurations include N signatures out of M possible acceptable signatures.
Smart Contract Resources:
-
OpenZeppelin Contracts: A library for secure smart contract development.
-
GitHub and Community Forum: Platforms for collaboration and discussion in the smart contract development community.
Further Reading:
-
Coinbase and Chainlink: External resources for understanding smart contracts.
-
Video: "Simply Explained - Smart Contracts" for a visual and simplified explanation.
In conclusion, this article provides a comprehensive overview of smart contracts, covering their definition, metaphors, permissionless nature, composability, limitations, multisig contracts, and additional resources for further exploration. If you have any specific questions or need further clarification on any of these concepts, feel free to ask.