Book a free call

How Does a Smart Contract Work, Step by Step?

A smart contract is code on a blockchain that runs automatically when its conditions are met. Here is how it executes, its limits, and where it goes wrong.

Hertiv engineering teamReviewed by a senior engineer
6 min read
How Does a Smart Contract Work, Step by Step?
On this page

Short answer

A smart contract is a program stored on a blockchain that runs automatically when someone sends it a transaction that meets its conditions. The network's nodes run the same code, agree on the result, and record the outcome on the chain, so no single party controls whether it executes.

What is a smart contract?

A smart contract is a program stored on a blockchain that executes its own rules when its conditions are met. It holds its own state, such as balances and deadlines, and it changes that state only according to the code that was deployed.

The idea is simple: instead of a person or a company deciding whether an agreement was kept, the code decides. A buyer deposits funds, a seller delivers the agreed item or proof, and the contract releases the funds. If the conditions are not met, the funds stay put or return to the buyer.

Smart contract Traditional agreement
Who enforces it The code, automatically People, then courts if needed
When it runs As soon as the conditions are met When someone takes action
Cost of running it Gas fees for each step Admin time and legal fees
Reversing a result Not possible once confirmed Possible through an agreed process

Smart contracts run on blockchains that support programmable logic, such as Ethereum and its layer-2 networks. The contract is deployed once, and after that its code cannot be changed unless an upgrade mechanism was written into it from the start. For a closer look at the build side, see smart contract development.

How does a smart contract execute?

A smart contract executes when someone sends a transaction to its address. The transaction calls a function in the contract and passes in the inputs the function needs, such as an amount or a deadline.

The steps look like this:

  1. A transaction is sent. A user or another contract calls a function on the contract's address.
  2. Every node runs the code. Validators run the same function with the same inputs and the same starting state.
  3. Conditions are checked. The code tests its rules: is the caller allowed, is the deadline still open, is the amount correct?
  4. State changes or the call fails. If the checks pass, the contract updates its stored state. If a check fails, nothing changes and the transaction is rejected.
  5. The result is recorded. The new state is written into a block, and every node agrees on it.

Because every node runs the same code on the same state, the result is the same everywhere. That shared execution is what makes the outcome trustworthy without a central operator.

What happens when the conditions are met?

When the conditions are met, the contract updates its own state and may move assets or send a message to another contract. A payment contract releases funds to the seller. A voting contract counts a vote. A lending contract updates a borrower's balance and the interest owed.

These changes are final once the block is confirmed. There is no undo button and no administrator who can reverse a transfer that the code allowed. That is why the conditions in the code matter more than any written description of the deal.

The contract can also emit an event, which is a log entry that outside systems can watch. Wallets, dashboards and other applications listen for these events to show users what happened, such as a payment arriving or a deadline passing.

Where does a smart contract get its data?

A smart contract cannot see the outside world on its own. It only knows what is already on the chain: balances, timestamps, other contracts and the data passed in with a transaction.

For anything off-chain, such as a price, a delivery confirmation or a weather reading, the contract needs an oracle. An oracle is a service that writes outside data onto the chain, and the contract reads it like any other value. The contract trusts the oracle's data, so a bad oracle can make a good contract behave badly.

This limit shapes what smart contracts are good at. They work best when the facts they need are already on-chain or can be delivered reliably by an oracle, and worst when the key fact is a judgement call that only a person can make.

What are the limits of a smart contract?

The main limits of a smart contract are that it cannot change its own code, cannot fetch outside data, and cannot fix its own bugs. Once deployed, the code is the agreement, including any mistakes in it.

  • Immutability. A bug stays in the contract unless an upgrade path was built in. Fixing it usually means deploying a new contract and moving users over.
  • No outside data. Prices, deliveries and real-world events need oracles, which add their own trust and failure points.
  • Execution cost. Every step costs gas, so heavy computation or large storage is expensive on a public chain.
  • Public by default. Contract code and state are visible to everyone, so private logic and sensitive data do not belong in a plain contract.
  • Determinism. The code must produce the same result on every node, so it cannot use randomness or the current time in a loose way.

These limits are why an independent smart contract audit is standard practice before a contract holds real value.

Example: an escrow contract in practice

Example (hypothetical): a buyer wants to pay a seller for a digital item, but neither trusts the other to go first. They agree to use an escrow contract with a fee of 0.5 percent.

The buyer deposits 10 units of a token into the contract. The seller delivers the item and marks the delivery complete. The buyer confirms receipt, and the contract releases the payment. The fee is 0.5 percent of 10, which is 0.05 units, so the seller receives 9.95 units and the fee account receives 0.05 units. The numbers add up: 9.95 plus 0.05 equals 10.

If the buyer does not confirm within 14 days, the contract lets either side open a dispute. A named arbitrator can then release the funds to one side or return them to the buyer. If the buyer never confirms and no dispute is opened, the funds stay locked, which is why the contract needs a clear timeout and a way out.

What are the common mistakes?

The most common smart contract mistakes are treating the code as a draft, trusting outside data without checking it, and leaving no way to handle a dispute.

  • Deploying without an audit. A small logic error can lock or drain funds, and there is no undo after deployment.
  • Trusting an oracle blindly. If the price feed is wrong or stale, the contract will act on the wrong price.
  • No timeout or dispute path. Funds can sit locked forever when one side stops responding.
  • Upgrade logic added as an afterthought. An upgrade mechanism that was not planned carefully can hand control of the contract to one party.
  • Copying code without understanding it. Code copied from another project may assume conditions that do not hold in yours.

How to decide whether a smart contract fits

A smart contract fits when the agreement is simple enough to write as rules, the facts are on-chain or reliably delivered, and both sides want the outcome to be automatic rather than negotiated.

  1. Write the rules in plain language first. If you cannot state the conditions clearly, the code will not be clear either.
  2. Check where the facts come from. On-chain facts are safe; off-chain facts need a trusted oracle.
  3. Decide what happens when something goes wrong. Timeouts, disputes and refunds need to be in the design, not added later.
  4. Estimate the cost. Gas fees and audit costs are part of the price, so compare them with the cost of a manual process.
  5. Start small. Deploy on a test network, then hold a small amount of real value before scaling up.

If the rules are clear and the facts are available, a contract can remove a middleman. If the deal needs judgement, negotiation or privacy, a plain agreement is usually the better tool. A team that builds on blockchain development can help you weigh the two.

Key takeaways

  • A smart contract is code on a blockchain that runs automatically when its conditions are met
  • Every node runs the same code on the same state, so the outcome is agreed without a central operator
  • Contracts cannot see off-chain data, so prices and real-world facts need an oracle
  • Once deployed, contract code cannot be changed unless an upgrade path was built in
  • An audit, a timeout and a dispute path are the minimum before a contract holds real value

Frequently asked questions

Is a smart contract a legal contract?

Not by itself. A smart contract is code that executes rules automatically. Whether it is also a legal contract depends on the law in your jurisdiction and on whether the parties intended a legal agreement. Many teams pair the code with a written agreement that states the terms in plain language.

Can a smart contract be changed after deployment?

Only if an upgrade mechanism was written into it from the start. Otherwise the code is fixed, and fixing a bug means deploying a new contract and moving users and funds to it. This is why careful design and testing matter so much before deployment.

Do smart contracts need a blockchain?

Yes, in the usual sense. A smart contract runs on a blockchain so that every node executes the same code and agrees on the result. Without that shared execution, there is no reason to use a contract instead of a normal program running on a server.

What is gas in a smart contract?

Gas is the fee paid to run code on a public blockchain. Each operation costs a small amount, and the total depends on how much work the contract does. Complex contracts cost more to run, which is one reason to keep contract logic simple.

Can a smart contract hold money?

Yes. A contract can hold tokens and release them according to its rules, which is how escrow, lending and payment contracts work. That is also why audits matter: a bug in a contract that holds funds can lock or drain those funds.

What happens if a smart contract has a bug?

The bug stays in the contract unless an upgrade path exists. Depending on the bug, funds can be locked, sent to the wrong address, or drained. There is no administrator who can reverse the result, so testing and auditing before deployment are the main defences.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: blockchain systems in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Planning a smart contract that holds real value? Talk to Hertiv.

A senior engineer replies within one business day. Or see our Smart Contract Development service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to [email protected].

Prefer email? Write to [email protected]
Estimate costBook a call