Smart Contract

A smart contract is a program stored on a blockchain that runs automatically when its conditions are met; bugs in the code are a real risk to funds held in it.

A normal contract needs people to honour it and courts to enforce it. A smart contract enforces itself: the rules are code, and the blockchain runs them. That makes DeFi, stablecoins and tokens possible — and it also means a single bug can drain millions with no one able to undo it.

Category
Cryptocurrency and Blockchain
Reading time
4 min
Also called
Self-executing contract, On-chain program, Contract account
Updated

Smart contracts in plain words

A smart contract is a bundle of code and data placed on a blockchain through cryptographically signed transactions.1

ethereum.org compares it to a vending machine: specific inputs guarantee predetermined outputs.2 The code runs according to its rules and cannot be changed once created.2

That also means mistakes are hard to fix: deployed code usually cannot be patched, and stolen assets are mostly irrecoverable.3 See Stablecoin and Liquidation.

How a smart contract works, step by step

  1. Write the code

    A developer writes the rules as a program.

  2. Deploy it

    The code and data are deployed on the blockchain with a signed transaction.1

  3. Send a transaction

    A user calls the contract, for example to swap or deposit.

  4. Check conditions

    The contract follows its 'if this then that' rules.2

  5. Execute automatically

    If conditions are met, the result is predetermined, like a vending machine.2

  6. Record publicly

    Anyone can track the transfers on the public blockchain.2

Worked examples

Example 1 — An escrow

Rules run automatically

A contract holds 1,000 USDC and pays the seller when the buyer confirms delivery, or refunds the buyer after 30 days.

Example 1 — An escrow — calculation
If confirmedSeller receives 1,000 USDC
If no confirmation by day 30Buyer refunded 1,000 USDC

Specific inputs guarantee predetermined outputs.2

Example 2 — Automatic liquidation

Liquidated at $4,900

A lending contract liquidates a loan if collateral falls below 125% of the debt. Debt is $4,000.

Example 2 — Automatic liquidation — calculation
Liquidation level$4,000 × 1.25 = $5,000
Collateral now$4,900 → liquidated

The contract follows its coded rules with no human decision.2

Example 3 — A bug

−$2,000,000

A contract holding $2,000,000 has a flaw that lets an attacker withdraw funds.

Example 3 — A bug — calculation
Funds drained$2,000,000
Patch the deployed code?Usually not possible

Attackers seek to exploit vulnerabilities in smart contracts, and stolen assets are mostly irrecoverable.3

Smart contract vs. traditional contract

AspectSmart contractTraditional contract
Enforced byCode on a blockchainPeople and courts
SpeedAutomaticCan take days or years
ChangesUsually cannot be changedCan be amended by agreement
MistakesHard or impossible to reverseCan be disputed

Where smart contracts are used

  • Decentralised exchanges

    Pools that swap tokens.

  • Lending

    Collateralised loans with automatic liquidation.

  • Stablecoins

    Issuing and redeeming tokens.

  • Token creation

    Defining supply and transfers.

Advantages and disadvantages

Advantages

  • Run automatically without intermediaries.
  • Rules are public and predictable.
  • Anyone can check the transactions.
  • Available 24/7.

Disadvantages

  • Bugs can drain funds.
  • Deployed code is hard to fix.
  • Stolen funds are rarely recovered.
  • Legal status differs by country.

Going deeper

Read as far as you need. Each level builds on the one before it.

Code that runs itself

Beginner

A smart contract is code and data on a blockchain.1 Like a vending machine, inputs lead to set outputs.2

Immutable rules

Intermediate

Contracts execute according to their code, which cannot be changed once created.2 That gives certainty, but also rigidity.

Security is the main risk

Advanced

Smart contracts attract attackers who exploit vulnerabilities.3 Because code usually cannot be patched, losses are mostly irrecoverable.3 Audits reduce, but do not remove, the risk.

Common mistakes

  1. Assuming 'audited' means safe

    Audits can miss bugs.

  2. Approving unlimited spending

    Letting a contract move all your tokens.

  3. Ignoring upgrade keys

    Not checking who can change a contract.

  4. Trusting copycat contracts

    Using a fake contract with a familiar name.

Check your understanding

Test yourself before moving on. Each answer comes with an explanation.

Q1A loan of $8,000 is liquidated below 150% collateral. At what collateral value?
Q2Why are smart contract bugs so costly?

Key points to remember

  1. A smart contract is a program on a blockchain.
  2. It runs automatically when conditions are met.
  3. Its rules are public and usually cannot be changed.
  4. Bugs can drain funds.
  5. Stolen funds are rarely recovered.

Frequently asked questions

Are smart contracts legally binding?

It depends on the country and situation. Rules differ; this is not legal advice.

Can a smart contract be changed?

Usually not, unless it was built with an upgrade mechanism — which adds its own trust risk.

Do I need to code to use one?

No. Wallets and apps call contracts for you.

Further learning

Sources

This article is written in our own words. The sources below support the specific facts, rules and figures marked with a number in the text. Links open the original publisher’s page.

  1. NIST CSRC Glossary. Smart contract (opens in a new tab) Checked .
  2. ethereum.org. Introduction to smart contracts (opens in a new tab) Checked .
  3. ethereum.org. Smart contract security (opens in a new tab) Checked .

Educational content, not financial advice. Examples use hypothetical prices and simplified costs; actual fees, margin rules and regulations vary by broker, market and country. We are not affiliated with any publisher linked above. Last reviewed . Report an error or request a source credit.