ERC20 is the Ethereum token standard used for Ethereum smart contracts. Developed in 2015, ERC-20 defines a common set of rules that Ethereum tokens must follow. This standardization allows developers to program how new tokens function within the Ethereum ecosystem. The protocol gained popularity through crowdfunding initiatives like Initial Coin Offerings (ICOs).
The ERC20 Token Standard Interface
The ERC20 standard outlines required functions and events for Ethereum token contracts. Below is an interface contract illustrating these components:
contract ERC20Interface {
function totalSupply() public constant returns (uint);
function balanceOf(address tokenOwner) public constant returns (uint balance);
function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
function transfer(address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success);
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}Most major Ethereum tokens are ERC-20 compliant. However, some tokens like Golem Network Token (GNT) partially comply, omitting functions like approve, allowance, and transferFrom.
How ERC-20 Token Contracts Work
Token contracts manage balances using mappings:
contract TokenContractFragment {
mapping(address => uint256) balances;
mapping(address => mapping (address => uint256)) allowed;
function transfer(address to, uint tokens) public returns (bool success) {
balances[msg.sender] -= tokens;
balances[to] += tokens;
emit Transfer(msg.sender, to, tokens);
return true;
}
}Key Operations:
- Balance Tracking: Stores token balances per account.
- Transfers: Adjusts balances between accounts via
transfer. - Approvals: Authorizes third-party transfers using
approveandtransferFrom.
Sample Fixed-Supply Token Contract
Below is a complete ERC20 contract with a fixed supply of 1,000,000 tokens (18 decimals):
pragma solidity ^0.6.12;
contract FixedSupplyToken {
string public symbol = "FIXED";
string public name = "Example Fixed Supply Token";
uint8 public decimals = 18;
uint _totalSupply = 1000000 * 10**uint(decimals);
mapping(address => uint) balances;
mapping(address => mapping(address => uint)) allowed;
constructor() public {
balances[msg.sender] = _totalSupply;
}
function transfer(address to, uint tokens) public returns (bool success) {
balances[msg.sender] -= tokens;
balances[to] += tokens;
emit Transfer(msg.sender, to, tokens);
return true;
}
}FAQs
What is ERC20?
ERC20 is a technical standard for Ethereum tokens, ensuring interoperability across wallets and exchanges.
Why use ERC20?
It simplifies token creation and integration with existing Ethereum infrastructure like MetaMask and MyEtherWallet.
Can ERC20 tokens have variable supply?
Yes. While the example above uses a fixed supply, ERC20 supports dynamic minting/burning logic.
What are ERC20’s limitations?
- No native support for privacy features.
- Cannot handle non-fungible tokens (NFTs; use ERC721 instead).
Further Reading
- Ethereum Tokens – Official Ethereum documentation.
- Token Deployment Guide – Step-by-step token creation tutorial.
- OpenZeppelin ERC20 Library – Secure, audited token templates.