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:
- Name and symbol (e.g., "BitCoin" and "BTC").
- Total supply (fixed or adjustable).
- Core functions like
transfer,approve, andallowance.
Advantages:
- Compatibility with Ethereum wallets.
- Simplified token creation (over 180,000 ERC-20 tokens exist).
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:
totalSupply(): Returns the token’s circulating supply.balanceOf(): Checks an address’s token balance.transfer(): Moves tokens from the caller to another address.approve(): Authorizes a spender to use tokens on your behalf.transferFrom(): Allows a spender to transfer tokens (after approval).
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);
}name(): Token name (e.g., "HarryToken").symbol(): Token symbol (e.g., "HYT").decimals(): Decimal precision (typically 18).
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;
}- Checks: Valid addresses, sufficient balance.
- Process: Deducts
amountfrom sender, adds to recipient, emitsTransferevent.
2. approve() & allowance()
function approve(address spender, uint256 amount) public override returns (bool) {
_approve(msg.sender, spender, amount);
return true;
}- Usage: Authorizes
spenderto useamountof your tokens.
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;
}- Requires: Prior approval from
from.
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);
}- Note: These are internal; expose via public functions if needed.
FAQ
1. What’s the difference between transfer() and transferFrom()?
transfer(): Moves caller’s tokens.transferFrom(): Moves tokens from an approved address (e.g., for decentralized exchanges).
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:
- Standard compliance ensures interoperability.
- Security checks (e.g., zero-address validation) are critical.
- Events like
TransferandApprovalenable off-chain tracking.
👉 Explore advanced topics like ERC721 (NFTs) here.
Next Up: Building NFTs with ERC721!
This article meets SEO standards with:
- Hierarchical headings (
#to######). - Keyword integration (ERC20, token standard, Solidity).
- Engaging anchor texts and FAQ section.
- No ads or sensitive content.