Token standards introduction: ERC-20, ERC-777, ERC-721, and ERC-1155

Introduction

With the rise in popularity of blockchain technologies and cryptocurrencies, we are seeing many more tokens being created. But what are they, exactly? This post will go over some of the most common types of crypto tokens: ERC-20, ERC-777, ERC-721, and ERC-1155.

What are tokens?

Tokens are digital assets built on a cryptocurrency’s blockchain. They differ from coins because while a coin is built on its native blockchain, a token is built on an existing blockchain.

For example, ETH is the official coin for the Ethereum blockchain, while Basic Attention Token (BAT), Chainlink (LINK), or OmiseGo (OMG) are tokens built on Ethereum.

Tokens are often a quicker way to leverage the existing standards of a successful and popular blockchain while building digital assets. Their most common use case is smart contracts on decentralized applications (DApps).

Difference between fungible and non-fungible tokens

Tokens generally fall under two categories: fungible tokens and non-fungible tokens.

Fungible tokens are divisible digital assets that you can swap for other assets of the same type. No single unit is worth more or less than another. Think about how a dollar bill is similar to another one and can be easily exchanged.

On the other hand, non-fungible tokens (also known as NFTs) are digital assets that you cannot easily exchange for other assets due to their uniqueness. For example, consider an artwork that holds a particular value for its owner and would be difficult to exchange for another artwork because their perceived values are different.

ERCs and EIPs

Before the adoption of token standards, blockchain developers often created tokens according to personal preferences, causing token ecosystems to experience difficulties when interacting with each other and limiting interoperability.

To solve this problem, Ethereum developers now create ERCs (Ethereum Request for Comments) and EIPs (Ethereum Improvement Proposals). These help to define the rules and required functions for tokens created on the Ethereum blockchain, making integrations and interactions much more accessible.

ERC-20 tokens

ERC-20 is a protocol that defines standard APIs for creating fungible tokens in a smart contract. It was created in 2015 by Fabian Vogelsteller.

Its main methods include:

  • name() (optional): The name of the token.
  • symbol() (optional): The symbol of the token.
  • decimals() (optional): The decimal places of the token. This allows for fungibility.
  • totalSupply(): The total number of existing tokens.
  • balanceOf(): The total number of tokens owned by a particular account.
  • transfer(): Moves a number of tokens from the caller’s account to a specified address.
  • transferFrom(): Same as transfer(), but it also specifies the address to move tokens from.
  • allowance(): The number of tokens a spender is allowed to spend on behalf of the owner via transferFrom().
  • approve(): Sets the number of tokens a spender is allowed to spend.

All these methods are strictly required in an ERC-20 smart contract, apart from the first three marked (optional) which help to improve usability.

An ERC-20 also has two events—Transfer (which triggers when tokens are transferred) and Approved (which triggers on any successful call to the approve() method).

Examples of ERC-20 tokens are USDC, a stablecoin pegged to the dollar, or UNI, the governance token used in Uniswap to vote on changes on the platform.

Many projects use ERC-20 tokens during their fundraising period (known as ICO – Initial Coin Offering). They are also widely used for trading purposes—some exchanges only support trading in tokens that adhere to this standard due to their popularity among investors and traders.

ERC-777 tokens

The ERC-777 standard offers improvements in how users interact with fungible tokens in a smart contract while remaining backward compatible with ERC-20. It was created in 2017 by Jacques Dafflon, Jordi Baylina, and Thomas Shabibi.

Some of its improvements include clearing the confusion around decimals in a smart contract and introducing hooks that allow your smart contract to react when you send or receive tokens. These help to prevent tokens from getting locked forever or lost when sent to the wrong address.

ERC-777 contains methods like:

  • granularity(): The smallest part of the token that is not divisible.
  • send(): Sends a specific amount of tokens to a recipient
  • burn(): Destroys a specific amount of tokens, which reduces the totalSupply.
  • isOperatorFor(): Checks if an account is an operator.
  • authorizeOperator(): Makes an account an operator.
  • revokeOperator(): Revokes an account’s operator status.
  • defaultOperators(): Returns a list of token holders who are default operators.
  • tokensToSend(): This hook is called when tokens are about to be destroyed or moved from a specific holder’s address. It is triggered before the smart contract’s state is updated and can prevent the operation from being executed.
  • tokensReceived(): This hook is called when tokens are about to be destroyed or moved to a specific holder’s address. It is triggered after the smart contract’s state is updated, which can prevent the operation from being executed.

ERC-721 tokens

ERC-721 is a standard for creating non-fungible tokens (NFTs) on Ethereum. It was created in 2018 by Dieter Shirley, Jacob Evans, Natassia Sachs, and William Entriken.

A few examples include rare collectibles (e.g., CryptoKitties and Gods Unchained), or limited edition items like sneakers and art prints.

They can also signify membership in a community, like Developer DAO.

A basic ERC-721 smart contract contains methods like:

  • balanceOf(): The number of tokens in the owner‘s account.
  • ownerOf(): The tokenId of the owner.
  • safeTransferFrom(): Safely transfers tokens from the owner’s address to the recipient’s. The tokenId must be specified as a parameter.
  • transferFrom(): Same function as safeTransferFrom(), but generally not recommended.
  • approve(): Allows an address to transfer a token identified by its tokenId, into another account. It triggers the Approval event.
  • setApprovalForAll(): Allows an operator to call safeTransferFrom or transferFrom for any token owned by the caller.
  • getApproved(): Gets the approved account for a specific tokenId.
  • isApprovedForAll(): Checks if an operator is allowed to manage all the assets of the owner.

It also contains events like Transfer (which triggers when ownership of any NFT changes) and Approval (which activates when the approved address for an NFT is changed).

ERC-721 has multiple extensions split across different contracts.

Here are two such extensions.

ERC721Enumerable

The ERC721Enumerable contains all the methods available in the original ERC721 and three extra methods:

  • totalSupply(): The total amount of tokens in the contract.
  • tokenOfOwnerByIndex(): The tokenId of an owner’s address at a given index in its token list. You can use it with balanceOf to enumerate all of the owner‘s tokens.
  • tokenByIndex(): The tokenId at a given index. You can use it with totalSupply to enumerate all tokens.

This extension is often not implemented because enumerating tokens on the blockchain could significantly spike gas costs.

ERC721A

ERC721A is an extension of ERC-721 that aims to significantly reduce transaction fees by allowing users to mint multiple unique NFTs in a single transaction.

It was created by the Azuki team in 2022 and is currently used by projects like Dastardly Ducks and Zero Gravity Club.

In addition to the original ERC721 methods, it contains extra methods like:

  • _startTokenId(): The starting token ID.
  • _nextTokenId(): The next token ID to be minted.
  • _totalMinted(): The total amount of minted tokens.
  • _numberMinted(): The number of tokens minted by an owner.
  • _getAux(): Gets the auxiliary data for an owner (e.g., the number of whitelist mint slots used.)
  • _setAux(): Sets the auxiliary data for an owner
  • _ownershipOf(): The token ownership data for a specific tokenId.
  • _initializeOwnershipAt(): This can be used to initialize some tokens in a large batch to reduce first-time transfer costs. It initializes token ownership data at the index slot.
  • _mint(): Mints a number of tokens and transfers them to a specific address.
  • _safeMint(): Same functionality as _mint, but it contains a data parameter which gets forwarded to contract recipients in IERC721Receiver.onERC721Received.
  • _beforeTokenTransfers(): This hook is called before a token ID set is about to be transferred. It is also called before burning one token. It contains the startTokenId and the quantity of tokens.
  • _afterTokenTransfers(): This hook is called after a set of token IDs are to be transferred.

ERC-1155 tokens

The ERC1155 protocol combines the abilities of ERC-20 and ERC-721, allowing tokens to have fungible and non-fungible characteristics. It was created by Witek Radomski and made public in 2019.

ERC-1155 provides a way to model assets and their ownership, as well as a way to create, transfer, and settle those assets. With these capabilities, you can trade fungible assets like gold bullion, or collectibles such as art, baseball cards, loyalty points, etc., using the same smart contract.

In an ERC-1155 smart contract, the balanceOf() method contains an id argument to identify the token you want to query its balance.

It also contains other methods like:

  • balanceOfBatch(): It returns the balance in a batch of accounts with specified ids.
  • setApprovalForAll(): Allows an operator to transfer a caller‘s tokens.
  • isApprovedForAll(): Checks if an operator is allowed to transfer a caller‘s tokens.
  • safeTransferFrom(): Transfers a number of tokens from a caller‘s address to a recipient’s address. The tokens must have a type of id.
  • safeBatchTransferFrom(): Same functionality as safeTransferFrom() but in batches.

Conclusion

The first step in creating a smart contract is deciding the right tool for the job. You can choose to create a simple fungible token based on ERC-20, improve its features based on ERC-777, create NFTs with ERC-721, or hybrid smart contracts with ERC-1155.

Other token standards exist with their specific use-cases, like ERC-4626 for tokenized vaults and you can learn more about crypto tokens through our blog or these resources listed below.

Resources

Have you already explored what you can achieve with Chainstack? Get started for free today.

Divine Orji is a software engineer passionate about building fast, scalable apps with beautiful user interfaces and seamless user experience. He is an avid learner and enjoys sharing knowledge as part of a community.

Chainstack uses cookies to provide you with a secure and
personalized experience on its website. Learn more.