2 min to read.
Smart Contract
A program stored on a blockchain that runs automatically, exactly as written, when it is called.
What is a Smart Contract?
A smart contract is a computer program stored on a blockchain. It runs when a transaction calls it, and every node on the network executes the same code and reaches the same result. A smart contract can hold funds and move them according to its rules, with no intermediary needed to carry out the agreement.
How it works
A developer writes the contract, on Ethereum usually in a language called Solidity, and deploys it with a transaction. The contract receives its own address. Users and other contracts interact with it by sending transactions to that address, specifying which function to run. The network executes the code, updates the contract's stored data and records the outcome in a block. Each step of computation costs gas, paid by the caller.
Once deployed, a contract's code generally cannot be changed, unless it was deliberately built to be upgradeable.
The term was coined in the 1990s by the computer scientist Nick Szabo, who compared the idea to a vending machine: insert the right input and the machine delivers the product automatically. Ethereum, launched in 2015, was the first major blockchain designed around general-purpose smart contracts.
Example
A token is a smart contract that records balances and lets holders transfer them. A decentralised exchange is a set of contracts that hold pools of tokens and swap one for another by formula. A lending protocol releases a loan when collateral is deposited and sells the collateral if its value falls too far.
Why it matters
Smart contracts make blockchains programmable. They are the foundation of tokens, DeFi, NFTs, DAOs and decentralised applications in general.
Risks and misunderstandings
- Bugs are costly. Code that handles money in public is a target, flaws have led to very large thefts, and immutable code cannot simply be patched.
- A smart contract is not automatically a legal contract, and it is not "smart": it follows its instructions literally, including mistaken ones.
- Contracts cannot see the outside world. They depend on services called oracles for data such as prices, and those can fail or be manipulated.
- Upgradeable contracts place trust in whoever holds the upgrade keys.