"Understanding the ERC20 Token Standard in The Ethereum Blockchain"
Introduction
The introduction of the blockchain technology has changed the way we think about assets (digital assets) and decentralized finance (DeFi). At the heart of this innovation lies the concept of tokens—digital assets that represent value, ownership, or utility within a blockchain ecosystem.
This article aims to explain the ERC20 standard, explaining its significance in the Ethereum ecosystem, the key concepts behind it, and why it is used as a standard to create token.
What is the ERC20 Token Standard?
The ERC20 standard is a specification used for creating tokens on the Ethereum blockchain. It defines a set of rules and functions that Ethereum-based tokens must adhere to in order to interact seamlessly with the Ethereum network.
Importance of Using the ERC20 Standard
For a token to be compatible with ERC20, the functions and the features of ERC20 needs to be implemented.
Although, further functionalities can be added by implementing other functions but the ones in this standard interface (ERC20) must be implement in order to promote interoperability between smart contracts.
Aside the fact to promote interoperability between smart contracts, the ERC20 token standard is secured due to well-established set of rules.
It is a complex and time-consuming process to start the creation of a token, the ERC20 is a ready-made blueprint that developers can use as a foundation and it adoption by many has created a robust ecosystem. So, why reinvent the wheel?
Understanding the Interface in ERC20
An interface is a blueprint of methods and constants that must be implemented by any class or contract that implements it, without specifying how these functions should be executed.
In Solidity, which is the programming language for writing Ethereum smart contracts, interfaces are used to ensure that a contract or contracts follow a specific structure.
There are 9 functions on the ERC20 token standard, in which 6 are compulsory and 3 are optional.
The compulsory ones are as follows and each function is explained.
totalSupply() : This function returns the total number of tokens supplied.
function totalSupply() public view returns (uint256) { return _totalSupply }
balanceOf() : This function returns the account balance of another account with address _owner.
function balanceOf(address _owner) public view returns (uint256 balance)
transfer() : This function allows a token holder to transfer tokens to another address.
function transfer(address _to, uint256 _value) public returns (bool success)
The transfer()
function checks that the sender (msg.sender
) has enough tokens to transfer. If they do, it subtract the amount from the sender's balance and add it to the recipient's balance.
Then, it fires the Transfer
event to notify the network. The function SHOULD throw
if the message caller’s account balance does not have enough tokens to spend.
Lastly, it returns true
to indicate that the transfer was successful.
Note: Transfers of 0 values MUST be treated as normal transfers and fire the Transfer
event.
approve(): This function allows
_spender
to withdraw from your account multiple times, up to the_value
amount. If this function is called again it overwrites the current allowance with_value
.function approve(address _spender, uint256 _value) public returns (bool success
Although, in order to prevent attack vector, clients SHOULD make sure to create user interfaces in such a way that they set the allowance first to
0
before setting it to another value for the same spender. Further reading can be found here docstransferFrom(): This function allows contract or spender to transfer tokens on your behalf (sender) to a recipient, using the allowance mechanism.
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success)
The
transferFrom()
function first checks if thesender
has enough tokens and if themsg.sender
is allowed to transfer the specifiedvalue
, it then reduces the sender’s balance and the spender’s allowance by thevalue
.The recipient’s balance is increased by the
value
, and aTransfer
event is emitted and it returnstrue
to indicate that the transfer was successful.Note: Transfers of 0 values MUST be treated as normal transfers and fire the
Transfer
event.allowance(): This functions returns the amount which
_spender
is still allowed to withdraw from_owner
.function allowance(address _owner, address _spender) public view returns (uint256 remaining)
The optional methods are as follows:
name()
: It returns the name of the token, e.g., "NewTokenCreated".symbol()
: This returns the token's symbol, e.g., "NTC".decimals()
: This returns the number of decimal places the token uses, typically set to 18 for Ethereum-like tokens.Note: These methods can be used to improve usability. However, other contracts should not expect these values to be present.
Benefits: Using an interface provides several advantages. It ensures that all ERC20 tokens can be easily integrated with existing infrastructure, reduces the likelihood of errors in contract implementation, and promotes code reuse.
Conclusion
Following the ERC20 standard is crucial for developers looking to develop tokens on the Ethereum blockchain. Its widespread adoption, security features, and ease of use make it an indispensable tool for developers and businesses alike.
Subscribe to my newsletter
Read articles from Michael John directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by