What is ERC20?
ERC20 is a standard interface defined for “Fungible Tokens”, which are exchangeable tokens. It is one of the most common ERC standards used in applications such as cryptocurrencies, voting governance tokens, and staking tokens. When developers create applications on the Ethereum platform, they need to issue tokens to provide services like points and general currency. The ERC20 standard includes concepts on how to issue tokens, transfer tokens between parties, authorize third-party usage, etc. Developers can utilize ERC20 to issue tokens specific to their services.
ERC Standard
The ERC20 standard specifies the implementation of functions and events. It is crucial to note that when a function call fails, the caller must handle the failure exception and not assume that the function call will always succeed. For example, handling cases where transfers fail is essential.
Functions
name
Returns the name of the token. This function is solely for enhancing usability and ease of reference, making it non-essential. Therefore, it cannot be assumed that every ERC20 contract implements this functionality.
- Implementation:
function name() public view returns (string) {}
symbol
Returns the symbol of the token. Similar to the name function, this serves to enhance usability and ease of reference, making it non-essential. It cannot be assumed that every ERC20 contract includes this functionality.
- Implementation:
function symbol() public view returns (string) {}
decimal
Returns the position of the decimal point for the token. For example, if denoted as N, the quantity of the token will be considered as the basic unit only after dividing by (10**N), with values below N representing decimal points. This function is for enhancing usability and is non-essential; hence, not all ERC20 contracts are required to implement this feature.
- Implementation:
function decimals() public view returns (uint8) {}
totalSupply
Returns the total supply of the token.
- Implementation:
function totalSupply() public view returns (uint256) {}
balanceOf
Returns the token balance of the _owner
account.
- Implementation:
function balanceOf(address _owner) public view returns (uint256 balance) {}
transfer
Transfers _value
amount of tokens from the “function caller” to the recipient _to
. This function always triggers a Transfer
event. If the balance of the “function caller” is insufficient, an error should be thrown.
- Implementation:
function transfer(address _to, uint256 _value) public returns (bool success) {}
transferFrom
When there is an authorized limit, this function can be used to transfer tokens from another account. It transfers the _value
amount of tokens from the sender _from
to the recipient _to
, triggering the Transfer
event.
This function is triggered after authorization (approve) of a third-party contract or account, allowing them to utilize one’s limit.
- Implementation:
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {}
approve
The function caller authorizes the _spender
to utilize an allocated limit of _value
. If called repeatedly, the new limit will directly overwrite the old one.
- Implementation:
function approve(address _spender, uint256 _value) public returns (bool success) {}
allowance
Queries the remaining usage limit remaining that the authorizer _owner
has granted to the authorized _spender
.
- Implementation:
function allowance(address _owner, address _spender) public view returns (uint256 remaining) {}
Events
transfer
When a transfer of tokens occurs, the Transfer event must be triggered, regardless of the quantity of tokens being transferred. It records the transfer of the token quantity _value
from the sender’s address _from
to the receiver’s address _to
.
- Implementation:
event Transfer(address indexed _from, address indexed _to, uint256 _value) {}
approval
When the approve function is successfully called with approve(address _spender, uint256 _value)
, the Approval
event must be triggered. It records that the token owner _owner
authorizes the token quantity _value
to the authorized party _spender
.
- Implementation:
event Approval(address indexed _owner, address indexed _spender, uint256 _value) {}