Week 7 / Blockchain from zero
Tokens
- Week 7 of Blockchain from zero
- A token is a contract with a ledger inside it.
Week 7 · Blockchain
What You Can Do By The End
- Explain what a token is
- Describe ERC-20 transfer and allowance
- Pick the right standard for a use case
- Deploy an ERC-20 on a test network
- Judge a token design
Where we left off
Recap Of Week 6
- A contract has storage, functions and events.
- It lives at an address on the chain.
- Calling a function that changes state costs gas.
- We deployed a first Solidity contract to a test network.
- Today the same skills build tokens.
Week 7 · Blockchain
A Token Is A Record In A Contract
- Ether is built into the protocol.
- Tokens are ordinary contracts.
- Balances are a mapping from address to number.
- Your keys let you change your own entry.
mapping(address => uint256) public balanceOf;
// balanceOf[alice] == 50
// means: alice owns 50 units
Week 7 · Blockchain
Why Standards Exist
- Wallets and apps need one shared interface.
- EIPs propose changes and standards.
- ERCs define contract interfaces.
- Follow the interface and every wallet understands you.
Fungible tokens
ERC-20 At A Glance
- Every unit is interchangeable with every other unit.
- Functions: totalSupply, balanceOf, transfer.
- Functions: approve, allowance, transferFrom.
- Events: Transfer and Approval.
Week 7 · Blockchain
Decimals And Integers
- The EVM stores whole numbers only.
- 18 decimals means 1 token is 10^18 units.
- Decimals is a display hint for apps.
- Never use floating point for money.
// 1.5 tokens with 18 decimals
uint256 amount = 15 * 10**17;
// stored as 1500000000000000000
Week 7 · Blockchain
A Minimal ERC-20, Part One
- State: name, symbol, supply, balances.
- The constructor mints the first supply.
- Solidity 0.8 reverts on overflow.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract MiniToken {
string public name = "Mini";
uint8 public constant decimals = 18;
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
event Transfer(address indexed from, address indexed to, uint256 value);
constructor() { totalSupply = 1000 * 10**18; balanceOf[msg.sender] = totalSupply; }
}Week 7 · Blockchain
A Minimal ERC-20, Part Two
- transfer checks the balance first.
- Then it moves units and emits Transfer.
- Real projects inherit from OpenZeppelin instead.
function transfer(address to, uint256 value) external returns (bool) {
require(balanceOf[msg.sender] >= value, "balance too low");
balanceOf[msg.sender] -= value;
balanceOf[to] += value;
emit Transfer(msg.sender, to, value);
return true;
}Week 7 · Blockchain
Approve And transferFrom
- You approve a spender for an amount.
- The spender calls transferFrom to move tokens.
- The token contract reduces the allowance.
- This pattern drives most DeFi.
// 1. you: token.approve(market, 100)
// 2. market: token.transferFrom(you, shop, 100)
// allowance[you][market] goes 100 -> 0
Non-fungible
ERC-721: Unique Tokens
- Each token has a unique id.
- ownerOf(tokenId) returns exactly one owner.
- tokenURI points to JSON metadata.
- Images are usually stored off chain.
Week 7 · Blockchain
ERC-1155: Many Types, One Contract
- Each id is a token type.
- Balances map account and id to a number.
- Batch transfers save gas.
- Fits games, tickets and mixed collections.
// id 1: gold coin (supply 1,000,000)
// id 2: sword (supply 500)
// id 3: royal crown (supply 1)
Week 7 · Blockchain
Choosing A Standard
- Identical units: ERC-20.
- One-of-a-kind items: ERC-721.
- Mixed or many types: ERC-1155.
- Start from the use case, not the trend.
| Need | Standard |
|---|
| Interchangeable units | ERC-20 |
| One of a kind items | ERC-721 |
| Many types in one contract | ERC-1155 |
Design
Token Design Questions
- Purpose: what does holding it do?
- Supply: fixed, mintable or burnable?
- Distribution: who gets what, and when?
- Powers: who can pause, mint or upgrade?
Week 7 · Blockchain
Build It Safely With OpenZeppelin
- Inherit from audited library code.
- Keep your own code small.
- Test failing cases, not only success.
- Deploy to Sepolia before anything real.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract ClassToken is ERC20 {
constructor(uint256 initial) ERC20("ClassToken", "CLS") {
_mint(msg.sender, initial * 10**decimals());
}
}Week 7 · Blockchain
Recap
- A token is a balance table in a contract.
- ERC-20, 721 and 1155 cover most needs.
- Approve plus transferFrom lets contracts move tokens.
- Design the purpose, supply and powers first.
Week 7 · Blockchain
Next Week: DeFi
- Automated market makers swap tokens without an order book.
- Lending pools, stablecoins and oracles.
- Composability and its risks.
Week 7 · Blockchain
Questions And Exercise
- Deploy ClassToken on Sepolia.
- Send tokens to a second address.
- Find the Transfer event on the explorer.