Ethereum & Smart Contracts — Web3

Ethereum is a blockchain that runs programs. An EOA is controlled by a private key and can start transactions; a contract account holds code plus storage at…

The PTS book in the last lesson only keeps records. Ethereum goes further: it's a blockchain that can run programs. Such a program is called a smart contract: code stored at an address on the blockchain, then run by every node each time a transaction calls it. It's a lot like the drinks vending machine outside Raka's Shirt Shop. Its rules are written on the machine and the same for everyone: put money in, press a button, the drink comes out, no cashier. The machine has a location (an address), contents (storage), and every button press costs something (gas). Raka's PTS contract works like that: anyone can read its rules, and nobody can quietly change them.

Two kinds of account: EOA and contract

EOA (Externally Owned Account) Contract account
Controlled by a private key, held by a person through a wallet its own code
Can start a transaction yes no, it only reacts when called
Has code and storage no, unless delegated (see below) yes
In our example Raka 0xf39F...2266, Dinda 0x7099...79C8 RakaPoints 0x5FbDB2315678afecb367f032d93F642f64180aa3

Since the Pectra upgrade (May 2025), EIP-7702 lets an EOA point to a contract's code. The EOA then runs that code with its own storage, and is still controlled by its private key. So an eth_getCode result other than 0x no longer means "this is a contract": a delegated EOA has 23 bytes of code (0xef0100 + the contract's address).

A contract's address is computed from its creator's address and nonce (the number of transactions that account has sent). The first transaction of account #0 on a fresh local chain always gives 0x5FbD...0aa3, so RakaPoints has the same address on anyone's laptop.

A contract never runs by itself. There's no "at midnight, hand out PTS": a transaction or a call always has to trigger it. What's automatic is the rule: once it's called, the code runs exactly as written, with no middleman. That's also the main difference from Bitcoin, which was designed to send value, with a simple script for transfer rules, not for general programs.

State and transactions

State is the current condition of every account: ETH balance, nonce, and each contract's code and storage. A transaction is a signed message from an EOA that changes the state. It holds from, to, value (ETH sent along), data (the function called and its arguments), nonce, a gas limit and the gas price.

This is what changes when Raka calls earn(Dinda, 9500) for Dinda's Coffee Print Shirt ($95, passed in cents), at an example price of 11 gwei per gas:

Before After
Raka's nonce 1 2
Raka's ETH balance X X - 0.000788337 ETH (gas cost)
balanceOf[Dinda] in RakaPoints 0 95 PTS
totalSupply in RakaPoints 0 95 PTS

Raka sends no ETH to the contract (value 0). All he pays is gas.

The EVM: the computer every node runs

The EVM (Ethereum Virtual Machine) is the machine that runs contract code on every node. Solidity code is compiled into bytecode, a series of small instructions (opcodes) such as ADD or SSTORE. The EVM is deterministic: the same input gives exactly the same state on any node, which is why thousands of nodes can agree. Every opcode has a gas price: ADD is 3 gas, storing a new value in storage about 20,000 gas. So a contract is code plus storage at an address. RakaPoints's code is 2,790 bytes, and its storage is a set of 32-byte slots: slot 0 holds owner (Raka's address), slot 1 holds totalSupply.

Gas: the cost of every step

Gas is the unit of work in the EVM. A transaction's cost = gas used × price per gas. Since EIP-1559 (2021), the price per gas has two parts:

You also set maxFeePerGas, the highest price you're willing to pay. You only pay base fee + tip; the rest goes unused. The units: 1 ETH = 10^18 wei, 1 gwei = 10^9 wei.

The gas prices in this lesson (ADD 3, a transfer 21,000) hold as of the Fusaka upgrade (3 December 2025). The next upgrade, Glamsterdam, is expected in Q4 2026 according to ethereum.org, and its date is not confirmed yet. Glamsterdam will change some prices: a plain ETH transfer can become up to 71% cheaper, while creating and reading state becomes more expensive.

This gas used was measured on a local Hardhat 3 chain for the PTS contract:

Transaction Gas used
Send 0.01 ETH from Raka to Dinda 21,000
Deploy RakaPoints 677,320
earn(Dinda, 9500) the first time 71,667
earn(Dinda, 9500) the second time 37,467
earn() sent by Dinda (fails) 24,717

The first earn costs more because it fills two storage slots that were still empty. The cost with a 10 gwei base fee and a 1 gwei tip:

71,667 gas × (10 + 1) gwei = 788,337 gwei = 0.000788337 ETH
burned:         71,667 × 10 gwei = 716,670 gwei
to validator:   71,667 × 1 gwei  =  71,667 gwei

Only Raka may hand out PTS. Dinda tries calling earn() herself anyway, and the transaction fails (reverts). Before you read the log below, guess: does Dinda still pay for gas? This is the Hardhat node's real log:

eth_sendTransaction
  Contract call:       <UnrecognizedContract>
  Transaction:         0x45acf7925d6fef322c89bd5cb85fd835d259ca3202599d558c1038aef69a2914
  From:                0x70997970c51812dc3a010c7d01b50e0d17dc79c8
  To:                  0x5fbdb2315678afecb367f032d93f642f64180aa3
  Value:               0 ETH
  Gas used:            24717 of 100000
  Block #7:            0x9be58412fa89b460a1be320609b65b022f486b1ecbea61f8e51b4f8a6411db58

  Error: reverted with reason string 'Only owner'

The transaction went into block #7, all its changes were undone, but 24,717 gas was still paid and Dinda's nonce still went up. So yes, Dinda still pays. The EVM's work up to the revert has already happened and has to be paid for. Luckily, wallets and libraries simulate a transaction first (eth_estimateGas), and here the simulation fails right away with execution reverted: "Only owner". That transaction only got into a block because it was forced through with a gas limit of 100,000.

On that local chain, Raka's 0.01 ETH transfer used an effective price of 972,119,469 wei per gas (about 0.97 gwei), so it cost 0.000020414508849 ETH. Test ETH, worth nothing. On mainnet the same numbers are paid in real ETH.

Immutable, and how to "upgrade"

A deployed contract's code can't be changed. Found a bug? There's no edit button. The options: deploy a new contract and move the data over, or use the proxy pattern from the start, a small contract that holds the data and forwards calls to a logic contract that can be swapped (for example through OpenZeppelin Upgrades). A proxy means somebody can change the rules, so users have to trust whoever holds that upgrade key. That's why contracts are tested hard on a local chain and a testnet before mainnet.

Testnets and local chains

Network Chain ID Used for
Hardhat / Anvil (local) 31337 learning and testing on your laptop, instant, can be reset
Sepolia 11155111 the public testnet for apps, free test ETH from a faucet
Hoodi 560048 the testnet for validators and staking, not for apps
Mainnet 1 real money, not used in this track

Holesky has been shut down since September 2025. As of October 2026, ethereum.org still names Sepolia the default testnet for apps, and network upgrades are tried there first. Core developers have discussed a replacement testnet and a plan to retire Sepolia around 2027, so check the Networks page on ethereum.org before starting a new project.

⚠️ Wrong assumptions that waste ETH

TL;DR: Ethereum is a blockchain that runs programs. An EOA is controlled by a private key and can start transactions; a contract account holds code plus storage at an address, and only runs when called. Transactions change the state (balances, nonces, storage), and the EVM runs them deterministically on every node. Cost = gas used × (base fee + tip): the base fee is burned, the tip goes to the validator, and maxFeePerGas is the ceiling. An ETH transfer is 21,000 gas, the first earn() for Dinda 71,667 gas, and a transaction that reverts still pays for its gas (24,717 for Dinda). A contract can't be edited, only replaced or put behind a proxy. Learn on a local chain (31337) and Sepolia (11155111); Holesky is gone, Hoodi is for validators, mainnet means real money.

⚡ A smart contract: a program at an address

Meaning: A smart contract is code plus storage at an address, and it only runs when a transaction or a call reaches it. The difference from Bitcoin: Ethereum runs general programs, Bitcoin's script is only for transfer rules.

⚡ Cost = gas × price per gas

Write:

71,667 gas × (10 + 1) gwei = 788,337 gwei = 0.000788337 ETH
burned:         71,667 × 10 gwei = 716,670 gwei
to validator:   71,667 × 1 gwei  =  71,667 gwei

Meaning: Price per gas = base fee + tip. The base fee is burned, the tip goes to the validator, and maxFeePerGas is the ceiling.

⚡ A failed transaction still pays

Guess first: Dinda calls earn() herself, though only Raka may. The transaction reverts. Does Dinda still pay for gas?

Yes. The transaction went into block #7, all its changes were undone, but 24,717 gas was still paid and Dinda's nonce still went up.

Meaning: A revert undoes the changes, not the cost. The EVM's work up to the revert has already happened.

⚡ A contract can't be edited

Meaning: Found a bug after deploying? There's no edit button. The options: deploy a new contract, or use the proxy pattern from the start. That's why you test on a local chain (31337) and Sepolia first.

What you will learn