Smart Contracts Explained: Code That Runs on a Blockchain

what is a smart contract

You keep hearing about them, so what is a smart contract? It is a program on a blockchain that runs its rules automatically when someone sends it a transaction.

For the wider picture, start with our explainer on how does blockchain work.

Item Key fact
What it is A program made of code and data that runs on the Ethereum blockchain
Where it lives At a specific address on the blockchain
Languages named by ethereum.org Solidity and Vyper
Maximum contract size 24KB
Deletion Cannot be deleted by default

These figures come from ethereum.org’s developer introduction to smart contracts. Figures like these can change as the network develops, so check the source before you rely on them.

How a smart contract works on Ethereum

A smart contract is, in the words of ethereum.org’s developer documentation, a program that runs on the Ethereum blockchain. It is a collection of code (its functions) and data (its state) that sits at a specific address on that blockchain. The ethereum.org introduction uses Ethereum as its example, and so does this explainer.

A contract is also a type of account. It has a balance and can be the target of transactions, but no user controls it: it is deployed to the network and runs as programmed.

The usual life of a contract follows a short sequence:

  1. A developer writes the code in a smart contract language such as Solidity or Vyper.
  2. The code is compiled so that Ethereum’s virtual machine can interpret and store it.
  3. The contract is deployed to the network, where it resides at a specific address.
  4. A user account submits a transaction that executes a function defined on the contract.

The point of all this is automatic enforcement. A smart contract can define rules, like a regular contract, and the code enforces them automatically instead of a person doing it.

If you want to see how the computers that make up such a network fit together, read about what is a blockchain node.

The vending machine comparison

Ethereum.org calls a vending machine perhaps the best metaphor for a smart contract, and credits the description to Nick Szabo. You put in money, make a selection, and the snack is dispensed. That logic is programmed into the machine, just as a smart contract has logic programmed into it.

The documentation turns the idea into a short Solidity example. In it, the contract starts with a stock of 100 cupcakes and sells them on fixed terms, as the ethereum.org example shows. The rules in that example are simple:

  • The address that deploys the contract becomes its owner.
  • Only the owner can refill the stock.
  • Anyone can buy cupcakes, but they must pay at least 1 ETH per cupcake.
  • The purchase fails if there is not enough stock to complete it.
what is a smart contract Screen closeup
A monitor displays blurred multicolored lines beneath a reddish reflection.

Ethereum.org notes that, just as a vending machine removes the need for a vendor employee, smart contracts can replace intermediaries in many industries. That is a description of what the technology can do, not a promise that any particular contract is well built or safe to use.

Who can write and deploy a smart contract?

Anyone can. Ethereum.org describes smart contracts as permissionless: you need to learn to code in a smart contract language and have enough ETH to deploy your contract.

Deploying is technically a transaction, so you pay gas the same way you would for a simple ETH transfer. Gas costs for contract deployment are far higher than for a simple transfer, according to the same documentation.

Contracts are also composable. They are public on Ethereum and can be thought of as open APIs, so one contract can call other contracts to extend what it can do. Contracts can even deploy other contracts.

Because anyone can deploy code, the mere existence of a contract at an address says nothing about whether it is trustworthy. Treat that as a reason for caution, not as a recommendation about any contract.

What smart contracts cannot do

Ethereum.org lists limits that are worth knowing before you picture contracts doing everything. Two stand out:

  • No direct view of the outside world. A contract cannot retrieve data from offchain sources, so it cannot respond to real-world events on its own.
  • A size cap. A smart contract can be a maximum of 24KB or it will run out of gas, though the documentation says a technique called the Diamond Pattern can work around this.

The offchain limit is by design. Relying on external information could jeopardise consensus, which ethereum.org says is important for security and decentralization. If you are curious how networks reach consensus, see our comparison of proof of work vs proof of stake.

Blockchain applications still need outside data, and the answer is oracles: tools that ingest offchain data and make it available to smart contracts. A contract that depends on an oracle depends on that tool as well, which is one more thing to understand before trusting an application.

Risks and safeguards to understand

The same features that make smart contracts useful create risks. Ethereum.org states that smart contracts cannot be deleted by default and that interactions with them are irreversible. A flawed contract or a mistaken transaction therefore cannot simply be undone afterwards.

Scams and rug pulls exist in this space, and anyone can publish a contract, so loss of funds is a real possibility. This site does not tell you what to use or avoid; it only explains why caution is sensible.

One safeguard the documentation describes is the multisig contract. It is a smart contract account that requires multiple valid signatures to execute a transaction. Ethereum.org calls this very useful for avoiding single points of failure for contracts holding substantial amounts of ether or other tokens, and says it divides responsibility between multiple parties.

A multisig reduces one kind of risk but does not remove the others. The code, the oracles it relies on and the people holding the keys all still matter.

Frequently asked questions

Is a smart contract the same as a legal contract?

Show answer

Not necessarily. Ethereum.org says smart contracts can define rules, like a regular contract, and enforce them through code. Whether a given smart contract has legal effect depends on the situation and the place, and this site cannot give legal advice.

Can a smart contract be deleted or reversed?

Show answer

Not by default. A deployed contract stays at its address unless it was built otherwise, and once you send it a transaction the result cannot be reversed. Treat every transaction as final before you submit it.

Can a smart contract check a price or the weather by itself?

Show answer

No. A contract cannot retrieve data from offchain sources alone. Oracles are the tools that ingest offchain data and make it available to contracts.

What does it cost to deploy a smart contract?

Show answer

You need enough ETH to deploy it and must pay gas, as with a simple ETH transfer. Ethereum.org says gas costs for contract deployment are far higher than for a simple transfer, and it gives no fixed amount.

This article is for general information and education only and is not financial, investment, tax or legal advice.