The ERC-20 standard defines a common way for tokens on the Ethereum blockchain to work. It makes sure all tokens behave the same, so wallets and apps can use them easily.
ERC-20 fungible token standard in Blockchain / Solidity
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address to, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address from, address to, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}This is an interface, meaning it only defines functions without code.
All ERC-20 tokens must implement these functions and events exactly.
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address to, uint256 amount) external returns (bool);event Transfer(address indexed from, address indexed to, uint256 value);This is a simple ERC-20 token contract. It creates tokens, lets users send tokens, approve others to spend tokens, and transfer tokens on behalf of others.
pragma solidity ^0.8.0; contract SimpleToken { string public name = "SimpleToken"; string public symbol = "SIM"; uint8 public decimals = 18; uint256 public totalSupply; mapping(address => uint256) public balanceOf; mapping(address => mapping(address => uint256)) public allowance; event Transfer(address indexed from, address indexed to, uint256 value); event Approval(address indexed owner, address indexed spender, uint256 value); constructor(uint256 initialSupply) { totalSupply = initialSupply * 10 ** decimals; balanceOf[msg.sender] = totalSupply; emit Transfer(address(0), msg.sender, totalSupply); } function transfer(address to, uint256 amount) public returns (bool) { require(balanceOf[msg.sender] >= amount, "Not enough tokens"); balanceOf[msg.sender] -= amount; balanceOf[to] += amount; emit Transfer(msg.sender, to, amount); return true; } function approve(address spender, uint256 amount) public returns (bool) { allowance[msg.sender][spender] = amount; emit Approval(msg.sender, spender, amount); return true; } function transferFrom(address from, address to, uint256 amount) public returns (bool) { require(balanceOf[from] >= amount, "Not enough tokens"); require(allowance[from][msg.sender] >= amount, "Allowance too low"); balanceOf[from] -= amount; balanceOf[to] += amount; allowance[from][msg.sender] -= amount; emit Transfer(from, to, amount); return true; } }
ERC-20 tokens must emit Transfer events on token moves so wallets can track balances.
Decimals define how divisible the token is; 18 decimals means tokens can be split into very small parts.
Always check for enough balance and allowance before transferring tokens.
ERC-20 is a standard that makes tokens work the same way on Ethereum.
It defines functions to check balances, send tokens, and approve spending.
Following ERC-20 helps your token work with wallets, exchanges, and apps easily.