DEV Community

Tchisom17
Tchisom17

Posted on

Understanding ERC-20: The Standard for Fungible Tokens on Ethereum

Introduction

In the consistently advancing universe of blockchain technology, Ethereum has arisen as one of the most distinguished platforms for decentralized applications (dApps) and smart contracts. A vital part of Ethereum's environment is the idea of tokens — digital assets that address different utilities or values inside a blockchain network.

Among the numerous symbolic guidelines accessible on Ethereum, ERC-20 has turned into the most broadly embraced and perceived norm for making fungible tokens. These tokens are indistinguishable in value and can be handily traded, making them the foundation of many decentralized applications and monetary systems. In this article, we'll dive into what ERC-20 is, the reason it makes a difference, and how you can make your own ERC-20 token on Ethereum.

What is ERC-20?

ERC-20, short for Ethereum Request for Comment 20, is a specialized standard utilized for making and giving smart contracts on the Ethereum blockchain. These smart contracts are liable for overseeing tokens — digital assets representing anything from currency to loyalty points.

Fabian Vogelsteller proposed ERC-20 in November 2015 and it immediately turned into the norm for making fungible tokens on Ethereum. The standard blueprints are a bunch of rules and functions that a token contract should execute, guaranteeing that all ERC-20 tokens act typically and can be integrated with different dApps, wallets, and exchanges.

The purpose of ERC-20 is to create a standard set of rules for creating tokens. This standard ensures that any ERC-20 token can easily interface with other smart contracts, DAPPs and decentralized exchanges on the Ethereum blockchain.

Key Features of ERC-20

Fungibility
The fungibility of ERC-20 tokens is one notable feature. Then there are fungible tokens, which have no unique properties and characteristics meaning one token is the same as any other. This makes ERC-20 tokens best suited for currency-like assets where every unit should be equal and exchangeable.

Interoperability
This is a capability that the creators had in mind when they were creating ERC-20s, hoping for these tokens to be interoperable with as long of a list of dApps and wallets and exchanges as possible. These functions allow for this interoperability, and they define the standard that ERC-20 tokens are meant to meet. As a result, any ERC-20 tokens compatible app can easily add new Tokens without major development effort.

Standardized Functions
Each contract included in the ERC-20 standard specifies a core set of functions that all contracts are required to provide. Such functions are basic functions such as transferring tokens and querying balances and limiting how many times third parties can be allowed to utilize the tokens owned by the particular owner. Such standardization facilitates the influx of new tokens and interoperability among these new token services and the rest of the Ethereum ecosystem.

The Core Functions of ERC-20

The ERC-20 standard outlines six core elements without which no token contract will be created. These functions make up the structure upon which the ERC-20 tokens rest regarding operation and integration with contracts and applications.

  • totalSupply(): It indicates the number of tokens that have been created and are stored in the blockchain. If a smart contract implements this function, it declares how many tokens, in total, can be issued for free or sold to investors.
    /**
     * @dev Returns the value of tokens in existence.
     */

    function totalSupply() external view returns (uint256);
Enter fullscreen mode Exit fullscreen mode
  • balanceOf(address _owner): It explains how many tokens a given address is holding. It is a function that is used in checking the balances of any owned Ethereum tokens.
    /**
     * @dev Returns the value of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);
Enter fullscreen mode Exit fullscreen mode
  • transfer(address _to, uint256 _value): This function enables the owner of the token to send a given amount of the token to another address. It is the simplest of all possible functions which allows transferring the tokens to and excluding the knees of patients.
    /**
     * @dev Moves a `value` amount of tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 value) external returns (bool);
Enter fullscreen mode Exit fullscreen mode
  • approve(address _spender, uint256 _value): This function enables a token owner to approve a third party (the spender) for the withdrawal of a certain number of tokens from their account which includes but is not limited to the user’s account. This often happens within the decentralized finance applications where a smart contract has to move the tokens on the user’s behalf.
    /**
     * @dev Sets a `value` amount of tokens as the allowance of `spender` over the
     * caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 value) external returns (bool);
Enter fullscreen mode Exit fullscreen mode
  • allowance(address _owner, address _spender): This method allows a spender receiving the amount to know the total amount yet to be withdrawn from the owner's account. It works hand in hand with the approve() method in giving authorizations for tokes to be used.
    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);
Enter fullscreen mode Exit fullscreen mode
  • transferFrom(address _from, address _to, uint256 _value): This function allows a third party (the spender) to transfer tokens on behalf of the token owner subject to the condition that there is reasonable allowance. This feature is critical in the functioning of smart contacts to enable them to integrate token management within intricate operations.
    /**
     * @dev Moves a `value` amount of tokens from `from` to `to` using the
     * allowance mechanism. `value` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address from, address to, uint256 value) external returns (bool);
Enter fullscreen mode Exit fullscreen mode

Creating an ERC-20 Token

Creation of an ERC-20 token involves writing a smart contract for the token in the Ethereum language Solidity. Below, we’ll walk through the basic steps to create a simple ERC-20 token.

Setting Up the Development Environment
Before writing the contract, you will be required to prepare a development environment. Some known tools are:

  • Remix IDE: An Integrated Development Environment which runs in a web browser to create, analyze, and launch the development of periodic smart contracts.

  • Truffle Suite: Ethereum's Development Framework comes with an array of tools that allow for the development, testing, and deployment of decentralized applications.

  • Hardhat: More control over network simulation and contract tests is accessible with this adaptable development environment.

Writing the Solidity Contract
Now that the environment is set up, it’s time to write the code for your ERC-20 token. Below gives the reader a simple understanding of an ERC-20 contract:

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MyToken is ERC20 {
    constructor() ERC20("MyToken", "MTK") {
        _mint(msg.sender, 1000000 * 10 ** decimals());
    }
}

Enter fullscreen mode Exit fullscreen mode

In this contract:

  • ERC20("MyToken", "MTK"): This parameter includes the name of the token and its symbol.
  • _mint(msg.sender, 1000000 * 10 ** decimals()): The address that is registered as the creator of the address should receive exactly 1,000,000 tokens.

Deploying the Contract on Ethereum
Having prepared and written the said contract, it’s possible to deploy it into the Ethereum Blockchain. This can be done with the use of tools like Remix, Truffle, or Hardhat. After which, the token will be operational on the Ethereum platform and it will be available for trading or using it within dApps.

Conclusion

The adoption of the ERC-20 standard by developers when creating a fungible token has indisputably marked the legitimation of this crucial subsystem of Ethereum. The stability, popularity and cross-functional nature of the token is a worthwhile factor for developers more so those who are looking to launch new tokens. During this phase, whether it is a new cryptocurrency, a DeFi platform, or utility tokens for your dApp there is a great emphasis on the loss-free compliance and the use of the ERC-20 standard.

The guide in several other newspapers, the Safe ERC20 defined as: this new implementation will want to easily create a more secure fast page application using the guidelines oriented to the core features of the ERC20 safe multi-coin.

So if you are eager to create your own ERC-20 token or you want to expand your knowledge of Ethereum development, go straight to Solidity and try creating your tokens. Please share your ideas, questions, or comments in the comments section below. Happy coding!

Top comments (2)

Collapse
 
properprogress_umunnakwe profile image
Proper-Progress Umunnakwe

Great content, thanks for sharing

Collapse
 
chinonso_nwajagu_67d1ab97 profile image
Chinonso Nwajagu

Great content