ERC-20 Explained: Breaking Down Key Functions and Their Roles
Exploring the Basics of ERC-20 Tokens
As beginner smart contract writers, encountering ERC-20 tokens is inevitable. It has become the backbone of most tokens on the Ethereum blockchain, enabling seamless interaction between smart contracts and decentralized applications.
This technical article is made to dive deep into the logic that makes up the erc-20 token, line by line.
Let's dive in
What is the ERC-20 Token?
The erc-20 token is built based on the EIP-20: The Ethereum Improvement Proposal (EIP) 20, which defines a standard interface for tokens on the Ethereum blockchain.
Analysis
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view
returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount)
external
returns (bool);
}
Core Functions
As with the interface
syntax, for you to be able to create an Erc-20 token, your functions have to implement every method stated in the IERC20
.
function totalSupply() external view returns (uint256);
The first function totalSupply returns a
uint256
, which is the total supply of the token .the visibility
external
,which means it can be accessed only outside the contract , when it has been deployed.The
view
modifier, which means it can only be read, and does not modify the state
function balanceOf(address account) external view returns (uint256);
The balanceOf function returns the account balance of another account with address
It takes in a parameter
address account
which is an ethereum address . The address is queried , and returns an integer which is the balance.the
visibility
external, which means it can be accessed only outside the contract , when it has been deployed.view
modifier, which means it can only be read, and does not modify the state
transfer
function transfer(address recipient, uint256 amount) external returns (bool);
The transfer function allows one wallet address to send an ERC20 token to another
it takes in two parameters the recipient address
, and the uint256 amount
.
It returns a boolean, to confirm true if successful and false if reverted.
transferFrom
function transferFrom(address sender, address recipient, uint256 amount)
external
returns (bool);
The transferFrom function takes in three parameters:
address sender
: The _from address which you are sending fromaddress recipient
: The _to addressuint256 amount
: The value as an integer.
It returns a boolean to confirm true if successful and false if reverted.
Approve
function approve(address spender, uint256 amount) external returns (bool);
The approve function allows the owner of the account permit another wallet to spend their tokens for them.
It takes in 2 parameters
address spender
: the address of the spenderuint256 amount
: the value of the token
It returns a boolean value indicating whether the approval was successful true
or not false
.
Allowance
function allowance(address owner, address spender) external view
returns (uint256);
This function returns the remaining number of tokens that the spender
is allowed to transfer from the owner
account. It takes in 2 parameters:
address owner
: where it runs checks to confirm the owner of the tokenaddress spender
:the ethereum address of the spender
It returns a value which is the amount the spender can still withdraw from the account.
Events
The
transfer
function emits an eventevent Transfer(address indexed from, address indexed to, uint256 value);
whenvalue
tokens are moved from one account (from
) to * another (to
). It triggers even if it is a zero value.The
approve
function emits an eventevent Approval(address indexed owner, address indexed spender, uint256 value);
when the allowance of aspender
for anowner
is set by a call to {approve}. Thevalue
is the new allowance.
It triggers only when the approve call is successful
Implementation
Examples of these implementation of the above is the Oppenzeppelin ERC-20 or the Consensys ERC-20
Conclusion
Mastering ERC-20 token functions is essential for navigating and innovating within the Ethereum ecosystem. Whether you're building or exploring, this knowledge will empower your journey in the blockchain space.