0
0
BlockchainConceptBeginner · 3 min read

What is an ERC20 Token: Simple Explanation and Example

An ERC20 token is a standard type of digital token on the Ethereum blockchain that follows a set of rules for how tokens behave. It defines how tokens can be transferred, how to check balances, and how to approve spending, making tokens compatible with wallets and exchanges.
⚙️

How It Works

Think of an ERC20 token like a digital version of a gift card or points system that works on Ethereum. It follows a fixed set of rules so that any app or wallet knows how to handle it. This is like how all USB devices follow the same rules to connect to your computer.

The rules include how to check how many tokens someone has, how to send tokens to others, and how to allow someone else to spend tokens on your behalf. This makes it easy for different programs to work together without confusion.

💻

Example

This example shows a simple ERC20 token contract in Solidity. It creates a token with a fixed supply and allows users to transfer tokens.

solidity
pragma solidity ^0.8.0;

interface IERC20 {
    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function transfer(address recipient, uint256 amount) external returns (bool);
    event Transfer(address indexed from, address indexed to, uint256 value);
}

contract SimpleToken is IERC20 {
    string public name = "SimpleToken";
    string public symbol = "STK";
    uint8 public decimals = 18;
    uint256 private _totalSupply;
    mapping(address => uint256) private _balances;

    constructor(uint256 initialSupply) {
        _totalSupply = initialSupply * 10 ** uint256(decimals);
        _balances[msg.sender] = _totalSupply;
        emit Transfer(address(0), msg.sender, _totalSupply);
    }

    function totalSupply() external view override returns (uint256) {
        return _totalSupply;
    }

    function balanceOf(address account) external view override returns (uint256) {
        return _balances[account];
    }

    function transfer(address recipient, uint256 amount) external override returns (bool) {
        require(_balances[msg.sender] >= amount, "Not enough tokens");
        _balances[msg.sender] -= amount;
        _balances[recipient] += amount;
        emit Transfer(msg.sender, recipient, amount);
        return true;
    }
}
🎯

When to Use

Use an ERC20 token when you want to create your own digital currency or points system on Ethereum. It is perfect for things like reward points, voting tokens, or new cryptocurrencies.

Because ERC20 tokens follow a common standard, they work easily with wallets, exchanges, and other smart contracts. This makes them a popular choice for projects that want to be compatible with the Ethereum ecosystem.

Key Points

  • Standardized: ERC20 defines a common set of rules for tokens.
  • Interoperable: Works with wallets and exchanges easily.
  • Functions: Includes transfer, balance check, and approval.
  • Events: Emits events to track token transfers.
  • Widely Used: Most Ethereum tokens follow ERC20.

Key Takeaways

ERC20 tokens follow a standard set of rules for creating digital tokens on Ethereum.
They allow easy transfer, balance checking, and approval of tokens.
ERC20 tokens work smoothly with wallets, exchanges, and other smart contracts.
Use ERC20 when creating your own cryptocurrency or digital points system.
The standard ensures compatibility and wide adoption in the Ethereum ecosystem.