ERC-20 Interface
ERC-20 is the standard interface for implementing fungible tokens on the Ethereum network. IERC20 defines the required functions and events that compliant contracts must implement. This interface is provided by OpenZeppelin, a widely-used, secure smart contract library.
Events
Transfer
Transfer event Transfer(address indexed from, address indexed to, uint256 value);- Triggered when tokens are transferred from
fromtoto. valueindicates the number of tokens transferred.valuecan be zero.
Approval
Approval event Approval(address indexed owner, address indexed spender, uint256 value);- Triggered when
ownersets an allowance forspender. valueis the new approved amount.
Functions
totalSupply
totalSupply function totalSupply() external view returns (uint256);- Returns the total number of tokens in existence.
balanceOf
balanceOf function balanceOf(address account) external view returns (uint256);- Returns the number of tokens owned by
account.
transfer
transfer function transfer(address to, uint256 value) external returns (bool);- Transfers
valuetokens from the caller to addressto. - Returns
trueif successful. - Emits a
Transferevent.
allowance
allowance function allowance(address owner, address spender) external view returns (uint256);- Returns the remaining number of tokens that
spenderis allowed to spend on behalf ofowner.
approve
approve function approve(address spender, uint256 value) external returns (bool);- Sets
valueas the allowance ofspenderover the caller's tokens. - Returns
trueif successful. - Emits an
Approvalevent.
🔒 Note: Modifying allowances via approve() may lead to race conditions. A safer pattern is:
approve(spender, 0);
approve(spender, newValue);transferFrom
transferFromfunction transferFrom(address from, address to, uint256 value) external returns (bool);- Transfers
valuetokens fromfromtotousing the allowance mechanism. - The caller must have an allowance approved by
from. - Returns
trueif successful. - Emits a
Transferevent.
References
- Based on OpenZeppelin v5.3
- EIP-20 Specification: https://eips.ethereum.org/EIPS/eip-20
- Recommended implementation: OpenZeppelin ERC20
This document provides a quick reference to the ERC-20 interface for developers implementing or interacting with ERC-20 tokens. For full functionality and customization, refer to actual implementation contracts.
Updated 26 days ago