Ethereum And Smart Contracts
You learn how Ethereum extends the blockchain idea from payments to programs: accounts, gas, the EVM and smart contracts. You set up a wallet, get free testnet coins and read live network data with Python.
You Will Be Able To
- Explain the difference between externally owned accounts and contract accounts.
- Describe what a smart contract is and what the EVM does.
- Calculate a transaction fee from gas used, base fee and priority fee.
- Send a transaction on a testnet and read it on a block explorer.
- Query the network from Python using web3.py.
From Digital Cash To A Shared Computer
Bitcoin shows that strangers can keep a shared ledger of payments. Its scripting is deliberately narrow: you can lock coins to a key or a few simple conditions. In 2013 Vitalik Buterin proposed a network built around a more general idea. Instead of only recording payments, the shared system would run programs, and every node would run them the same way.
That network is Ethereum, which launched on 30 July 2015. People sometimes call it a world computer. The phrase needs care: it is not a fast or cheap computer. It is a single, shared computer whose state and rules everyone can inspect, and whose results no single owner can change. Programs on it keep running as long as the network does.
A program stored on the blockchain is a smart contract. The name is slightly misleading. A smart contract is not a legal document and not smart. It is code with its own storage that executes exactly as written whenever someone sends it a transaction.
Accounts And State
Where Bitcoin tracks unspent outputs, Ethereum tracks accounts, like a table of balances. The entire table is the world state. Each block takes the state to a new state by applying a list of transactions.
There are two kinds of accounts. An externally owned account, or EOA, is controlled by a private key. This is what a wallet such as MetaMask manages. A contract account is controlled by code. It has no private key. It acts only when called, and its code decides what happens.
Every account has an address (20 bytes, shown as 42 characters starting with 0x), a balance in ether and a nonce that counts transactions sent from it. The nonce prevents replays: a signed transaction can be included only once, in order. Contract accounts also carry code and persistent storage, which is a table of values the contract can read and write.
- EOA: controlled by a private key, can start transactions.
- Contract account: controlled by code, reacts when called.
- Both hold an ether balance and have an address.
| Feature | Externally owned | Contract |
|---|---|---|
| Controlled by | A private key | Its own code |
| Can start a transaction | Yes | No |
| Holds code | No | Yes |
| Holds balance and nonce | Yes | Yes |
Both live in the same world state table. Source: Ethereum developer documentation: Accounts
Transactions
Only EOAs start transactions. A transaction is a signed message with several fields: the recipient address, an amount of ether, optional data, a gas limit, fee settings and the sender's nonce. The signature uses the key pair idea from week 2.
Transactions do three common things. A plain transfer moves ether from one account to another. A contract call includes data that names a function and its arguments. A contract creation has no recipient and carries the compiled code of a new contract; the network gives the new contract its own address.
Once a validator includes the transaction in a block, every node executes it and updates its copy of the state. A receipt records the outcome, including whether the transaction succeeded and which events the code emitted. If the code fails midway, all its state changes are reverted, but the sender still pays for the work done.
- Sign with your key
- Broadcast to the network
- Wait in the mempool
- Validator includes it in a block
- State updates and a receipt appears
Gas is paid for the computation the transaction uses.
The EVM
The Ethereum Virtual Machine, or EVM, is the program that executes smart contracts on every node. Contracts are compiled into bytecode, a low-level list of instructions called opcodes such as ADD, MSTORE and SSTORE. The EVM reads them one by one, using a stack of 256-bit values, temporary memory and the permanent storage of the contract.
Because every node runs the same instructions on the same input, every node reaches the same result. This is the determinism we need for consensus from week 4. Contracts therefore cannot read outside data directly, such as a website or a random number generator, because different nodes might see different answers.
Developers rarely write bytecode by hand. They write in a high-level language, most commonly Solidity, and a compiler produces the bytecode. Next week we do exactly that.
Gas: Paying For Computation
Running code on thousands of computers costs resources, so Ethereum charges for it. Each opcode has a price measured in gas. A simple ether transfer uses 21,000 gas. A call to a contract uses more, depending on how many instructions it runs and how much storage it changes. Writing to storage is among the costliest operations.
Gas also protects the network. A loop that never ends would stall every node, so the transaction runs out of gas and stops. The sender sets a gas limit, the most gas they allow the transaction to use. Unused gas is refunded.
Since the London upgrade in August 2021 (EIP-1559), the fee has two parts per unit of gas. The base fee is set by the protocol from network demand and is burned, meaning it is removed from circulation. The priority fee, or tip, goes to the validator and encourages inclusion. The fee you pay is gas used multiplied by (base fee plus tip). Prices are quoted in gwei, where 1 gwei is one billionth of an ether. For example, a transfer using 21,000 gas at a total price of 20 gwei costs 420,000 gwei, which equals 0.00042 ETH.
Ether, Wei And Testnets
Ether (ETH) is the native currency. It pays gas fees and secures the network through staking. The smallest unit is the wei, and one ether equals 10 to the power of 18 wei. Code usually works in wei to avoid fractions.
A testnet is a practice network that follows the same rules as the main network but uses coins with no market value. You obtain test coins from a faucet, a website that gives out small amounts for free. Sepolia has been the standard testnet for application developers. Testnets change over time, so check the current list and faucets on ethereum.org before you begin.
Always practise on a testnet first. Mistakes on the main network cost real money and cannot be undone, as week 2's discussion of private keys made clear. Use a separate wallet for learning and never put a main network recovery phrase into any test tool.
| Unit | In wei |
|---|---|
| 1 wei | 1 |
| 1 gwei | 1,000,000,000 (10^9) |
| 1 ether | 1,000,000,000,000,000,000 (10^18) |
A plain ether transfer costs 21,000 gas. Source: Ethereum developer documentation: Denominations and Gas
Your First Testnet Transaction
Install a browser wallet such as MetaMask, create a new account and write the recovery phrase on paper. Switch the network to Sepolia. Visit a Sepolia faucet to receive test ETH. After a short wait your balance appears.
Send a small amount to a second address that you also control. In the confirmation screen look at the gas limit, the fee estimate and the nonce. After you confirm, copy the transaction hash and paste it into a Sepolia block explorer such as sepolia.etherscan.io. You will see the status, block number, gas used, the effective gas price and the fee paid.
Spend a few minutes reading each field and matching it to this document. The sender is an EOA. The nonce is the count of transactions that account has sent. Gas used should read 21,000 for a plain transfer.
Reading The Chain From Python
The library web3.py lets you talk to an Ethereum node from Python. Install it with pip install web3. You connect through a JSON-RPC endpoint, a web address provided by a node operator. Many providers offer free Sepolia endpoints, and public ones exist too; choose one you trust and replace the example URL below if it no longer works.
The short script on the slides prints the latest block number, the base fee and the balance of an address. Reading is free and needs no private key. Writing, meaning sending a transaction, needs a signed message, which your wallet creates. In week 6 you will send transactions that deploy and call a contract of your own.
Key Terms
- Smart contract
- A program stored on the blockchain that runs when a transaction calls it.
- EVM
- The Ethereum Virtual Machine, which runs contract bytecode identically on every node.
- Externally owned account
- An account controlled by a private key that can start transactions.
- Contract account
- An account controlled by code, with its own storage and balance.
- Gas
- The unit that measures the computation a transaction uses.
- Base fee
- The per-gas price set by the protocol from demand, which is burned.
- Wei
- The smallest unit of ether, with 10 to the power of 18 wei in one ether.
- Testnet
- A practice network with the same rules as the main network and coins of no value.
- Faucet
- A website that gives small amounts of free test coins.
Exercise: Send A Testnet Transaction And Read It With Python
- Install a browser wallet, create a fresh account for learning and write the recovery phrase on paper.
- Switch to the Sepolia network and request test ETH from a faucet. Wait until your balance shows.
- Create a second account in the wallet and send 0.001 test ETH to it. Copy the transaction hash.
- Open the hash on a Sepolia block explorer. Note the block number, gas used, gas price and the fee, and check that fee equals gas used times gas price.
- Run pip install web3, then write a script that connects to a Sepolia RPC endpoint and prints the latest block number and your address balance in ether.
- Write three sentences on the difference between your account and a contract account.
Further Reading
- Ethereum Whitepaper, Vitalik Buterin, 2013, ethereum.org
- Mastering Ethereum, Andreas M. Antonopoulos and Gavin Wood, O'Reilly Media, 2018, chapters 1 to 6
- Ethereum developer documentation: Accounts, Transactions, Gas, EVM, ethereum.org
- web3.py documentation, Ethereum Foundation, web3py.readthedocs.io