ERC20 and ERC721 Token Standard Explained

ERC20 and ERC721 Token Standard Explained

Source: surl.li/flire

ERC20 is a token standard used for creating digital tokens(virtual assets) on the Ethereum blockchain. It means the basic rules and functionality that all ERC20 must be followed.

ERC20 tokens are fungible, which means they are interchangeable with each other and have the same value.
Each token is represented as a smart contract on the Ethereum blockchain.

Here are the following key features:

  • Each ERC20 token has a total fixed supply. that is defined when the contract is created, and cannot be changed afterward, except by the contract owner (mint() and burn())

  • ERC20 tokens can be transferred between addresses, the transfer token can be called by anyone who owns the token, it requires the recipient's address, and the amount required to transfer.

  • ERC20 also allows a 3rd party to spend a certain amount of tokens on your behalf. provided the user who owns the token grants permission for the 3rd party to spend it.

  • balanceOf() can be used to check the number of tokens a given address holds.

  • The ERC20 tokens can be divided into smaller units, like Ether. The number of smaller units that a token can be broken down to is defined by the contract creator. ranging anywhere between 0 to 18.

  • Each ERC20 token can have a name and symbol associated with it. once set cannot be changed afterward.

    All and all, the ERC20 standard allows developers to create and interact with inter-operatable tokens based on what rules apply to them.

Source: surl.li/fmcjr

The ERC721 is for non-fungible tokens(NFT) by non-fungible tokens we mean that no 2 tokens are the same. ERC20 on the other hand are fungible tokens, which means that tokens are the same. there is no difference between them.

Since no two tokens are the same in ERC721. each token is unique. which means each token can be used to represent a unique digital asset, just like owning a physical asset. Two can look the same, but they are unique.

Because of this, it makes ERC721 Ideal for representing ownership, of one of a kind.
It can represent In-game Items, Real Estate, art, etc

Key features of ERC721:

    1. Each ERC721 token is unique and has a unique identifier (ID).

      1. Each ERC721 token is owned by a single address at a time.

      2. ERC721 tokens are non-fungible, meaning that they cannot be exchanged on a one-to-one basis with other tokens.

      3. ERC721 tokens can be transferred from one address to another using the safeTransferFrom function.

      4. ERC721 tokens can be owned by contracts, allowing for more complex ownership models and game mechanics.

The ERC721 standard includes several optional functions that can be used to provide additional functionality, such as metadata retrieval and batch transfers.

The ERC721 contract can hold nfts of one type(one particular concert, 4 housing deeds).
The problem with ERC721 is if there are 5 concerts and you want to map the seats to an nft. you'll need 5 separate contracts. That means each contract holds only one type of nfts for a given purpose.
If I ask for the `balanceOf()` against a given address, I'll get the NFT's Id associated with only that given contract.
Secondly, you do not save the deed of the house or image, or whatever your digital asset is on the blockchain. you save it on an external server. what you save here is the id of that associated asset. it could be a hash of the content. or just a pointer along with some metadata. or something like that. We don't save the image on the blockchain as it's really heavy.

The ERC721 standard defines the following functions:

  • balanceOf(address owner) returns (uint256 balance) - This function returns the number of tokens(In this case the number of digital assets owned by the owner. These digital assets can be associated with only one contract. If Aaron owns 4 ERC721 tokens, the returned value will be 4.

  • ownerOf(uint256 tokenId)- Here this function returns the address of the owner of a specific token. for example, if a token ID 847 is owned by Charlene. then the address of Charlene will be returned.

  • safeTransferFrom(address from, address to, uint256 tokenId, bytes data):- This method is used to transfer an NFT from one user to another. We first check if the address is capable of accepting that given NFT, if yes then we transfer it to them. data is just additional info.[its optional]

      myNFT.safeTransferFrom(CharlenesAddress, SonalisAddress, 847, "Congratulations on your new token!");
    
  • approve(address to, uint256 tokenId): This function approves another address (3rd party) to transfer ownership of a token.
    The approved address can later on, call safeTransferFrom to transfer the token on behalf of the owner. For example, the following code approves Bob to transfer ownership of token ID 8:

      myNFT.approve(Bob, 8);
    
  • setApprovalForAll(address operator, bool approved): This function sets the approval status of a third-party operator for all tokens owned by the caller. The operator can call transferFrom to transfer any of the caller's tokens. For example, the following code sets Alice's brother Charlie as an approved operator for all of Alice's tokens:

  •   myNFT.setApprovalForAll(Charlie, true);
    
  • getApproved(uint256 tokenId): This function returns the address approved to transfer ownership of a specific token. For example, if Bob is approved to transfer ownership of token ID 9, calling getApproved(9) will return Bob's address. So its basically who approved to transfer the given token by this 3rd party.

  • isApprovedForAll(address owner, address operator): This function returns true if an operator is approved to transfer all tokens owned by an owner. For example, if Charlie is approved to transfer all of Alice's tokens, calling isApprovedForAll(Alice, Charlie) will return true.