Open In App

ERC20 vs ERC721 in Ethereum

Last Updated : 27 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

ERC or Ethereum Request for Comments is simply a guideline or a template that all Ethereum-based tokens should follow. Some rules are laid down by programmers in these templates or documents that Ethereum-based tokens must comply with. Functionalities such as transferring tokens from one account to another, to get the current token balance of an account, and the total supply of the token available on the network. ERC-20 and ERC-721 tokens are the initial types of ERC token standards that serve an important role in defining the functionality of the Ethereum ecosystem.

The following topics will be discussed here:

  1. How Are Tokens Created?
  2. Introduction To ERC-20 Token.
  3. Introduction To ERC-721 Token.
  4. ERC-20 Token vs ERC-721 Token.

Let’s start discussing each of these topics in detail.

How are Tokens Created?

Tokens exist on the Ethereum platform and that in itself consists of a blockchain that is capable of storing transactions and a virtual machine that is capable of running smart contracts. It is again important to note that the tokens live on the Ethereum blockchain. They aren’t independent and rely on Ethereum’s blockchain and platform. The native currency on the Ethereum network is Ether but it can also support other tokens and these can work like currencies that can represent shares of a company, gold certificates, and so on.

  • Tokens are created by smart contracts which are not only responsible for creating tokens but also for handling transactions and keeping track of balances. 
  • To get some tokens, one has to send some Ether to the Smart contract which will then give some tokens.
Product of Tokens

 

  • Once a smart contract is deployed, it can’t be changed anymore, so if one makes a mistake, nothing can be done about it which could be quite catastrophic. 
  • Imagine a bug inside the contract’s code that causes people to lose their tokens or get the tokens stolen. 
  • There’s also a problem with interoperability. Each token contract can be completely different from the other so if one wants the token to be available on an exchange, the exchange has to write a custom code that can talk to the contract and allow people to trade. 
  • The same thing goes for wallet providers. Supporting hundreds of tokens would be very complex and time-consuming.  
  • So instead, the community proposed a standard called ERC-20 which stands for  “Ethereum Requests for Comments” and 20 is just a number they assigned to a proposal that would create some structure in the world of tokens. 

Introduction To ERC-20 Token

ERC-20 is one of the most significant standards for Ethereum tokens. ERC-20 was first proposed in 2015 and integrated into the Ethereum blockchain in 2017. It is similar to bitcoin, Litecoin, etc in many ways. 

  • Token represents digital assets such as coupons or even real-life stuff. 
  • They can be sold, bought, and traded and have values that can be sent or received. 
  • Well-known currencies such as Basic Access Token (BAT), Augur (REP), Maker (MKR), and the OMG network are ERC-20 standards. 
  • The tokens have the same values and they are replaceable with another copy of the token. This makes it fungible i.e mutable or changeable or not unique. 

Before there was the ERC-20 standard, everyone who wanted to create a token had to reinvent the wheel which meant that each token contract was slightly different and that exchanges and wallets had to write custom code to support your token. With ERC-20 this code has to be implemented just once. That’s why exchanges can add new tokens so quickly. It’s important to keep in mind that there is no “central registry” for token contracts, which means that the uniqueness of a particular token name or symbol is not guaranteed.

ER-20 is a representation of some sort of asset on the Ethereum blockchain and a token is an ERC-20 token can represent a lot. It could represent a currency (cryptocurrency to be more clear) or any other kind of asset.

Functions of components in ERC-20 Tokens are mentioned in the below. These functions are defined in the IERC20 (Interface of the ERC20 standard ) defined in the EIP:

  • transfer(): Takes a certain amount of tokens from the total supply and transfers them to a user.
  • transferFrom(): Tokens is transferred between any two users who have them.
  • approve(): verifies that your contract can give a certain amount of tokens to a user.
  • allowance(): Same as approval, except the fact that it checks that a user has enough balance to send a certain amount of tokens to someone else.
  • balanceOf(): returns how many tokens a given address has.
  • totalSupply(): Defines the total number of tokens. When this limit is reached, the smart contract refuses to create any more tokens. 

These code functions are important when it comes to user/token implementation, mainly in making transfers, granting approvals and determining the number of tokens in circulation, storing and returning balances.

Drawbacks of the ERC-20:

ERC-20 is only a guideline and people are free to implement the required functions however they like. That has led to some interesting problems.

  • For instance, to buy some tokens, one has to send some Ether to the token contract but some people tried sending their ERC-20 tokens instead. 
  • If the contract was not designed with this in mind, it will result in your tokens being lost. 
  • In order to resolve this, the company is trying to improve the ERC-20 standards with the ERC-223 standard. 
  • This warns token creators about these risks and offers some workarounds.

Example of ERC-20 Token :- 

Solidity




// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.0 <0.9.0; 
import "@openzeppelin/contracts@4.8.1/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts@4.8.1/access/Ownable.sol";
/// @title A contract for demonstrate ERC-20 Token
/// @author Jitendra Kumar
/// @notice For now, this contract just show how to create an ERC-20 Token
 
contract GFGToken is ERC20, Ownable {
 
    constructor () ERC20("GFGToken", "GFG") {
        _mint(msg.sender, 1000 * (10 ** uint256(decimals())));
    }
     
   function decimals() public view virtual override returns (uint8) {
        return 8;
    }
}


Introduction To ERC-721 Token

ERC-721 is a standard “non-fungible” token. A non-fungible token is the type of token that is unique. These are cryptographic assets on a blockchain that has a unique code and metadata differentiating them from one another. The ERC-721 standard non-fungible tokens can be seen in blockchain-based games where each asset is unique and players can trade it and sell or buy items with it. ERC-721 is basically a template or a guideline that other developers agree to follow. It is a widely used standard. Being a widely used standard also means being compatible with a  wide range of applications.

  • They are non-fungible tokens. 
  • ERC721 is more of an Ethereum Improvement Proposal or EIP.
  • One of the most important components is the flexibility for using them in all the exchanges.
  • The main characteristic of ERC-721 tokens is that each one is unique.
  • The value of ERC-721 tokens depends on the uniqueness and rarity of the asset.
  • One NFT cannot replace another, due to differences in values.
  • We can emulate rare, collectible items with these Ethereum tokens.

Components in ERC-721 token:

The ERC721 has the following important functions:

  • name()
  • symbol()
  • tokenURI()
  • onERC721Received()
  • totalSupply()
  • balanceOf()
  • ownerOf()
  • safeTransferFrom()
  • transferFrom()
  • approve()
  • setApprovalForAll()
  • getApproved()
  • isApprovedForAll()
  • takeOwnership()
  • tokenMetadata()
  • tokenByIndex()
  • tokenOfOwnerByIndex()
  • supportsInterface()

Fungible means interchangeable and replaceable. Bitcoin is fungible because any Bitcoin can replace any other Bitcoin. Each NFT, on the other hand, is completely unique. One NFT cannot replace another, there an NFT is non-fungible. One of the examples that use the ERC-721 non-fungible tokens standard is the Cryptokitties game which is basically a game developed on the blockchain which allows users to sell, buy and breed virtual cats. The assets bought with a non-fungible token are unique and only belong to the owner. It can’t be traded. The ERC721 standard tokens are created the same way as other kinds of tokens and can be created as many as the user wishes but the only difference is that the tokens have a unique value and unique metadata.

Example of ERC-721 Token:

Solidity




// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.6 <0.9.0; 
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
/// @title A contract for demonstrate ERC-721 Token
/// @author Jitendra Kumar
/// @notice For now, this contract just show how to create an ERC-721 Token
contract GFGToken is ERC721, Ownable, ERC721URIStorage {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenCountCounter;
    constructor() ERC721("GFGToken", "GFG") {}
     
    //mint the NFT token along with the TokenURI using Owner Address
    function safeMint(string memory _tokenURI) public onlyOwner {
        uint256 tokenCount = _tokenCountCounter.current();
        _tokenCountCounter.increment();
        _safeMint(msg.sender, tokenCount);
        _setTokenURI(tokenCount, _tokenURI);
    }
    //burn the NFT token
    function _burn(uint256 tokenCount) internal override(ERC721, ERC721URIStorage) {
        super._burn(tokenCount);
    }
    //return the TokenURI of the NFT token
    function tokenURI(uint256 tokenCount) public view override(ERC721, ERC721URIStorage) returns (string memory)
    {
        return super.tokenURI(tokenCount);
    }
}


ERC-20 Token vs ERC-721 Token

Below are some of the differences between the ERC-20 token and the ERC-721 token.

 

ERC-20

ERC-721

Fungibility First and foremost, these tokens are fungible in nature. There’s nothing like a tokenId. These tokens are non-fungible in nature. Each NFT has a uint256 variable known as tokenId.
Substitution     They’re easier for substitution. There is no scope for substitution.
Examples Tether (USDT), Dai (DAI), Bitcoin, Dogecoin, Wrapped Bitcoin (WBTC)  NFTs, in-game assets like money or in-game avatars, and even tickets. One of the most popular examples of these tokens is CryptoKitties. The purpose of the game is for users to breed, buy and sell virtual cats. In this game, the user fully owns an asset; a virtual cat that is unique and cannot be shared with anyone else.
Divisibility They’re divisible; ERC20 tokens can be divided in a number of ways. Even sharing 0.1 % of the token is possible.  They’re not divisible at all.
Fluctuation
  1. The value doesn’t fluctuate, since they’re not unique. 
  2. Requires one common smart contract.
  1. The value fluctuates according to rarity and uniqueness. 
  2. Requires unique smart contracts for each token.
Ownership No special ownership functions can be allocated. Special ownership functions can be enabled by these tokens.
Adoption These tokens are commonly adopted. These tokens have limited levels of acceptance.
Collectability These tokens are not collectible, they’re  interchangeable and represent a single entity These tokens can be collected like fiat currencies, they’re not interchangeable and represent a collection of assets
Token identity There is no KYC verification required. The KYC verification is in-built.
Values In ERC20 standards, there’s no difference in values. The values of each token are different.
 Challenges faced ERC20 token faces the challenge of them being lost when are being transferred to other wallets or smart contracts that don’t support ERC20 tokens.  ERC721’s token standards’ main challenge is that transferring numerous assets can become very expensive.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads