Learn more about the technical details of how ERC20 and ERC721 tokens work in a simplified explanation.
From the CMC Editorial Desk: You’ve heard about ERC20 and ERC721 “token standards” – but what do they look like under the hood? Find out in this simple-to-read piece.
Ethereum tokens became popular in 2016 / 2017 when they started to become widely used by ICOs to represent utility or ownership. Later, in 2017, Ethereum tokens also started to be used to represent in-game assets, like in the famous game CryptoKitties.
One of the most interesting property of tokens is that they can be traded. If I buy a token, I want to be able to sell it for another token, or for Ether. In order to enable this, we need to standardize tokens – That’s what ERC (Ethereum Request for Comments) were made for. Inspired by the famous internet RFCs (Request For Comments), ERC is a public and open system where anyone can create and comment on proposals for standardizing Ethereum smart contracts and tokens. Note that this is different from EIP (Ethereum Improvement Proposals) which deal with the Ethereum protocol itself.
Two of the most common ERC token standards are ERC20 and ERC721. They are used respectively to represent fungible assets and non-fungible assets. In this article we will break down the structure of ERC20 and ERC721 tokens, and see how they work.
Fungible vs. non-fungible assets
WA fungible asset is an asset that can be swapped with another one, like a currency. For example, if I have a 1 USD bill, it can be swapped for any other 1 USD bill. It does not matter which 1 USD bill I own, they all represent the same value. A share in a company is the same. If I buy 100 shares of Microsoft, I don’t care which shares I get, I just want to receive 100 shares.
A non-fungible asset is an asset that cannot be swapped for another. For example, a house is a non-fungible asset. Each house has some unique properties that makes it different. When you get back from work, you (probably) don’t want your neighbor to greet you with “Hi buddy, I have swapped your house for mine, these are your new keys, cheers!”
Fungible vs. non-fungible assets in cryptoland
In the Ethereum crypto world, assets are also grouped into fungibles and non-fungibles.
Fungible assets are represented by ERC20 tokens. These tokens represent ownership in projects, vouchers redeemable for services, staking tokens, or governance tokens.
Non-fungible assets are represented by ERC721 tokens. Currently, the only prominent use of ERC721 tokens are in-game assets. For example, in CryptoKitties, a game where you collect and breed virtual kitties, each kitty is represented by a unique ERC721 token. The future tokenization of real-world assets, like your house, could use ERC721 tokens.
A coin in the coin
Before we get deeper into the ERC20 and ERC721 specifications, it’s important to understand that tokens live in smart contracts, which themselves live in the Ethereum blockchain. Tokens can be looked at as a “coin in a coin”:
The Ethereum blockchain itself has no salient distinction of ERC20, ERC721 as tokens. To Ethereum, tokens are just variables defined in smart contracts. It’s just humans writing the contracts who decide to assign some particular meaning to some variables in smart contracts.
So far we have talked about the ERC20 and ERC721 standards from a high-level perspective, but in the next few sections we are going to dive into these standards and see how they work.
The ERC20 standard
The ERC20 standard was created by Fabian Vogelsteller in 2015. (You can find the whole specification in this link.) For the sake of brevity, let’s recap the most important parts here.
First, it’s important to understand that the ERC20 standard, like other token standards, only defines the interface / API of a smart contract, but not its implementation.
When you go to McDonald’s and order a burger, you only tell the cashier what kind of burger you want, but you don’t tell him/her all the little details about how to cook the burger. In the kitchen, the cooks will deal with this. Ordering a burger is the interface of McDonald’s, and cooking the burger is the implementation.
In smart contracts the code is organized into logical groups called “functions”. The interface of the smart contract is a description of what its functions should do, and the implementation is the actual code of the functions.
An ERC20 token is a smart contract with code that respects the ERC20 standard. Practically, this means that the functions of the smart contract respect the interface defined in the ERC20 standard.
If we want to understand the ERC20 standard, we need to have a look at the function interfaces defined inside.
There is a group of functions that defines the metadata of smart contracts such as:
Anyone can call these functions. The smart contract will answer with the relevant information.
But the meat of the ERC20 standard is the account system. An ERC20 token manages a ledger of Ethereum addresses and token balances, just as a bank manages a ledger of account owners and fiat money balances:
While a bank manages this ledger in a traditional database, an ERC20 token uses variables in its smart contracts. If you never heard of variables in programming languages, they are basically slots that you can use to store and reference data.
Besides storing tokens, an ERC20 token must also be able to transfer tokens. There are 2 functions in the ERC20 standards for that:
Why do we need 2 functions and not just one?
The first function allows the owner of a token (identified by his/her Ethereum address) to transfer tokens to another Ethereum address. The cryptographic system of the Ethereum blockchain will ensure that the rightful owner of the tokens triggered the function.
The second function allows a third-party Ethereum address to do a token transfer on behalf of the actual owner of the coin. This is very similar to credit cards where merchants can be allowed to debit credit cards on behalf of their owner. The big difference is that in the case of ERC20 tokens, we only allow a third party to spend a specific amount of tokens, but not a potentially large or unlimited amount, like for credit cards.
The full process of third-party payment involves 2 steps:
- First, the owner of tokens approves a third-party to spend a token amount on his/her behalf;
- Then, the approved third-party can spend up to this token amount. These tokens can be sent to any Ethereum address.
If you have used decentralized exchanges such as IDEX, you might already be aware of this mechanism. That explains why you need several transactions to trade with them.
The ERC721 Standard
The ERC721 standard was introduced in late 2017 by the popular game CryptoKitties. As mentioned earlier, in this game, players collect virtual kitties that are each represented as an ERC721 token.
This seems familiar… What’s the difference with ERC20 tokens? Well, while an ERC20 token represents a single type of asset, an ERC721 token represents a class of assets. In the case of CryptoKitties, its ERC721 token contract represents ALL the unique kitties in the game, as well as who owns which.
Compared to the ERC20, in the case of ERC721, the ownership is simplified: a player fully owns an asset, or not. It’s not possible to own “half a kitty” in Cryptokitties, for example. That is why we say the ERC721 token standard is for non-fungible assets.
That’s the most important thing to know for the ERC721 standard. The rest of the standard is very similar to the ERC20 standard, in particular in terms of token transfers. If you want to know more about the specific functions of the standard, you can read the ERC721 standard document.
ERC20 and ERC721 are works in progress. They both have their own sets of challenges, and improvements are constantly being discussed. For example, the ERC20 standard has a flaw in which tokens can be lost when sent to wallets or other smart contracts that can’t handle ERC20 tokens. ERC223 and ERC777 were created to fix that. As for ERC721, it can be really costly to batch transfer a lot of assets. That’s why the ERC721x standard was created.
About the author
If you want to learn how to develop Ethereum Dapps & Smart Contracts, go to my website and youtube channel EatTheBlocks. You will find a lot of free tutorials about Ethereum, Solidity, Truffle & other tools for Dapps. I also released a video course with Manning to create a full Decentralized Exchange (DEX) for ERC20 tokens on Ethereum.