Understanding the Architecture of ERC-20 Tokens
ERC-20 tokens are fungible digital tokens that exist on the Ethereum blockchain. "Fungible" means that individual tokens are not different from one another within the set, similar to traditional fiat currency like the US dollar.
What is ERC-20?
ERC-20 is a technical standard that defines a set of rules for how tokens are transferred, how transactions are approved, and the total supply of tokens. The ERC-20 standard was introduced in 2015 through EIP-20.
In this article, we will take a deep dive into the structure and architecture of the code that constitutes an ERC-20 token.
ERC-20 Token Contract Overview
The ERC-20 token contract inherits from one abstract contracts and three interfaces, as explained below:
IERC20: This is an interface that contains function and event definitions without implementation.
IERC20Metadata: This is an interface that also contains function definitions without implementation.
Context: This is an abstract contract that contains functions that return either
msg.sender
,msg.data
, or_contextSuffixLength
, which is 0.IERC20Errors: This is an interface that contains implementations of custom errors.
Functions in the ERC-20 Contract
Let’s take a closer look at the various functions that make up the logic of the ERC-20 contract:
Constructor Function: This function takes two parameters: the name and symbol of the token.
name
Function: This function returns the name of the token.symbol
Function: This function returns the symbol of the token.decimal
Function: This function returns the number of decimals used to get user data. Most tokens use a value of 18 to reflect the relationship between Ether and Wei.totalSupply
Function: This function returns the total supply of the token.balanceOf
Function: This function returns the balance of every account address using a mapping that uses addresses as keys to keep track of balances inuint256
.transfer
Function: This function handles the transfer of tokens from afrom
address to ato
address, specifying the value to be transferred.allowance
Function: This function returns the number of tokens a spender is allowed to spend on behalf of the owner. By default, it is zero.approve
Function: This function sets the value amount of tokens as the allowance of the spender and returns a boolean value to indicate whether the transaction succeeded.transferFrom
Function: This function sends an amount of tokens from thefrom
address to theto
address using the allowance mechanism._update
Function: This function transfers tokens between thefrom
address and theto
address, and also alternatively mints or burns tokens if theto
orfrom
address is the zero address._mint
Function: This function creates a specified amount of tokens and assigns them to an account by transferring the tokens from address 0._burn
Function: This function destroys a specified amount of tokens from an account, thereby reducing the total supply._spendAllowance
Function: This function updates the owner’s allowance for the spender according to the value that has already been spent by the spender.
This is a high-level overview of the functions in an ERC-20 token contract. However, there are other layers of complexity in the logic that have not been fully covered in this article.
Subscribe to my newsletter
Read articles from Shaaibu Suleiman directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by