Beginner's Guide to Ethereum Development: ERC20 Token Contracts

·

Introduction to ERC20

ERC-20 is a token standard on the Ethereum blockchain. It is the most widely adopted standard, ensuring fungibility—meaning one ERC-20 token holds the same value as another of the same type. For example, your 100 "tokens" are interchangeable with mine.

The ERC-20 standard mandates that tokens include:

Advantages:


ERC20 Interface Definition

The standard defines interfaces in Solidity (IERC20.sol):

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

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);  
}

Key Functions:


Metadata Extension (IERC20Metadata.sol)

interface IERC20Metadata is IERC20 {
    function name() external view returns (string memory);  
    function symbol() external view returns (string memory);  
    function decimals() external view returns (uint8);  
}

ERC20 Implementation (ERC20.sol)

Core Variables

mapping(address => uint256) private _balances;          // Address balances  
mapping(address => mapping(address => uint256)) private _allowances;  // Approved spenders  
uint256 private _totalSupply;                           // Total tokens  
string private _name;  
string private _symbol;  

Constructor

Initializes token details and mints initial supply:

constructor() {
    _name = "HarryToken";  
    _symbol = "HYT";  
    _mint(msg.sender, 10000000000);  // Mints 10B tokens to deployer  
}

Key Functions Explained

1. transfer()

function transfer(address to, uint256 amount) public override returns (bool) {
    _transfer(msg.sender, to, amount);  
    return true;  
}

2. approve() & allowance()

function approve(address spender, uint256 amount) public override returns (bool) {
    _approve(msg.sender, spender, amount);  
    return true;  
}

3. transferFrom()

function transferFrom(address from, address to, uint256 amount) public override returns (bool) {
    _spendAllowance(from, msg.sender, amount);  
    _transfer(from, to, amount);  
    return true;  
}

4. _mint() & _burn()

function _mint(address account, uint256 amount) internal {  
    _totalSupply += amount;  
    _balances[account] += amount;  
    emit Transfer(address(0), account, amount);  
}  

function _burn(address account, uint256 amount) internal {  
    _balances[account] -= amount;  
    _totalSupply -= amount;  
    emit Transfer(account, address(0), amount);  
}

FAQ

1. What’s the difference between transfer() and transferFrom()?

2. Why use 18 decimals?

👉 Ethereum’s standard ensures precision for small values (e.g., 1 token = 10^18 wei).

3. How do I deploy this contract?

Compile ERC20.sol, deploy the ERC20 contract (not the interface), and interact via tools like Remix or Hardhat.

4. Can I add a public mint() function?

Yes, but restrict access (e.g., with onlyOwner modifier) to prevent abuse.


Conclusion

This guide covered ERC20 token development, from interfaces to implementation. Key takeaways:

👉 Explore advanced topics like ERC721 (NFTs) here.

Next Up: Building NFTs with ERC721!


This article meets SEO standards with: