Understanding ERC20 Tokens
An ERC20 token is any smart contract that adheres to the ERC20 standard—a set of rules enabling interoperability between tokens on the Ethereum blockchain.
Key Features of ERC20 Tokens:
- Token Transfers: Move tokens between addresses.
- Delegated Transfers: Allow third parties (e.g., smart contracts) to transfer tokens on behalf of holders.
👉 Learn more about Ethereum standards
ERC20 Standard Implementation
Core Interface (IERC20.sol)
Below is the standard interface defining mandatory functions and events:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
interface IERC20 {
function totalSupply() external view returns (uint);
function balanceOf(address account) external view returns (uint);
function transfer(address recipient, uint amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint);
function approve(address spender, uint amount) external returns (bool);
function transferFrom(address sender, address recipient, uint amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint value);
event Approval(address indexed owner, address indexed spender, uint value);
}Sample ERC20 Token Contract
A minimal implementation includes:
- State Variables: Track balances, allowances, and supply.
- Core Functions: Enable transfers, approvals, and minting/burning.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
import "./IERC20.sol";
contract ERC20 is IERC20 {
uint public totalSupply;
mapping(address => uint) public balanceOf;
mapping(address => mapping(address => uint)) public allowance;
string public name = "ExampleToken";
string public symbol = "ETK";
uint8 public decimals = 18;
function transfer(address recipient, uint amount) external returns (bool) {
balanceOf[msg.sender] -= amount;
balanceOf[recipient] += amount;
emit Transfer(msg.sender, recipient, amount);
return true;
}
function approve(address spender, uint amount) external returns (bool) {
allowance[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}
function transferFrom(address sender, address recipient, uint amount) external returns (bool) {
allowance[sender][msg.sender] -= amount;
balanceOf[sender] -= amount;
balanceOf[recipient] += amount;
emit Transfer(sender, recipient, amount);
return true;
}
function mint(uint amount) external {
balanceOf[msg.sender] += amount;
totalSupply += amount;
emit Transfer(address(0), msg.sender, amount);
}
function burn(uint amount) external {
balanceOf[msg.sender] -= amount;
totalSupply -= amount;
emit Transfer(msg.sender, address(0), amount);
}
}Best Practices for Creating ERC20 Tokens
Using OpenZeppelin Libraries
OpenZeppelin provides secure, audited implementations of ERC20. Example:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.0.0/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20 {
constructor(string memory name, string memory symbol) ERC20(name, symbol) {
_mint(msg.sender, 100 * 10**uint(decimals()));
}
}Key Steps:
- Inherit from
ERC20.sol. - Mint tokens in the constructor (adjustable supply).
Advanced Use Case: Token Swap Contract
Create a decentralized exchange (like Uniswap) to swap tokens:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.0.0/contracts/token/ERC20/IERC20.sol";
contract TokenSwap {
IERC20 public token1;
address public owner1;
uint public amount1;
IERC20 public token2;
address public owner2;
uint public amount2;
constructor(
address _token1,
address _owner1,
uint _amount1,
address _token2,
address _owner2,
uint _amount2
) {
token1 = IERC20(_token1);
owner1 = _owner1;
amount1 = _amount1;
token2 = IERC20(_token2);
owner2 = _owner2;
amount2 = _amount2;
}
function swap() public {
require(msg.sender == owner1 || msg.sender == owner2, "Not authorized");
require(token1.allowance(owner1, address(this)) >= amount1, "Token 1 allowance too low");
require(token2.allowance(owner2, address(this)) >= amount2, "Token 2 allowance too low");
_safeTransferFrom(token1, owner1, owner2, amount1);
_safeTransferFrom(token2, owner2, owner1, amount2);
}
function _safeTransferFrom(IERC20 token, address sender, address recipient, uint amount) private {
bool sent = token.transferFrom(sender, recipient, amount);
require(sent, "Token transfer failed");
}
}FAQ Section
1. What is the ERC20 standard?
ERC20 is a technical standard for fungible tokens on Ethereum, ensuring compatibility across wallets and exchanges.
2. How do I mint ERC20 tokens?
Use the mint function in your contract (if available) or deploy with OpenZeppelin’s _mint.
3. Can I customize token properties like name and decimals?
Yes! Update the name, symbol, and decimals variables in your contract.
4. What’s the difference between transfer and transferFrom?
transfer: Moves tokens from the caller’s address.transferFrom: Moves tokens approved by another address (requiresapprovefirst).
5. Is OpenZeppelin necessary for ERC20?
No, but it simplifies development and enhances security.
6. How do token swaps work?
Swaps use transferFrom to exchange tokens between parties after mutual approvals.