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
from
toto
. value
indicates the number of tokens transferred.value
can be zero.
Approval
Approval
event Approval(address indexed owner, address indexed spender, uint256 value);
- Triggered when
owner
sets an allowance forspender
. value
is 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
value
tokens from the caller to addressto
. - Returns
true
if successful. - Emits a
Transfer
event.
allowance
allowance
function allowance(address owner, address spender) external view returns (uint256);
- Returns the remaining number of tokens that
spender
is allowed to spend on behalf ofowner
.
approve
approve
function approve(address spender, uint256 value) external returns (bool);
- Sets
value
as the allowance ofspender
over the caller's tokens. - Returns
true
if successful. - Emits an
Approval
event.
🔒 Note: Modifying allowances via approve()
may lead to race conditions. A safer pattern is:
approve(spender, 0);
approve(spender, newValue);
transferFrom
transferFrom
function transferFrom(address from, address to, uint256 value) external returns (bool);
- Transfers
value
tokens fromfrom
toto
using the allowance mechanism. - The caller must have an allowance approved by
from
. - Returns
true
if successful. - Emits a
Transfer
event.
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 about 1 month ago