silver and gold coins

Fungible Tokens in Ethereum: ERC-20

Part of this series:

Some time ago, I wrote a post about Fungible and Non-Fungible Tokens, with the intent of starting a series on them. Unfortunately, I never followed up on that, but given the current interest in NFTs, this seems like a great moment to revive that series. 

Before I dive into NFTs, and the ERC-721 and ERC-1155 standards in later posts, I start with the easier and original notion of Fungible Tokens, and the definition that started it all: ERC-20.

ERC-20

You can tell by looking at the number that ERC-20 has been around for a long time. It was, indeed, first proposed in 2015, which means in the early days of Ethereum. It is, for me, a genius idea. Let me explain.

Bitcoin was created to be a decentralized currency, running on top of an innovative construction: the blockchain. Ethereum, coming about 7 years later, was built as a blockchain with a difference: the ability to create and execute arbitrary logic, in the guise of smart contracts.

The Ethereum blockchain was powered by a resource, the Ether token, that is needed to pay for the execution of smart contracts. By sheer market forces (Supply and Demand) Ether became a valuable commodity, but it was still not meant to be a currency. That role was filled by ERC-20. Its purpose is to standardize a way to create an arbitrary tradeable token with currency-like properties in Ethereum. Moreover, all of these tokens are compatible and can mostly be handled with the same code.

This enables anyone to quickly create a new currency by applying the definitions of ERC-20 without reinventing the wheel. Besides, this compatibility also enabled different projects to share or reuse tokens and truly build on a common economy in the Ethereum space.

What are ERC-20 Tokens?

The ERC-20 standard enables the creation of smart contracts in the Ethereum blockchain that have a very simple mechanism. Each contract acts as a centralized master ledger for a new currency. ERC-20 tokens are fungible. This means they don’t have any individual identity. Rather, we talk of fungible tokens in terms of quantities, like chips in a casino. Tokens are simply a quantity. For me, the easiest way is to consider that 1 token is an amount equal to the basic unit defined by the contract.

The contract keeps a record of recognized accounts (token holders) and value held in the account. Each account is represented by an Ethereum public address, which is related to a public-key pair. The owner of the private key of this pair is the only person that can transfer, or authorise a transfer, of any part of its balance to another account.

Using ERC-20 Tokens

Any holder of an Ethereum account can receive and transfer ERC-20 tokens. Assuming you have some tokens in your account (for example if you are the contract’s creator), you can invoke the transfer method and tell the contract to move some of your tokens to another account.

To receive tokens, it is enough that some account transfers a value to the holder’s account. That is done by invoking a transfer method in the token’s contract. Any account that has never received a transfer is simply considered to have 0 balance. So, all you need to own ERC-20 tokens is to have an Ethereum address.

On some occasions, it is necessary for contracts to charge fees or commissions from users. But since only the owner of an account can authorise a transfer, this means the contract would have to rely on the good faith of the users to execute a transaction to pay those fees. Since this is not automatable, the standard specifies a way for users to authorise ahead of time an account (eg some contract) to withdraw tokens from this user up to a certain limit. This is called an allowance.

Another use case supported by this is to enable the Withdrawal pattern, in which a user withdraws funds from a contract. This is typical in financial operations, for example if a user has accrued interest.

When the time comes, the authorised account can rely on this allowance and take the due fees. The allowance is reduced by that amount. When it reaches 0, the account can not withdraw any more tokens. Allowances are not limited to contracts, they can be given to any account held by a person, but that is probably a less frequent use case.

ERC-20 Tokens as Currencies

Most of us have a basic understanding of what money is: it’s something with value, that we can trade for anything else without the inconveniences of bartering. 

Money serves at least 3 functions. It can be:

  • a means of exchange
  • a store of value
  • a unit of account

There are also several properties that any form of money must possess, in order to enable those functions:

  • Fungibility
  • Divisibility
  • Portability
  • Durability
  • Limited Supply and Value stability
  • Acceptability and Recognizability

Despite its simplicity, the ERC-20 standard enables all these properties.

Fungibility

There are no individual tokens in ERC-20. Each contract defines a token type, and a unit. Since balances are transferred electronically, by debiting one account and crediting another, fungibility is automatic: any transfer of, say, 100 units is equivalent to any other transfer of 100 units.

Divisibility

A currency is not of much use if it only has one denomination. At the time of writing, if you could only spend Bitcoins in units of 1 BTC, you’d be limited to buying high-end cars or similarly-valued luxuries.

In order to be useful, a currency must be tradeable in multiples of basic units of different sizes (eg £5, £1, 20p). In the case of ERC-20 tokens, the handling contract can specify the minimum unit that is tradeable, as a fraction of the main unit. For example, AVT, like ETH, can be traded in multiples of 0.000000000000000001 (18 decimal places). It’s like having an infinite number of banknote sizes at your disposal.

Portability and Durability

Tokens can easily be transferred by invoking the appropriate method in a smart contract. A token balance can be transferred between any two users of the contract, immediately and across the world without more inconvenience than accessing a virtual wallet. This makes the token extremely portable.

It is also durable. As long as the chain exists and the smart contract is not destroyed, the value held under each account does not disappear and is accessible at any point. The current balance of an account can also be checked at any point by querying the Ethereum blockchain via programmatic means or appropriate chain explorers.

Limited Supply

An ERC-20 contract must define the maximum value of tokens ever issued in total. This ultimately confers stability to the token’s value: if supply could be increased easily, inflation would destroy the token’s unit value.

Admittedly, this is a point of contention. Fiat currencies are not capped since the removal of the Gold standard, and some crypto-tokens (for example Ether itself) don’t have a hard cap in place. But in the crypto-community at large, there is a strong current of belief that a hard cap on supply, like Bitcoin does, is better for the preservation of value in opposition to the ability to eternally deflate a currency that we can see in fiat currencies.

Acceptability and Recognizability

In a viable currency, it must be possible to quickly assess a currency piece’s value and authenticity. Banknotes can be scanned for forgeries, and their value is prominently displayed.

For an ERC-20 token, these properties are enforced by the handler smart contract. If a payer offers to transfer a certain balance, the contract rejects the transfer if the payer does not have enough balance, and the transfer’s authenticity is guaranteed by cryptographic operations.

ERC-20 Standard Definitions

The ERC-20 standard is very simple. It defines how a Fungible Token can be created by a dedicated handler smart contract  by implementing particular functions.

Mandatory Functionality

All ERC-20 contracts must specify the following information:

  • the maximum supply of the currency, measured in minimum units. The contract must never be able to increase the total circulation supply past this value.
  • the balance of the token held by any Ethereum account. That is, how many minimum units of this token are exclusively controlled by the account.

Besides, the contract must support:

  • transferring a number of tokens from one account to another.
  • letting an account holder specify an allowance to another account.

The contract must support two types of transfer.

  1. the account holder submits a transactions to transfer value. This can only send funds from the sender’s account to the destination account.
  2. a transaction submitter can initiate a transfer between any two accounts. To do so, the submitter must have an allowance authorising it to move funds from the sender account.

Optional Functionality

Besides the above, a token’s contract may define the following information:

  • the currency’s name (eg Aventus) and symbol (eg AVT)
  • how many decimal places the minimum tradeable unit represents.

Summary

As you can see, ERC-20 is very simple, but it is also very powerful. It is the standard at the basis of the DeFi explosion, and the reason is easy to see. It provides a uniform way to write new tokens that can be traded in exactly the same way. 

With ERC-20, it is extremely simple to create a new project that is to be financed by crypto-tokens and then let them be traded in exchanges. Although this is not the only reason why you’d want to create a transferrable token, it is an impactful one.

I hope you enjoyed reading about ERC-20. In the next posts in this series, I will go over NFT tokens and their support in Ethereum. Stay tuned, and don’t forget to Like and Share if you’ve enjoyed this post.

2 thoughts on “Fungible Tokens in Ethereum: ERC-20”

  1. Pingback: Fungible and Non-Fungible Tokens in Blockchains • Coder's Errand

  2. Pingback: Non-Fungible Tokens in Ethereum: ERC-721 • Coder's Errand

Leave a Reply