Foundations · Blockchain
Smart Contracts
A smart contract is a program stored on a blockchain that runs exactly as written when a transaction calls it. Its code and balances are public, and bugs in it can be exploited.
Why it matters
Smart contracts are what turned blockchains from payment networks into platforms. Every token, decentralised exchange, lending market and NFT is a smart contract or a set of them. They are also where a large share of crypto’s losses have occurred. Understanding what a contract is, and what it cannot do, is the foundation for everything in DeFi.
How it works
- WriteA developer writes the rules as code.
- DeployA transaction stores the code on the chain.
- AddressThe contract now lives at its own address.
- CallUsers send transactions to its functions.
- ExecuteEvery node runs the same code and records the result.
A smart contract is a computer program stored on a blockchain. It has its own address, it can hold coins and tokens, and it contains functions that anyone can call by sending a transaction.
When a function is called, every node on the network runs the same code with the same inputs and arrives at the same result. The outcome is recorded in the next block. No company’s server is involved, and usually no operator can step in to change the result.
The term was coined in the 1990s by the computer scientist Nick Szabo, well before blockchains existed. Ethereum, launched in 2015, was the first widely used blockchain designed to run general-purpose contracts.
In plain words
A vending machine is the classic comparison. Its rule is fixed: put in the right coins, press a button, receive the item. No shopkeeper decides. A smart contract is a vending machine made of code, standing in a public place, that handles digital assets. If the machine has a fault that lets a can drop without payment, it will do that too, for anyone who finds the fault.
What it can and cannot do
CAN
Follow its code without an operator.
- Hold and send coins and tokens
- Keep records, such as balances
- Call other contracts
- Enforce rules identically for everyone
CANNOT
Go beyond its code or the chain.
- See outside data without a feed
- Act without being called
- Undo a completed transaction
- Fix its own bugs
Four properties follow from running on a blockchain:
- Deterministic. The same call always gives the same result on every node.
- Public. The compiled code and all stored data are visible to anyone, as is every call ever made.
- Paid for by the caller. Each step of execution costs gas.
- Hard to change. Deployed code is permanent by default.
A contract cannot look at the outside world. It cannot check a share price, a football score or a bank balance. Outside information has to be written to the chain by a service called an oracle, and the contract is then only as reliable as that feed.
Going deeper
Tokens are contracts. A token is a contract that keeps a table of balances. Sending a token means calling its transfer function.
Composability. Contracts can call other contracts. An application may route one user action through an exchange contract, a lending contract and several token contracts in a single transaction. This lets developers build quickly, and it means a fault in one contract can spread to those that depend on it.
Permissions. To let a contract move your tokens, you normally grant it an allowance first. These token approvals stay in force until removed, and are a common route for theft.
- You approveYou let a contract spend a token from your wallet.
- Allowance is storedOften unlimited, and it stays until revoked.
- Contract can spendUp to the allowance, without asking again.
Upgradeable contracts. Many projects deploy contracts in a form that lets an administrator replace the logic later. That allows bugs to be fixed. It also means that whoever holds the administrator key can change the rules, so the code visible today may not be the code running tomorrow.
Audits. Independent reviewers examine contract code before or after launch. An audit lowers the chance of known kinds of error. It does not show that the code is free of faults, and audited contracts have been exploited.
An escrow, and a bug in itHypothetical illustration
A hypothetical contract holds payments for goods. A buyer deposits 10 units. The rules are: if the buyer confirms delivery within 30 days, the 10 go to the seller. If not, the buyer may call a refund function after day 30 and receive the 10 back.
- Case 1. The buyer confirms on day 12. The contract sends 10 to the seller.
- Case 2. No confirmation arrives. On day 31 the buyer calls refund and receives 10.
Both work as intended, with no intermediary.
Now suppose the refund function contains a mistake: it pays out without recording that the deposit has been returned. The contract holds 50 units in total, from five buyers who each deposited 10.
- One buyer calls refund on day 31 and receives 10. The contract holds 40.
- The same buyer calls it again. The contract still believes a deposit is owed and pays another 10.
- After 5 calls, 5 x 10 = 50 units have been paid to one address, and the contract is empty.
The code did exactly what it said. The other four buyers have no one to appeal to, and the transactions cannot be reversed.
Code runs as written
A smart contract carries out its instructions whether or not they match what its authors intended. Flaws in widely used contracts have led to very large losses, and there is usually no way to recover the assets. See smart contract risk.
What it does NOT mean
- “Smart” does not mean intelligent. A contract follows fixed instructions and makes no judgements.
- “Contract” does not mean legally enforceable. It is software. Its legal standing is a separate question.
- Open code does not mean safe code. Being readable by anyone is not the same as having been checked by someone competent.
- Trustless does not mean risk-free. Trust moves from an institution to the code, its authors and any administrator keys.
- Decentralised does not mean uncontrolled. Many contracts have owners with special powers.
Common misunderstandings
- Assuming someone can reverse a bad outcome. A contract has no support desk, and the chain has no undo.
- Treating an audit as a guarantee of safety. It is a review at one point in time.
- Signing without understanding the call. A wallet prompt describes a function call. Its effects are whatever that function does. See signing transactions.
Risks and limits
- Bugs can be exploited by anyone who finds them, often within a single transaction.
- Administrator keys can be misused, lost or stolen.
- Oracles can supply wrong or manipulated data.
- Faults spread between contracts that depend on one another.
- Execution costs gas, and complex calls can become expensive when a network is busy.
Key takeaway
A smart contract is a public program on a blockchain that runs exactly as written for anyone who calls it. That removes the need for an intermediary and places all the weight on the correctness of the code.
Sources and further reading
- Mastering Ethereum, Andreas M. Antonopoulos and Gavin Wood. Chapters on smart contracts and on smart-contract security
- Ethereum white paper (opens in a new tab), Vitalik Buterin
- Introduction to smart contracts (opens in a new tab), ethereum.org. A smart contract is a program that runs on the Ethereum blockchain at a specific address, and interactions with it are irreversible