ERC 20 Introduction — Part 2

Dejan Jovanovic
TheBlockchainHub
Published in
5 min readJan 22, 2019

So How It Works

ERC-20 provides a formalized specification of the Token Smart Contract. Token Smart Contract is a smart contract that contains the map of account addresses and related balances. The balance represents a value that is defined by the contract creator. Here is the example of the smart contract balance:

When tokens are transferred from one account to another the token contract updates the balance of two accounts. For example, if from address 0x1F8…….02AA3h we transfer 10 tokens to address 0x2bb….9d8f11 it will result in balance being updated to following:

If token contract allows there are two ways how to change the total supply of tokens. As we have mentioned ERC-621 provides the specification for two methods: increase supplies (minting) and decries supplies (burning). If token contract has implemented increaseSupply(uint256 _value, address _to) method then following calling increaseSupply(100, 0x2ff….0004d21) will result in following balance update:

By calling method decreaseSupply(uint256 _value, address _to) total supply can be decreased. This is also called burning. So for example if we call decreaseSupply(70, 0xdff……aaf321) this will result in the following balance:

So, as we understand how balance of tokens works let explore how is ERC-20 implementation work. There are few items that we need to define before we start with implementation.

There are few attributes that needs to be defined:

string public constant namestring public constant symboluint8 public constant decimalsstring public version

First, we need to select token name. We need to understand that there is no central repository that will guaranty your token name uniqness. There is no restriction of how long the name can be but still keep it short as some wallets will truncate it anyways. For example, it can be “Hive Project Token”.

Token symbol is the symbol by which token contract will be known, for example “HVN”. It should be no longer than 3 to 4 characters.

Attribute decimals are common source of confusion. Depends on what is contract for value can ware. Let’s go through the cases:

1. If the smart contract is tracking number of software licenses that customers have then the decimal value is going to be 0 as the values that accounts have a re going to be round numbers. See the below example:

In this example total number of available licenses is 100. Owner of the contract holds the majority of licenses. As users purchases a license a single token will be transferred from the holding account (in our case 0x3h9..….87223A) to the purchaser.

2. Second example uses GoldToken, the token contract that represents ownership of physical gold. The token creator wants unit of to represent 1 kg of gold, but in the same time wants to allow users to hold and trade amounts of gold down to the gram level. In this case value of decimals is set to 3. Do not forget Ethereum doesn’t support decimal numbers so we deal with this on the code level.

So for total supply of 50 kg of gold represented (1 g per token * 50000 tokens). However, as decimals attribute is set to 3 the view to the users will be as follows:

3. And at last if you are creating utility token it is recommended to use 18 decimals. In some cases contract owners are using 8 decimals, but I will recommend to stay away from it. The idea of divisibility is to allow for token contract to represent fine grained decimal value.

In summary when selecting a suitable value for decimals following rules should be followed:

· Does the token contract represent an invisible entity, then set decimals to 0

· Does the token contract represent an item with a fixed number of decimals, then set decimals to that number?

· If neither of the above apply set decimals to 18

It is important to understand impact of decimal point on the token creation. Following equation shows the calculation of total supply of tokens that token contract is going to have:

And finally string public version attribute is there to track the version of the contract and to ensure that test results provided with a contract are the one that are correspondent to the version of the token contract.

The Blockchain Hub is an inclusive education and innovation hub, with a strong network of diverse alumni from many industry verticals. Would you like to add your voice to ours, and have your articles featured in The Blockchain Hub? Just send us a note at content@theblockchainhub.com

Disclaimer: The views and opinions expressed in this article are those of the author. They do not purport to reflect the policies, views, opinions or positions of The Blockchain Hub, any other agency, entity, organization, employer or company.

--

--

Dejan Jovanovic
TheBlockchainHub

Seasoned executive, business and technology leader, entrepreneur, blockchain and smart contract expert