Week 5
Ethereum And Smart Contracts
- Blockchain from zero, week 5 of 12
- From a shared ledger to a shared computer
Outcomes
What You Will Be Able To Do
- Tell EOAs from contract accounts
- Explain smart contracts and the EVM
- Compute a fee from gas
- Send a testnet transaction
- Read the chain with web3.py
Idea
Why Ethereum
- Vitalik Buterin proposed it in 2013
- Network launched on 30 July 2015
- Goal: run programs, not only record payments
- A shared computer anyone can inspect
Idea with limits
The World Computer, Honestly
- One shared state, one set of rules
- Every node runs every program
- Slow and costly compared with normal servers
- Value: results nobody can quietly change
Accounts
World State: A Table Of Accounts
- Each block moves the state to a new state
- Account fields: address, balance, nonce
- Contracts also store code and data
- Addresses are 20 bytes, written as 0x plus 40 hex characters
| Account | Controlled by |
|---|
| Externally owned | A private key |
| Contract | Its own code |
0xAb...12 balance 1.5 ETH nonce 7 (EOA)
0xCd...34 balance 0.0 ETH nonce 1 code, storage (contract)
EOA and contract
Two Kinds Of Account
- EOA: private key controls it, can start transactions
- Contract: code controls it, reacts when called
- Contracts have no private key
- Only EOAs begin a chain of calls
Fields
Anatomy Of A Transaction
- Recipient, value, optional data
- Gas limit and fee settings
- Nonce and signature
- Kinds: transfer, contract call, contract creation
to: 0xRecipient
value: 0.001 ETH
data: 0x (empty)
gasLimit: 21000
nonce: 7
signature: (r, s, v)
Definition
What Is A Smart Contract?
- Code plus storage at an address
- Runs exactly as written when called
- Anyone can read and call it
- Not a legal contract, just a program
Execution
The EVM
- Contracts compile to bytecode of opcodes
- A stack of 256-bit values plus memory and storage
- Every node runs the same instructions
- Same input always gives the same result
Fees
Gas: Paying For Computation
- Every opcode costs gas
- Simple transfer uses 21,000 gas
- Storage writes are costly
- Gas limit stops endless loops
EIP-1559
Fee = Gas Used X (Base Fee + Tip)
- Base fee is set by demand and burned
- Tip goes to the validator
- Prices quoted in gwei, a billionth of ether
- London upgrade, August 2021
gas_used = 21_000
price = 20 # gwei, base fee + tip
fee_gwei = gas_used * price
fee_eth = fee_gwei / 1e9
print(fee_eth) # 0.00042
Units and practice
Ether, Wei And Testnets
- 1 ether = 10^18 wei, 1 gwei = 10^9 wei
- Testnet follows main network rules
- Test coins come free from a faucet
- Sepolia is the common application testnet
| Unit | Wei |
|---|
| wei | 1 |
| gwei | 10^9 |
| ether | 10^18 |
Hands-on
Live: Your First Testnet Transaction
- Create a learning wallet, switch to Sepolia
- Request test ETH from a faucet
- Send 0.001 ETH to a second account
- Paste the hash into a block explorer
Block explorer
Reading A Transaction Page
- Status, block number, timestamp
- From, to, value
- Gas used and effective gas price
- Fee equals gas used times price
Python
Reading The Chain With Web3.Py
- Connect through a JSON-RPC endpoint
- Reading is free and needs no key
- Replace the URL with your own Sepolia endpoint
from web3 import Web3
URL = 'https://ethereum-sepolia-rpc.publicnode.com'
w3 = Web3(Web3.HTTPProvider(URL))
print(w3.is_connected())
print(w3.eth.block_number)
block = w3.eth.get_block('latest')
print(block['baseFeePerGas'])
bal = w3.eth.get_balance('0xYourAddress')
print(Web3.from_wei(bal, 'ether'))Week 5 in five lines
Recap
- Ethereum tracks accounts and runs code
- EOAs start transactions, contracts react
- The EVM runs bytecode identically everywhere
- Gas prices computation, fee is gas times price
- Testnets let you practise safely
Week 6
Next Week: Your First Contract
- Solidity basics
- Compile and deploy in Remix
- Call your contract on Sepolia
- Read your contract on Etherscan
Hands-on
Questions And Exercise
- Send a testnet transaction
- Check the fee by hand on the explorer
- Run the web3.py script
- Explain EOA versus contract