0
0
Blockchain / Solidityprogramming~7 mins

ERC-20 fungible token standard in Blockchain / Solidity

Choose your learning style9 modes available
Introduction

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.

When creating a new digital currency that can be traded or used in apps.
When you want your token to work with popular wallets and exchanges without extra work.
When building a game or app that needs tokens to represent points, assets, or rewards.
When you want to make sure your token can be sent, received, and tracked like others on Ethereum.
Syntax
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.

Examples
Returns the total number of tokens that exist.
Blockchain / Solidity
function totalSupply() external view returns (uint256);
Returns how many tokens a specific address owns.
Blockchain / Solidity
function balanceOf(address account) external view returns (uint256);
Sends tokens from the caller to another address.
Blockchain / Solidity
function transfer(address to, uint256 amount) external returns (bool);
Event emitted when tokens move from one address to another.
Blockchain / Solidity
event Transfer(address indexed from, address indexed to, uint256 value);
Sample Program

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.

Blockchain / Solidity
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;
    }
}
OutputSuccess
Important Notes

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.

Summary

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.