Introduction
In the world of Ethereum, tokens represent a powerful concept. They can digitally represent virtually anything of value. This can include points in a loyalty program, in-game assets for a character, financial instruments like company shares, or even a representation of a physical asset like gold.
To manage such a diverse and powerful capability, a robust and universal standard is essential. This is where the ERC-20 standard comes into play. It provides a common set of rules that allow developers to create token applications that are interoperable across a wide ecosystem of wallets, exchanges, and other decentralized applications (dApps).
What is ERC-20?
ERC-20 is a technical standard used for creating and issuing smart contracts on the Ethereum blockchain. It defines a common list of rules that all Ethereum tokens must adhere to. This includes how the tokens are transferred between addresses, how data within each token is accessed, and how users can approve certain transactions.
Crucially, ERC-20 governs fungible tokens. Fungibility means that each individual token is identical to every other token in its type and value. One ERC-20 token is always equal to another; they are interchangeable, much like how one US dollar bill has the same value as any other US dollar bill. This is a fundamental property that enables their use as a uniform currency or asset within the Ethereum ecosystem.
👉 Explore the technical specifications of token standards
Prerequisites
To fully understand the ERC-20 standard, a basic knowledge of core Ethereum concepts is helpful:
- Accounts: Understanding the difference between Externally Owned Accounts (EOAs) and Contract Accounts.
- Smart Contracts: Knowing how self-executing contracts with the terms of the agreement directly written into code function on the blockchain.
- Token Standards: A general awareness of why standards like ERC-20 are necessary for interoperability.
Core Functionality of ERC-20
Proposed by Fabian Vogelsteller in November 2015, ERC-20 (Ethereum Request for Comments 20) implements a standard API for tokens within smart contracts. This standardization ensures that different tokens can interact with each other and with various dApps seamlessly.
A smart contract that implements the following mandatory methods and events can be called an ERC-20 token contract. Once deployed, this contract is responsible for tracking the creation and movement of all its tokens on the Ethereum network.
Mandatory Methods
According to the official EIP-20 specification, an ERC-20 contract must implement these core methods:
name(): Returns the name of the token (e.g., "MyToken").symbol(): Returns the symbol of the token (e.g., "MTK").decimals(): Returns the number of decimals the token uses (e.g., 18). This defines how divisible the token can be.totalSupply(): Returns the total token supply currently in existence.balanceOf(address _owner): Returns the account balance of another account with address_owner.transfer(address _to, uint256 _value): Transfers_valueamount of tokens to address_to.approve(address _spender, uint256 _value): Allows_spenderto withdraw from your account multiple times, up to the_valueamount.transferFrom(address _from, address _to, uint256 _value): Transfers_valueamount of tokens from address_fromto address_to.allowance(address _owner, address _spender): Returns the amount which_spenderis still allowed to withdraw from_owner.
Mandatory Events
Events are logged and stored on the blockchain, providing a transparent history of key actions.
Transfer(address indexed _from, address indexed _to, uint256 _value): MUST be triggered when tokens are transferred, including zero-value transfers.Approval(address indexed _owner, address indexed _spender, uint256 _value): MUST be triggered on any successful call toapprove(...).
A Practical Example: Interacting with ERC-20 Tokens
The power of a standard is that it makes inspection and interaction with any compliant token simple. We only need the contract's Application Binary Interface (ABI) to create an interface. Below is a simplified Python example using the Web3.py library.
This code connects to the Ethereum network, defines the contract addresses for two popular tokens (DAI and WETH), and uses a simplified ABI to query basic information about them, such as their symbol, total supply, and the balance of a specific address.
from web3 import Web3
w3 = Web3(Web3.HTTPProvider("https://cloudflare-eth.com"))
dai_token_addr = "0x6B175474E89094C44Da98b954EedeAC495271d0F"
weth_token_addr = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
acc_address = "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11"
simplified_abi = [
{
'inputs': [{'internalType': 'address', 'name': 'account', 'type': 'address'}],
'name': 'balanceOf',
'outputs': [{'internalType': 'uint256', 'name': '', 'type': 'uint256'}],
'stateMutability': 'view', 'type': 'function', 'constant': True
},
{
'inputs': [],
'name': 'decimals',
'outputs': [{'internalType': 'uint8', 'name': '', 'type': 'uint8'}],
'stateMutability': 'view', 'type': 'function', 'constant': True
},
{
'inputs': [],
'name': 'symbol',
'outputs': [{'internalType': 'string', 'name': '', 'type': 'string'}],
'stateMutability': 'view', 'type': 'function', 'constant': True
},
{
'inputs': [],
'name': 'totalSupply',
'outputs': [{'internalType': 'uint256', 'name': '', 'type': 'uint256'}],
'stateMutability': 'view', 'type': 'function', 'constant': True
}
]
# Interact with DAI Contract
dai_contract = w3.eth.contract(address=w3.to_checksum_address(dai_token_addr), abi=simplified_abi)
symbol = dai_contract.functions.symbol().call()
decimals = dai_contract.functions.decimals().call()
totalSupply = dai_contract.functions.totalSupply().call() / 10**decimals
addr_balance = dai_contract.functions.balanceOf(acc_address).call() / 10**decimals
print("===== %s =====" % symbol)
print("Total Supply:", totalSupply)
print("Addr Balance:", addr_balance)
# Interact with WETH Contract
weth_contract = w3.eth.contract(address=w3.to_checksum_address(weth_token_addr), abi=simplified_abi)
symbol = weth_contract.functions.symbol().call()
decimals = weth_contract.functions.decimals().call()
totalSupply = weth_contract.functions.totalSupply().call() / 10**decimals
addr_balance = weth_contract.functions.balanceOf(acc_address).call() / 10**decimals
print("===== %s =====" % symbol)
print("Total Supply:", totalSupply)
print("Addr Balance:", addr_balance)Known Limitations and the ERC-20 Token Problem
Despite its widespread adoption, the ERC-20 standard has a notable flaw: the lack of a token reception handling mechanism.
The Problem
When ERC-20 tokens are sent to a smart contract address that is not explicitly designed to handle them, the tokens can become permanently stuck and lost. This occurs because of the standard's design:
- Transfer Mechanism: Tokens are transferred using the
transferortransferFromfunctions. These functions execute the transfer regardless of the recipient's capability to handle them. - No Notification: The receiving contract does not receive a notification or callback that tokens have arrived. If the contract lacks a specific function to manage incoming tokens, it remains unaware of the deposit.
- No Built-In Handling: The ERC-20 standard does not mandate a function for contracts to implement for receiving tokens, leading to inconsistent support.
This issue has resulted in the permanent loss of significant value over time. It primarily manifests when users accidentally send tokens to a contract address instead of a personal wallet address. 👉 Learn about advanced token standards designed to solve these issues
Evolving Solutions
This critical shortcoming led to the proposal of newer, more robust token standards that include a notification mechanism, such as:
- ERC-223: Aims to solve the problem by notifying the recipient contract and allowing it to accept or reject the transaction.
- ERC-777: Provides more advanced features, including operator permissions and hooks that allow recipient contracts to react to incoming transactions.
Frequently Asked Questions
What does ERC stand for?
ERC stands for Ethereum Request for Comments. It is a formal process used in the Ethereum community to propose and debate new standards and improvements, similar to the Internet's RFC process.
Are all Ethereum tokens ERC-20?
No, not all tokens on Ethereum are ERC-20. While ERC-20 is the most common standard for fungible tokens, many other standards exist for different purposes, such as ERC-721 for non-fungible tokens (NFTs) and ERC-1155 for multi-tokens.
What is the difference between ETH and an ERC-20 token?
ETH is the native cryptocurrency of the Ethereum blockchain. It is used to pay for transaction fees (gas). ERC-20 tokens are assets created on top of the Ethereum blockchain via smart contracts. They rely on the Ethereum network to function but are separate from its native currency.
Can a lost ERC-20 token be recovered?
Generally, no. If tokens are sent to an address for which no one has the private key (like a burn address) or to a contract that cannot handle them, they are typically irrecoverable. This highlights the importance of always double-checking addresses before sending.
What is the purpose of the approve and transferFrom functions?
These functions enable delegated transfers. Instead of you having to initiate every transaction, you can approve a third-party service (like a decentralized exchange) to spend a specific amount of your tokens. That service can then use transferFrom to execute trades or transfers on your behalf, up to the approved limit, without needing your private key for each action.
What are some popular ERC-20 tokens?
Many of the most well-known cryptocurrencies besides ETH are actually ERC-20 tokens on the Ethereum network. This includes stablecoins like USDC (USD Coin) and DAI, as well as other major assets like Chainlink (LINK) and Uniswap (UNI).
Further Reading and Resources
- The original EIP-20: ERC-20 Token Standard proposal.
- OpenZeppelin's battle-tested and audited ERC-20 implementation, which is the industry standard for secure development.
- OpenZeppelin's guides on Tokens and ERC-20.
Other Fungible Token Standards
The ecosystem continues to evolve beyond ERC-20 to address its limitations and enable new functionalities.
- ERC-223: Designed to prevent the accidental loss of tokens sent to contracts.
- ERC-777: Offers more advanced features and improved user experience over ERC-20.
- ERC-4626: A standard for tokenized vaults that yield interest, simplifying their integration across the ecosystem.