Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 47 ยท Getting Started

How a Smart Contract Runs

Follow a smart contract from deployment to execution: addresses, calls versus transactions, storage versus memory, the ABI and why contracts are deterministic.

A smart contract is a program, but it does not run the way a script on your laptop does. Nobody launches it, nothing keeps it running between requests, and it cannot reach out to the outside world. It sits on the chain as code plus saved data, wakes up when a transaction or another contract calls it, does its work, and goes back to sleep. Understanding this life cycle makes the rest of Solidity much easier to reason about.

This lesson walks through that life cycle from deployment to a function call, and ends with a small JavaScript object that behaves like a contract. It builds on the pieces from What Is Ethereum?.

A contract's life

Deploy A transaction stores the code at a new address.
Address The address never changes.
Call Anyone can run its public functions.
State Storage survives between calls.

Between calls, a contract simply waits.

1 / 2

Deployment

You deploy a contract by sending a transaction whose to field is empty and whose data holds the compiled creation code. The network runs that code once. It runs your constructor, then stores the final runtime code at a brand new address. The address is computed from the deployer's address and the deployer's nonce, so it is known in advance and you cannot choose it freely (there is a variant called CREATE2 that makes addresses predictable from a salt, which you will not need yet).

After deployment two things hold:

  1. The code at that address is immutable. Nobody, including you, can edit it. Changing behavior later needs a pattern such as a proxy, covered in Proxies and Upgradeable Contracts.
  2. The contract has its own balance and its own storage, both owned by the address.

A contract address looks exactly like a wallet address. Whether an address has code is a property of the state, not of its format.

Calls and transactions

There are two very different ways to run a contract function.

A call (sometimes eth_call) is a question. You ask one node to run the function against its current copy of the state and tell you the answer. Nothing is written to the chain, no block is needed, and it costs you no gas. Functions that only read data are normally used this way. Anyone can read all of a contract's data for free.

A transaction is a command. You sign it, pay gas, and wait for a validator to include it in a block. Every node then runs it and the state changes become permanent. Any function that writes storage, moves ETH or emits an event must be run as a transaction.

One subtlety: when a transaction runs a function that returns a value, the caller does not get that value back. Return values are for other contracts and for calls. If a wallet needs the outcome of a transaction, the contract emits an event (see Events and Logs) or the caller reads the state afterward.

State, memory and the stack

Where a value lives decides how long it lasts and how much it costs:

Place Lifetime Cost Typical use
Storage Permanent, part of the state Very high to write Balances, owners, settings
Memory One function call Low, grows with size Temporary arrays and strings
Calldata The incoming call data Cheap, read-only External function arguments
Stack One function call Cheapest Small local values

The key difference is that storage persists. A variable declared at contract level (a state variable) lives in storage, so a counter incremented today still has the new value tomorrow. Local variables inside functions vanish when the call ends. Storage, Memory and Calldata goes deep on this, so for now just keep the picture.

The ABI: how callers know what to say

A contract's code only understands raw bytes. The caller sends bytes and the contract decides what to do with them. To make that workable, Solidity defines the ABI (application binary interface), a standard way to turn "call transfer with this address and this amount" into bytes, and to turn returned bytes back into values.

The first four bytes of the data are a function selector, which identifies the function by a hash of its name and parameter types. The rest is the encoded arguments. The compiler also produces an ABI description, a JSON file that lists every public function with its inputs, outputs and whether it changes state. Wallets and libraries read that file so you can write contract.count() instead of building bytes by hand. You will build a selector yourself in ABI Encoding and Low-Level Calls.

Determinism and why there is no internet

Every node re-executes every transaction, so every node must get exactly the same answer. That rules out anything that could differ between machines or between moments:

  • No network requests. A web server could return a different answer to each node, or fail for some of them.
  • No true randomness and no local clock. The only time a contract sees is the block's timestamp, the same for everyone.
  • No reading files or the local machine.

This is also why a contract cannot act on its own. It never wakes up at midnight to pay someone. Something outside, a wallet or a bot, must send a transaction. When a contract needs outside facts such as a price, a trusted service called an oracle posts them on-chain as a transaction first. See Oracles, Randomness and Off-Chain Data.

A contract as a JavaScript object

The stand-in below is a plain JavaScript object, not real Solidity or EVM code. It has storage, a table of functions with a flag saying whether they change state, and a dispatch function that plays the role of the ABI selector. It also shows the difference between a call (read-only, runs on a copy) and a transaction (commits changes).

JS A contract object with a call dispatcher

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that the failed transaction left count untouched: the work happened on a copy that was thrown away, which is how a real revert behaves. Reading with call costs nothing, while send is the only path that saves anything.

A contract that cannot reach the internet

To feel determinism, let three simulated "nodes" run the same logic. The first function depends only on its input and on block data, which is the same for everyone, so all nodes agree. The second reads each node's own local clock, so the nodes disagree, and a network that behaved like that could never reach consensus.

JS Deterministic versus non-deterministic

Press Run (or Ctrl + Enter) to see the console output here.

No output.

What is real: the call versus transaction split, storage that persists, the selector idea, and rollback on failure follow how Ethereum works. What is the stand-in: here the "node" is one JavaScript object, there is no gas, no signatures and no bytes. A real selector is four bytes of a Keccak-256 hash, not a property name.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main difference between a call and a transaction?

    A call is run by one node as a question, so nothing is written. A transaction is signed, paid for with gas and included in a block, so its state changes become permanent.
  2. 2. Why can a smart contract not make a web request?

    Determinism is required for consensus. External data must be posted on-chain by an oracle in a transaction, so every node sees the same value.
  3. 3. What do the first four bytes of call data identify?

    The ABI uses a four-byte selector, a hash of the function name and parameter types, followed by the encoded arguments.
  4. 4. After a contract is deployed, what can you do to its code?

    Deployed code cannot be changed. Upgradable systems keep logic behind a proxy that points to new code.

Summary

  • Deploying stores immutable runtime code at a new address with its own balance and storage.
  • A call reads for free from one node; a transaction is signed, costs gas and commits changes in a block.
  • Storage persists between calls; memory, calldata and the stack last only for one call.
  • The ABI turns function names and arguments into bytes, starting with a four-byte selector.
  • Contracts must be deterministic, so they cannot use the internet, local clocks or true randomness.
  • A contract never acts by itself. Something outside must send a transaction.

Next: Your Solidity Toolbox.