What a Smart Contract Is
Asha wants to buy Ravi's second-hand bicycle, but they have never traded before. Asha does not want to pay first, and Ravi does not want to hand over the bike first. Normally they would ask a middleman to hold the money. A smart contract can play that role, as code that neither of them, nor anyone else, can bend.
The rule
A smart contract is code at an address that every node runs the same way.
- Code at an address. A contract is deployed by a transaction and gets its own address, just like an account. It holds code, storage for its own variables, and often a balance of coins.
- Run by transactions. It never acts on its own. It runs only when a transaction, or another contract, calls one of its functions.
- Run by every node. Each validating node executes the call and must arrive at exactly the same result. That is why contracts are deterministic: no randomness from a node's own machine, no reading web pages, no private clocks.
- All or nothing. If any check fails during a call, the whole call is undone and every change it made is reverted. The sender still pays for the computation used.
- Public and hard to change. Anyone can read a deployed contract's code and its history. The code cannot be edited in place. Developers who want upgrades must build that in deliberately, for example with a "proxy" contract that forwards calls to a replaceable one, and users should check whether they have.
Both words in the name mislead a little. A smart contract is not intelligent, and it is not a legal agreement. It is a program that can hold and move money.