Sub topic 2
Token standards: how ERC-20 tracks balances
Prerequisites and the ERC-20 model
This chapter assumes you understand wallets, blockchains, and smart contracts. You should also be comfortable reading short Solidity snippets.
ERC-20 is a standard interface for fungible tokens on Ethereum-compatible chains. Fungible means that one unit is interchangeable with another unit of the same token. The standard does not create a separate blockchain asset. A smart contract stores balances and exposes functions that wallets, exchanges, and other contracts know how to call.
The core state is a mapping:
mapping(address => uint256) public balanceOf;A mapping associates each wallet address with an integer balance. The contract also stores the total supply and, usually, an allowance for delegated spending.
| Component | Purpose |
|---|---|
| `balanceOf` | Records each address's token balance |
| `totalSupply` | Records the number of token base units in existence |
| `allowance` | Records how much one address may spend for another |
| Events | Publish transfers and approvals for applications to index |
Transfers and allowances
A direct transfer moves units from the caller's balance to a recipient's balance. The contract emits a Transfer event so wallets and block explorers can update their displays.
Delegated transfers use two steps:
- The owner calls
approve(spender, amount). - The approved spender calls
transferFrom(owner, recipient, amount).
This pattern lets a decentralized exchange spend tokens after the user approves it. The allowance is separate from the owner's balance. A spender can use only the smaller of the remaining allowance and the owner's current balance.
The following contract demonstrates the basic accounting. It uses 18 decimal places, so an amount shown as 1.5 tokens is stored as 1.5 × 10^18 base units.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract SimpleERC20 {
string public name = "Example Token";
string public symbol = "EX";
uint8 public immutable decimals = 18;
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
constructor(uint256 wholeTokens) {
_mint(msg.sender, wholeTokens * 10 ** uint256(decimals));
}
function transfer(address to, uint256 amount) external returns (bool) {
_transfer(msg.sender, to, amount);
return true;
}
function approve(address spender, uint256 amount) external returns (bool) {
allowance[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}
function transferFrom(
address from,
address to,
uint256 amount
) external returns (bool) {
uint256 permitted = allowance[from][msg.sender];
require(permitted >= amount, "allowance exceeded");
if (permitted != type(uint256).max) {
allowance[from][msg.sender] = permitted - amount;
}
_transfer(from, to, amount);
return true;
}
function _mint(address to, uint256 amount) internal {
require(to != address(0), "zero recipient");
totalSupply += amount;
balanceOf[to] += amount;
emit Transfer(address(0), to, amount);
}
function _transfer(address from, address to, uint256 amount) internal {
require(to != address(0), "zero recipient");
require(balanceOf[from] >= amount, "insufficient balance");
balanceOf[from] -= amount;
balanceOf[to] += amount;
emit Transfer(from, to, amount);
}
}What the standard does not decide
ERC-20 specifies common function and event names. It does not decide whether a token has a fixed supply, who receives the initial allocation, or whether an administrator can mint, burn, pause, or freeze transfers. Those choices belong to the contract's design.
decimals affects display and user input. It does not change the underlying accounting precision or make a token more valuable. A token with six decimals can represent smaller or larger practical units through its chosen scale.
Common errors and safety checks
- Confusing base units with displayed token amounts can cause transfers that are too large or too small.
- Approving an unlimited allowance gives a spender continuing permission until the allowance changes.
- Replacing an existing allowance directly can create a race between the old and new approvals. Applications often set the allowance to zero first or use safer increase and decrease functions.
- A token can follow the ERC-20 interface while still having harmful owner controls, hidden fees, or transfer restrictions.
Exercise
A wallet has approved a decentralized exchange to spend 500 EX, but its balance is only 300 EX. How many EX can the exchange transfer immediately, and what happens to the allowance after that transfer?
Show answer
It can transfer at most 300 EX because the balance is the limiting value. If it transfers 300 EX, the allowance normally decreases from 500 EX to 200 EX.