ERC-20 · Final · ERC · 2015-11-19
ERC-20: Token Standard
The standard interface every fungible token on Ethereum implements, so wallets, exchanges and contracts can handle any token with the same six functions.
What problem does it solve?
In 2015 every project that issued a token wrote its own contract with its own function names. A wallet or exchange had to integrate each token by hand, and a smart contract could not accept "any token" as payment.
ERC-20 fixes this by defining one small interface. If a contract implements it, every tool that already speaks ERC-20 works with it on day one. Virtually all fungible tokens on Ethereum and EVM chains (USDC, DAI, UNI, wrapped ETH…) are ERC-20.
How it works
A token contract keeps a balance per address and exposes:
| Function | Purpose |
|---|---|
totalSupply() |
Total number of tokens in existence |
balanceOf(owner) |
Balance of one address |
transfer(to, value) |
Move tokens from the caller to to |
approve(spender, value) |
Allow spender to withdraw up to value from the caller |
allowance(owner, spender) |
How much spender may still withdraw |
transferFrom(from, to, value) |
Move tokens using a previously granted allowance |
Two events, Transfer and Approval, let indexers and wallets follow balances without reading storage. name(), symbol() and decimals() are optional metadata; decimals is usually 18.
The approve / transferFrom pair is what lets a DEX or lending protocol pull tokens from your account: you approve the protocol once, then it calls transferFrom when needed.
Sample code
Minimal interface and a tiny token (no access control, for illustration only):
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IERC20 {
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
function totalSupply() external view returns (uint256);
function balanceOf(address owner) external view returns (uint256);
function transfer(address to, uint256 value) external returns (bool);
function approve(address spender, uint256 value) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function transferFrom(address from, address to, uint256 value) external returns (bool);
}
contract MiniToken is IERC20 {
string public name = "Mini";
string public symbol = "MINI";
uint8 public decimals = 18;
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
constructor(uint256 supply) {
totalSupply = supply;
balanceOf[msg.sender] = supply;
emit Transfer(address(0), msg.sender, supply);
}
function transfer(address to, uint256 value) external returns (bool) {
return _move(msg.sender, to, value);
}
function approve(address spender, uint256 value) external returns (bool) {
allowance[msg.sender][spender] = value;
emit Approval(msg.sender, spender, value);
return true;
}
function transferFrom(address from, address to, uint256 value) external returns (bool) {
uint256 allowed = allowance[from][msg.sender];
require(allowed >= value, "allowance");
if (allowed != type(uint256).max) allowance[from][msg.sender] = allowed - value;
return _move(from, to, value);
}
function _move(address from, address to, uint256 value) internal returns (bool) {
require(balanceOf[from] >= value, "balance");
balanceOf[from] -= value;
balanceOf[to] += value;
emit Transfer(from, to, value);
return true;
}
}
Reading a balance from JavaScript (ethers v6):
import { Contract, JsonRpcProvider, formatUnits } from "ethers";
const provider = new JsonRpcProvider("https://eth.llamarpc.com");
const usdc = new Contract(
"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
["function balanceOf(address) view returns (uint256)", "function decimals() view returns (uint8)"],
provider,
);
const [raw, decimals] = await Promise.all([usdc.balanceOf(someAddress), usdc.decimals()]);
console.log(formatUnits(raw, decimals)); // "1234.56"
Things to know
- Some old tokens (notably USDT) do not return
boolfromtransfer. Use a wrapper such as OpenZeppelin'sSafeERC20instead of calling tokens directly. - Changing an allowance from one non-zero value to another has a known race condition. Common practice is to set it to 0 first, or use
increaseAllowancestyle helpers. - "Infinite approvals" (
type(uint256).max) are convenient but give a protocol unlimited access to that token. Gasless alternatives such as ERC-2612permitand ERC-7758 avoid the allowance pattern altogether. - In production, start from an audited implementation (OpenZeppelin
ERC20) rather than the sample above.