ERC-20: Fungible Token Standard
ERC-20 is the token standard that powers USDC, DAI, UNI, LINK, and thousands more. Every DeFi protocol interacts with tokens through this 6-function interface. Building a production-grade ERC-20 is a foundational Web3 skill.
ERC-20 Complete Implementation
The ERC-20 standard (EIP-20) defines 6 required functions + 2 events:
Required functions:
- totalSupply() → uint256
- balanceOf(address) → uint256
- transfer(address to, uint256 amount) → bool
- allowance(address owner, address spender) → uint256
- approve(address spender, uint256 amount) → bool
- transferFrom(address from, address to, uint256 amount) → bool
Required events:
- Transfer(address indexed from, address indexed to, uint256 value)
- Approval(address indexed owner, address indexed spender, uint256 value)
The allowance/approve/transferFrom trio enables the "pull payment" pattern:
User approves a DEX/protocol to spend their tokens → DEX calls transferFrom to swapCommon Mistakes
- Not using SafeERC20 when calling other tokens — some tokens (USDT) do not return bool from transfer. Use OpenZeppelin's SafeERC20.safeTransfer() instead of direct calls.
- Forgetting decimals — ERC-20 uses uint256 internal representation. 1 USDC = 1_000_000 (6 decimals), 1 ETH = 1e18 (18 decimals). Always use parseUnits/formatUnits.
- Infinite approvals — approving uint256.max allows a contract to spend all your tokens forever. Use time-limited or amount-limited approvals for better security.
- Missing events — ERC-20 events are required by the standard. Wallets and block explorers depend on Transfer events to show token balances.
Tip
Tip
Practice ERC20 Fungible Token Standard in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Once deployed, smart contracts are immutable — code is law
Practice Task
Note
Practice Task — (1) Write a working example of ERC20 Fungible Token Standard from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with ERC20 Fungible Token Standard is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready web3 code.
Key Takeaways
- ERC-20 is the token standard that powers USDC, DAI, UNI, LINK, and thousands more.
- Not using SafeERC20 when calling other tokens — some tokens (USDT) do not return bool from transfer. Use OpenZeppelin's SafeERC20.safeTransfer() instead of direct calls.
- Forgetting decimals — ERC-20 uses uint256 internal representation. 1 USDC = 1_000_000 (6 decimals), 1 ETH = 1e18 (18 decimals). Always use parseUnits/formatUnits.
- Infinite approvals — approving uint256.max allows a contract to spend all your tokens forever. Use time-limited or amount-limited approvals for better security.