Skip to main content

Type a word from a question or an article title.

1 · Basics

What is a smart contract and why you can trust it with money

A smart contract is a program on a blockchain that executes a deal's terms by itself. How it works, how it differs from an ordinary contract and what its risks are.

4 min readUpdated:
In this article

A smart contract is a program that lives on a blockchain. It has its own address, like a wallet, and it can receive, hold and send money by rules written in its code. The name misleads: "smart" here means automatic, not intelligent, and "contract" is not a legal agreement but code.

A vending machine as an image

The classic comparison is a vending machine. You insert coins, press a button and the machine gives you the product. No seller you must trust: the mechanism checks the conditions and carries them out. A smart contract is the same machine, only with any number of conditions and "products", and it runs identically for everyone on thousands of computers.

How a smart contract differs from an ordinary program

It is public. The contract's code and state are visible to all in a block explorer. Anyone can check what will happen to the money.

It is executed by the network. There is no server that can be switched off or bribed. Every node of the network runs the code and gets the same result.

It usually cannot be changed after launch. If a contract is written without upgradability, the rules stay the same forever. That is both the main virtue and the main risk - mistakes cannot be fixed.

It cannot "start" anything by itself. A contract acts only in response to a transaction: someone must call its function and pay the network fee.

What a contract can and cannot do

A contract is great at holding money and enforcing rigid rules: "if condition X is met, send amount Y to address Z". But it does not know what happens outside the blockchain. Whether the goods arrived, whether the work was good, whose word is truer - code cannot check any of that.

That is why in real-world deals a smart contract is complemented by a person - the guarantor. The contract ensures the money will not vanish and will be paid by rules, while the guarantor settles questions code cannot: who is right in a dispute. More in the article on escrow and guarantors.

What a contract consists of

Looking inside an escrow contract as an example:

  • State - the data the contract remembers: the list of deals, amounts, participants, statuses.
  • Functions - actions that can be called: create an offer, accept, confirm receipt, open a dispute.
  • Access rules - who may call a function: only the buyer can confirm receipt, only the guarantor can give a ruling.
  • Events - entries in the blockchain's journal: "deal created", "deal settled". Interfaces and services read them.

For example, in our contract a deal has five states: none, offer, active, dispute, settled. Transitions between them are strictly limited - for instance from "settled" there is no exit at all. How this is written in code is in the article on the state machine in Solidity.

Risks to know about

Bugs in the code. A contract does exactly what is written, not what the author meant. Several famous losses in blockchain history happened exactly like this. So serious contracts go through testing, static analysis and independent audit - see the article on testing contracts.

Irreversibility. There is no "undo" button. If you signed a transaction to a malicious contract the money cannot be returned.

Dependence on the environment. A contract depends on the tokens and other contracts it interacts with. A token with non-standard behaviour can break the logic - see the article on non-standard tokens.

The human factor around it. A contract can be flawless while the interface is fake. So check the site address and read what you sign.

How to check a smart contract

  1. Find the contract address on the service's official site.
  2. Open it in your network's block explorer: check the code is published and verified.
  3. See who the owner is and what they can do: can they change rules, take money, pause.
  4. Find out whether there is an audit and what the developers have done before.

More on reading an explorer in the article how to read Etherscan.

Now you have the whole picture: the blockchain stores data, the wallet signs, the smart contract executes rules. The next step is to learn what digital assets consist of: tokens and NFTs, and then what to pay with: stablecoins and their risks. Can you read code? See the Solidity tour.

Mentioned in

Ready to try it?

Create your first deal in a couple of minutes. The money is locked in a smart contract and paid out by rules you can see in advance.