‹ Understanding DeFi Lesson 1 of 16
Contents Lesson 1 of 16

5 min read · practitioner

What actually happens when a smart contract runs?

Almost everything in this course is built from one primitive. Get it precisely right and the rest of DeFi is arithmetic. Get it vaguely right and you will misjudge every risk in the stack.

Neither smart nor a contract

A smart contract is a program deployed to a blockchain. Its code and its stored state live on the chain, and it runs when someone sends it a transaction. That is the whole definition.

It is not smart: it has no judgement, no discretion, no ability to interpret intent. It is not a contract in the legal sense: there is no counterparty who can be sued into performing, and no clause saying "the parties shall act in good faith." The closest honest analogy is a vending machine. Preconditions are checked, the payload is dispensed, and there is nobody behind the glass to appeal to if you wanted something else.

Four properties matter, and each one becomes a risk later in this course:

  • Deterministic. Every node re-executes the same code on the same inputs and must reach the same result. A contract therefore cannot call an API, cannot read a web page, and cannot generate a random number on its own. That constraint is the entire reason oracles exist (Unit 4).
  • Public. The deployed bytecode is readable by anyone. So is the state — balances, positions, parameters. Attackers read it too, at leisure, before they act.
  • Immutable by default. Deployed code cannot be edited. It can only be replaced if the deployer built an upgrade mechanism in — and that mechanism is a key somebody holds, which is a trust assumption dressed as a feature.
  • It holds assets directly. A contract address can own tokens. There is no custodian, no ledger entry to reverse, and no bank to phone.

Execution costs money, per instruction

Every operation is metered in gas, paid in the chain's native token. The formula:

fee = gas used × gas price

A token swap that consumes 150,000 gas at a gas price of 20 gwei (20 × 10⁻⁹ ETH):

150,000 × 20 × 10⁻⁹ = 0.003 ETH

With ETH at $3,000, that is $9.00 — for one swap, regardless of whether you moved $50 or $50,000. This single fact shapes DeFi's whole economics: it makes small transactions uneconomic, it makes complex multi-step strategies expensive, and — importantly for Unit 4 — it means that when a network is congested, the machinery that is supposed to protect a protocol (liquidators, arbitrageurs) may find it too expensive to act at exactly the wrong moment.

"Code is law" is a social claim, not a technical one

In June 2016 a contract called The DAO held roughly 11.5 million ETH — about 14% of all ether then in existence. A reentrancy bug let an attacker drain about 3.6 million of it, roughly a third of the pot. The code had executed exactly as written. Under a strict reading, nothing wrong had happened.

The Ethereum community disagreed and hard-forked the chain to reverse the transfer. The minority who refused kept running the original chain, which survives as Ethereum Classic. Two chains, one history, and a permanent demonstration that "the code is final" holds only until enough humans decide otherwise.

Carry that forward. Immutability is a property of the code. It has never been a property of the people around it.

In the data

The dollar leg of a fee calculation is the price of ether, the whole history of which is below:

Interactive line chart: ETH-USD.CC (MAX)

What a market-data feed does not carry is the gas price itself: no base fee, no priority fee, no gas used. That half of the arithmetic comes from a chain explorer. Half of this lesson lives in market data and half of it lives on the chain, which is worth knowing before building anything that needs both.

Try it now

  1. Open a public block explorer (etherscan.io or similar) and find any contract address. Look at the Contract tab: you are reading the exact logic that governs whatever assets it holds. Note whether the source is verified — plenty of deployed code is not.
  2. On the same explorer, check the current gas price and compute the cost of a 150,000-gas transaction using the formula above. Convert it to dollars using the latest price on the chart above.
  3. Now navigate that chart back to mid-2016 and find the weeks around the DAO fork. Measure them and describe what the price did in one neutral sentence, then stop. One fork is a historical observation, not a pattern, and the interesting part is that the chain in front of you is the one that chose to fork.