Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Smart Contracts  ›  Lesson

What a Smart Contract Is

Smart Contracts 12 minA smart contract is a program stored on a blockchain at its own address that every node executes identically when a transaction calls it, so its rules run without a middleman and cannot be quietly changed.

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.

The shortcut

To design or read a contract, list its state, functions and rules: what it remembers, what can be called, and which checks each call enforces.

The bicycle escrow contractStep 1 of 4
  1. State

    The buyer (Asha), the seller (Ravi), the amount held, and a flag recording whether the money has been released.

Notice the gap: if Ravi never delivers, this simple version has no way to refund Asha. Designing a contract means thinking through every path in advance, because fixing it later is hard.

The trap is believing that "on the blockchain" means "correct". A contract does exactly what its code says, bugs included.

Drills

Answer fast — trust the rule.

quick drill

When does a smart contract's code run?

quick drill

Why can't a contract fetch a web page while it runs?

quick drill

A call to release passes its first check but fails a later one. What happens to the changes it had made?

quick drill

Which statement about deployed contract code is true?

Remember

  • A smart contract is code at an address that every node runs the same way.
  • Shortcut: read any contract as state, functions and rules.
  • Trap: on-chain does not mean correct. Bugs run faithfully too, and code cannot be patched in place.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainAn escrow without a middleman

Explain how the bicycle escrow contract replaces a middleman, what guarantees it gives Asha and Ravi, and one situation that this simple version does not handle.