Unpacking ERC-20 Tokens: Understanding Their Functions and Their Uses.
ERC-20 (Ethereum Request for Comments-20) is the name used in the Ethereum community to refer to a standard (set of rules and functions) that guides the creation of fungible tokens on the Ethereum blockchain. Fungible tokens are tokens that are identical and can be exchanged on a one-for-one basis. In this article, I will go through the different functions contained in an ERc20 token standard and how they are used.
Before a token on the Ethereum blockchain can be said to be ERC20-compliant, there are certain functions the token must implement. These functions allow for creation, transfer, management and interoperability between different tokens and wallets. Below is a breakdown of these functions:
- totalSupply:
function totalSupply() public view returns (uint256);
This function returns the total number of tokens that have been created and still exist in circulation, i.e., it keeps track of the supply of the token across the entire network.
- balanceOf:
function balanceOf(address account) public view returns (uint256);
`balanceOf` returns the amount of tokens a particular account owns. It does this by mapping a user or contract address to the number of tokens it holds.
- transfer:
function transfer(address _to, uint256 _amount) public returns (bool);
The transfer function, as its name suggests, is used to transfer a certain number of tokens defined by “_amount” from the caller’s account to the recipient's account. This function can only be called by the owner of the token, and it returns a boolean indicating if the transfer was successful or not.
- approve:
function approve(address spender, uint256 _amount) public returns (bool);
In the explanation of the transfer function above, I mentioned that it can only be called by the token owner. However, someone other than the owner can transfer tokens from the owner's account through the approve function. The approve function allows the token holder approve another account (the spender) to transfer/withdraw tokens equal to but not greater than "_amount" from their account. It can only be called by the token holder, and the specified spender address would have access to the specified amount of tokens in the owner's account.
- transferFrom:
function transferFrom(address _from, address _to, uint256 _amount) public returns (bool);
If the transfer function can only be called by the token owner, how does someone who isn't the owner but has been approved by the owner to spend their tokens go about it? This is where `transferFrom` comes in. The transferFrom function allows an approved spender spend tokens up to, but not more than, the allocated amount.
- allowance:
function allowance(address _owner, address _spender) public view returns (uint256)
Allowance returns the amount specified by the token owner at the point of approving an address to spend from their token balance. This amount reduces as the spender spends from their approved/allocated tokens.
Events
The ERC20 standard also defines some events that are triggered when certain actions occur, particularly when the transfer and approve functions are called.
Transfer:
event Transfer(address _from, address _to, uint256 _value);
The Transfer event must be triggered when tokens are transferred, including zero value transfers. It takes in the address of the sender, address of the recipient and the amount of tokens transferred. This event should also be triggered upon creation of a new token with the '_from' address set to the zero address.
Approval:
event Approval(address _owner, address _spender, uint256 _value);
The approval event must be triggered after successfully calling the approve function. And it takes in the address of both owner and spender and also the desired amount.
There are some optional functions that can be added to the ERC20 token. These includes:
name: Returns the name of the token.
symbol: Returns the symbol of the token, which is usually a shorter version of the name.
decimals: Returns an integer type that helps define the smallest unit of the token.
These sets of functions make up the ERC20 token standard and help to make ERC20-compliant tokens interoperable and widely adopted across different wallets and exchanges.
Subscribe to my newsletter
Read articles from Abolare Roheemah directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by