What is a smart contract? How blockchain programs work, and their risks
What smart contracts are, the vending machine idea behind them, what they do, why their code can't be changed after launch, the main risks and their legal status.
Figures checked against 6 official and primary sources on . See the sources · How we check
Key points
- A smart contract is a program stored on a blockchain that runs exactly as written when someone sends it a transaction. ethereum.org compares it to a vending machine.
- Smart contracts power stablecoins, decentralized exchanges, NFTs and DAOs. They let people transact without a trusted middleman.
- Once deployed, the code generally can't be changed, and interactions are irreversible. Bugs can be exploited, and stolen funds are mostly unrecoverable.
- ethereum.org estimates losses from smart contract security flaws at easily over $1 billion. Read what you're signing, and limit what you approve.
In this guide
When people say Ethereum is “programmable money”, smart contracts are what they mean. They’re the reason you can swap tokens without a broker, hold a dollar stablecoin in a wallet, buy an NFT or vote in a DAO. They’re also behind some of the largest thefts in crypto.
This guide explains what a smart contract is, how it works, what it’s used for, why its code is so hard to fix, and how to protect yourself when you use one.
The idea: a digital vending machine
The term goes back to 1994, when computer scientist Nick Szabo defined a smart contract as a computerized transaction protocol that executes the terms of a contract. NIST, the US standards agency, quotes his goals: satisfy common contract conditions such as payment terms, minimize both malicious and accidental exceptions, and minimize the need for trusted intermediaries.
ethereum.org uses Szabo’s favorite metaphor: a vending machine. With the right inputs, a certain output is guaranteed. You choose a snack, pay the price, the machine checks you paid enough and gives you the snack. Nobody has to decide whether to serve you; the rules are built into the machine.
A smart contract works the same way, except the “machine” is code on a blockchain and the snacks can be tokens, dollars in stablecoin form or digital certificates.
What a smart contract actually is
ethereum.org’s definition is simple: a smart contract is a program that runs on the Ethereum blockchain, a collection of code (its functions) and data (its state) that lives at a specific address.
A few consequences follow:
- It’s an account. A smart contract has a balance and can receive transactions, but no person controls it. It runs as programmed.
- You use it by sending a transaction. Your wallet sends a transaction that calls one of the contract’s functions, and the contract does what its code says.
- Every node gets the same result. NIST explains that all the nodes that run the contract must reach the same result, and that result is recorded on the blockchain. That’s why smart contracts must be deterministic: the same input always produces the same output.
- You pay for the computing. On networks like Ethereum, the user sending a transaction to a contract pays for running its code, through gas. NIST notes there’s a limit on how much computation a call can use; if it’s exceeded, execution stops. See Ethereum gas fees explained.
- Anyone can deploy one. ethereum.org describes smart contracts as permissionless: anyone who can code and has enough ETH for gas can deploy one.
NIST notes that not every blockchain can run smart contracts. Bitcoin supports some simple built-in rules, such as the time locks NIST describes, while ethereum.org calls smart contracts the fundamental building blocks of Ethereum’s application layer. See bitcoin vs Ethereum.
What smart contracts are used for
ethereum.org lists real-world uses such as:
- Stablecoins, tokens designed to track a currency. See stablecoins explained.
- Tokens that follow shared standards, like ERC-20.
- Automatic currency exchange, the engine of decentralized exchanges. See CEX vs DEX.
- Unique digital assets, or NFTs. See what are NFTs?
- Decentralized games and insurance policies that pay out automatically.
Smart contracts are also composable: they’re public and can call each other, like open building blocks. That’s how the wider world of decentralized finance is built. See what is DeFi? and what is a DAO?
What smart contracts can’t do
- They can’t see the outside world. ethereum.org and NIST both explain that a contract can’t fetch real-world data by itself, such as a price, a sports score or the weather. That information has to be supplied by an oracle, a service that brings off-chain data on-chain. If the oracle is wrong or manipulated, the contract will still act on it.
- They can’t use judgment. A contract does exactly what the code says, even if the outcome is clearly not what anyone intended.
- They have size limits. ethereum.org notes a maximum contract size of 24KB on Ethereum.
Why the code is so hard to change
ethereum.org says smart contracts can’t be deleted by default, and interactions with them are irreversible. Deployed code is immutable by design, which is part of what makes contracts trustworthy: nobody can quietly rewrite the rules.
The downside is serious. ethereum.org warns that immutable code can make it impossible for developers to fix a vulnerable contract, and that assets stolen from smart contracts are extremely difficult to track and mostly irrecoverable.
Developers have responded with upgrade patterns. ethereum.org explains that you still can’t change a program deployed at an address, but you can change which code runs when users interact with it, for example by moving the logic to a new contract. That fixes bugs, but it also means someone has the power to change the contract, so it’s worth knowing who holds that power and how it’s controlled.
The risks for users
ethereum.org puts the total value stolen or lost because of smart contract security defects at easily over $1 billion, though figures vary. It cites:
- The DAO hack in 2016: 3.6 million ETH stolen. NIST describes the same attack as a theft worth $50 million at the time. It led to a hard fork that created Ethereum Classic. See what is a hard fork?
- The Parity multi-sig wallet hack: $30 million lost to hackers.
- The Parity frozen wallet issue: over $300 million in ETH locked forever.
The main risks for ordinary users:
- Bugs and exploits in the contract itself.
- Malicious contracts that look legitimate, often promoted through phishing sites.
- Blind signing. ethereum.org warns that raw transaction data is hard for humans to read, so people often approve transactions without understanding what they’ll do. The ecosystem is moving towards Clear Signing standards that show plain-language descriptions.
- Token approvals that let a contract spend your tokens long after you’ve stopped using it. See token approvals and how to revoke them.
- Upgrade keys held by a small team that could change a contract’s behavior.
Audits help but aren’t a guarantee. ethereum.org cautions against treating audits as a silver bullet: they won’t catch every bug.
How to use smart contracts more safely
- Reach apps through addresses you trust, not ads or links in messages. See fake crypto apps and phishing.
- Read the signing request. If your wallet can’t show you what a transaction does in plain language, slow down.
- Limit approvals to the amount you need, and revoke old ones.
- Start small with any new app, and never put in more than you can afford to lose.
- Use a separate wallet for experimenting, away from your long-term holdings. See hot vs cold wallets.
Are smart contracts legally binding?
Most smart contracts are just programs; they don’t create legal obligations by themselves. But code can also be used to define and perform a real agreement. The Law Commission of England and Wales calls these “smart legal contracts”, and in advice to the government on 25 November 2021 it concluded that the current legal framework is clearly able to facilitate and support them. In other countries, it depends on local law.
For the bigger picture, see what is Ethereum? and what is a blockchain?
Frequently asked questions
Is a smart contract a legal contract?
Can a smart contract be changed or deleted?
Does bitcoin have smart contracts?
Do I pay to use a smart contract?
Sources
- Introduction to smart contracts — ethereum.org (Ethereum documentation)
- Smart contracts — ethereum.org (Ethereum documentation)
- Smart contract security — ethereum.org (Ethereum documentation)
- Upgrading smart contracts — ethereum.org (Ethereum documentation)
- NISTIR 8202: Blockchain Technology Overview — National Institute of Standards and Technology (NIST)
- Smart contracts (advice to Government, 25 November 2021) — Law Commission of England and Wales
First published . Spotted an error or an outdated figure? Tell us and we will check it against the source.