Introduction to ERC-20 Tokens
The ERC-20 token standard defines a common interface for Ethereum smart contracts to represent tokens on the Ethereum blockchain. This standardization offers significant benefits for the development of blockchain-based applications by ensuring interoperability across various platforms.
Key Applications of ERC-20 Tokens
- Wallets: All ERC-20 compliant tokens can be managed within a single wallet (e.g., MyEtherWallet, MetaMask).
- Exchanges: Tokens can be traded on any exchange that supports the ERC-20 standard (e.g., Uniswap, Binance).
- Block Explorers: Token balances and transfers can be tracked using tools like Etherscan or Ethplorer.
function transfer(address _to, uint256 _value) returns (bool success)How Tokens Are Represented
Tokens are tracked using a simple balance sheet to record ownership and transfers. Below is an example of a token balance sheet for three holders (A, B, C) with a total supply of 10,000 tokens:
| Owner | Token Balance |
|---|---|
| A | 640 |
| B | 50 |
| C | 100 |
| ... | ... |
| Total | 10,000 |
Example Token Transfer
If A transfers 40 tokens to B, the balance sheet updates as follows:
| Owner | Token Balance |
|---|---|
| A | 600 |
| B | 90 |
| C | 100 |
| ... | ... |
| Total | 10,000 |
Core Attributes of an ERC-20 Token
A basic ERC-20 token contract includes four key attributes:
- Name: A descriptive string (e.g., "Example Token").
- Symbol: A short identifier (e.g., "EXP").
- Decimals: Defines token divisibility (e.g., 18 decimal places).
- Total Supply: The total number of tokens in circulation, including fractions.
Example Smart Contract
Below is a simplified Solidity contract implementing an ERC-20 token:
contract BasicERC20TokenExample {
string public constant name = "Example Token";
string public constant symbol = "EXP";
uint8 public constant decimals = 18;
uint256 public constant totalSupply = 10000 * (10 ** uint256(decimals));
mapping(address => uint256) balances;
function BasicERC20TokenExample() public {
balances[msg.sender] = totalSupply;
}
function transfer(address _to, uint256 _value) public returns (bool) {
require(_to != address(0));
require(_value <= balances[msg.sender]);
balances[msg.sender] -= _value;
balances[_to] += _value;
return true;
}
function balanceOf(address _owner) public view returns (uint256 balance) {
return balances[_owner];
}
}Advanced ERC-20 Functions
Allowances and Delegated Transfers
ERC-20 tokens support allowances, enabling third parties (e.g., exchanges) to transfer tokens on behalf of holders.
Key Functions:
approve: Grants a spender the right to transfer tokens.function approve(address _spender, uint256 _value) returns (bool success)transferFrom: Allows the spender to transfer tokens.function transferFrom(address _from, address _to, uint256 _value) returns (bool success)allowance: Checks the remaining allowance for a spender.function allowance(address _owner, address _spender) constant returns (uint256 remaining)
Security Considerations
- Events: ERC-20 contracts emit
TransferandApprovalevents for tracking. - Approval Attack: Changing allowances atomically can prevent front-running attacks.
👉 Learn more about Ethereum smart contracts
FAQ
What is the ERC-20 standard?
The ERC-20 standard defines a set of rules for creating fungible tokens on Ethereum.
Why are ERC-20 tokens important?
They ensure compatibility across wallets, exchanges, and dApps.
How do I create an ERC-20 token?
Deploy a smart contract with the required functions (e.g., transfer, balanceOf).
What are token decimals?
Decimals determine the smallest divisible unit of a token (e.g., 18 decimals = 1e18 units per token).
Can ERC-20 tokens be swapped?
Yes, decentralized exchanges (DEXs) like Uniswap support ERC-20 token swaps.