ConceptsEthereum

How smart contracts work

Programs that live on a blockchain and run exactly as written โ€” no company in the middle. Here's what that really means, and where the risks hide.

A smart contract is a small program stored on a blockchain that runs exactly as written. When someone sends it a transaction that meets its conditions, the network executes the code and records the result. No bank, escrow agent or company sits in the middle โ€” the rules enforce themselves.

Abstract render representing smart contract logic

"If this, then that" โ€” on a public computer

The classic description is a vending machine: put in the right coins, get the snack, automatically, with no cashier. A smart contract is the same idea for money and data. For example: "if address A sends 1 ETH, then transfer 100 tokens back to A." Once deployed, it will do that for anyone, forever, unless it was written to be pausable or upgradeable.

These programs run on a virtual machine that every node executes identically โ€” most famously the Ethereum Virtual Machine (EVM). Because every node runs the same code on the same inputs, they all reach the same result, and that result becomes part of the ledger.

Gas: running code isn't free. Each operation costs a fee called gas, paid in the chain's native coin. It compensates validators and stops anyone from clogging the network with infinite loops.

What people actually build with them

  • Tokens: most coins that aren't a chain's native asset are just smart contracts tracking balances (the ERC-20 standard on Ethereum, for example).
  • Exchanges (DEXs): contracts that let people swap tokens against a shared pool, no order book or broker.
  • Lending: deposit collateral, borrow against it, all enforced by code.
  • NFTs: contracts that record unique ownership of an item or piece of media.

The catch: code is law โ€” including the bugs

Because a contract does exactly what it says, a mistake in the code is also enforced exactly. There's no manager to reverse a bad transaction. Over the years, bugs and exploits have drained billions from poorly written or unaudited contracts.

Before you interact with one: favour contracts that are well established and independently audited, understand what permissions you're granting (an "approval" can let a contract move your tokens), and remember that "audited" is not the same as "safe." This is education, not a recommendation โ€” see our risk disclaimer.
What it is
On-chain program
Runs on
A virtual machine (e.g. EVM)
Costs
Gas per operation
Main risk
Bugs are irreversible

Why it matters

Smart contracts are what turned blockchains from "digital cash" into programmable platforms. Almost everything you'll hear about โ€” DeFi, NFTs, DAOs, staking โ€” is smart contracts wearing different hats. Learn to read what a contract does and you've unlocked most of the ecosystem.

Next up: the thing that authorises every contract call โ€” Keys & signatures. Or step back to What is a blockchain.

Related guides