ERC20 Token Standard: A Beginners Guide
This was originally written on shivendramisra.com/blog
While most of us are fascinated by technological applications such as ICOs and Smart Contracts, very few of us understand what’s really happening. One of the main reasons we have had an ICO mania is because of the presence of ERC20, which makes it super easy to deploy tokens with a wide reach.
Who doesn’t want free money?
Let’s dive into the topic for a detailed discussion!
The cryptocurrency and blockchain community is flooded with talks and discussions about ICOs. The investors are throwing money at them like crazy while the regulators are scratching their heads to figure out a way to regulate them.
Nonetheless, I don’t think the market cares, and after all, the show must go on, right?
If we have a look at the statistics, ICOs have raised more in the first 3 months of 2018 than they were able to raise in the whole of 2017!
This is tremendous growth for ICOs. Whilst the price of Bitcoin and other altcoins dwindles, the ICOs show no signs of slowing down.
Why are ICOs popping up at such a high rate?
Because it’s too easy!
With websites like Token Factory, you just have to enter basic details — Total Supply, Name, Decimal Places, and Symbol — of your token and boom!
Your token is created!
The question is, which blockchain platform is being used to create such tokens?
The most popular blockchain for ICOs is Ethereum. Almost 45 out of the top 100 cryptocurrencies (by market cap) are made on top of the Ethereum Blockchain.
Though Ethereum is only 4 years old, by enabling people to have such a wide access to creating tokens and launching ICOs, its growth has been through the roof.
However, there is one more feature, rather, a standard, which has helped the widespread adoption of such tokens. That is why I am writing this post.
This standard is known as ERC20 which is basically a template, on the basis of which you can create your own token.
Think of MS Word or Google Docs. You can either create your own document or you can go with a template. Ethereum has the same ideology.
You can either create your own token from scratch or you can go with the ERC standard (template) to make changes according to your needs and customize your token the way you want.
Now that you understand the idea at a broad level, let’s dive into the details
All tokens are essentially Smart Contracts.
The function of each smart contact is to create the token, handle the transactions and keep a check on the balances of the user.
Just like any contract contains certain terms and conditions, these smart contracts contain a set of functions, which define the behavior of the token, written in a programming language.
The functions can be defining the total supply of the coin, checking the balance of a user, approving a transaction etc.
For those who have programmed before, and are familiar with Object Oriented Programming, you can think of ERC as a simple interface, which has to be implemented by developers to make their own token.
Let us have a look at the contract;
For non-coders, this can be pretty overwhelming. But let me explain it in simple terms.
The image shows the interface or template (as we discussed above) for making a token.
The first function is totalSupply() which gives a number as an output. This number is the total supply of the token.
The second function is balanceOf() which takes the address of an owner as the input and gives the balance of the users as output.
The allowance() function takes in the address of the tokenOwner and the spender and gives as output, the number of tokens allowed to be transferred from a given address by another given address.
The transfer() function simply takes in the address of the receiver and the funds which are to be sent to the account. It then gives a boolean output value which is either true or false. True means that the amount was transferred successfully whereas false means the amount could not be transferred.
Then we have the approve() function. You can approve someone to spend from your account. This function takes the address of the spender along with the coins to be spent as the input and tells whether the transaction was a success or a failure.
Last but not the least, we have the transferFrom() function. It takes in three inputs — the address of the sender, the address of the receiver, as well as the number of tokens to be transferred. It again gives an output to notify the success or the failure of the transfer.
In addition to these 6 mandatory functions, there are 2 events also — Transfer() and Approval().
The Transfer() event produces the details of the transaction which resulted in the movement of tokens from one address to the other, whereas the Approval() event gives the details of the approval of tokens.
These events are triggered when a relevant action, pertaining to the 6 functions above, takes place on the blockchain.
In the image, you can also see three variables at the bottom of the contract. These are optional information such as name, symbols and, decimals, which the token may or may not include.
But why do we need ERC20 Tokens? Why can’t just everyone make their Token Contracts in their own style?
Let us go back to our example of Google Docs. Let us say you with your friends have to submit a report on a particular topic. Now if you all make your reports with a template provided by Docs, the person assessing your report will know exactly where to find the information and what is the structure of the report.
On the other hand, if all of you prepare your reports with different structures, then it will be relatively difficult to assess and evaluate them.
Similarly, while making your own Token Contract in your style is possible, it can have certain disadvantages.
If the code structure of all the tokens is different, then exchanges, wallets, smart contracts and other developers will have to write custom code to communicate with each type of token.
This will take a lot of time which means that for your token to be widely adopted, third parties would need a significant amount of time to change their code to support your token as well.
Who wants that right? With ERC20, as soon as you launch your token, third parties can already support the token as they have support for Ether as well as other ERC20 tokens.
This means your token can instantly be listed on exchanges and users can trade them with their existing wallets. Thus, it establishes interoperability.
The Dark Side of ERC20
So far we have learned about what ERC20 token standard is and how it can make the life of developers easier.
There is a large community of Ethereum developers which one can always take advantage of in case of any difficulty.
The companies wanting to launch their own tokens do not have to go through the long process of making their own blockchain, thus, saving time, effort and money.
Just as there are two sides to every coin, ERC20 Tokens are also not a one-size-fits-all solution.
I would like to mention two main bugs (errors) here — The first one is the batchOverlofw bug and the second one causes Unexpected Money Losses.
The batchOverflow Bug
Simply put, this bug helped attackers to possess a lot of tokens out of nowhere.
It is a ‘classical integer overflow issue.’ Here is a code snippet of the function that was faulty
Many exchanges suspended such tokens in April this year, which were affected by the batchOverflow Bug
For those of you who are super interested in the technical details, check this out.
Unexpected Money Losses
As mentioned above, all tokens are smart contracts and these contracts define the behavior of the token. These contracts are designed to work with only specific types of tokens.
So if you send ether to a smart contract which is not built to work with ether, it should return your funds back to you and the transaction would not be completed, right?
This is where the tragedy happens with ERC20 tokens. It is not designed to work in this way. When you send a ‘non-compliant’ coin to the smart contract, the transfer function in that contract produces weird results like permanent loss of funds of the sender.
There is no warning to the user which says, “WARNING: if you send funds to this contract, they may be lost”, which should be there in this case.
Around $3 million have been lost due to this bug as of December 2017.
There must be a solution to this bug, you say? Well, there is!
ERC223 Token Standard
The ERC 223 standard improves upon the ERC20 standard and is backward compatible too. This means that all the contracts and services interacting with ERC20 tokens will be able to interact with ERC223 tokens as well without any changes.
One of the main advantages of ERC223 over ERC20 is that it allows the developers to handle incoming transactions and provides a possibility to avoid losses arising due to the inability of a contract to work with the incoming token, thus, solving the occurrence of unexpected losses with ERC20.
The ERC 223 standard deserves a full blog post which will be coming out soon.
Despite the solutions being present, the problem still persists as the Ethereum Foundation is reportedly promoting the use of ERC20 and many other developers and ICOs are using ERC20 to make their life easier to make money rather than addressing the problem at hand.
Though the ERC223 token standard is not widely adopted, its benefits should slowly convince the developer community to shift from ERC20 to ERC223.
ERC20 Token Standard is a huge catalyst in the growth of ICOs and the number of projects on the Ethereum Blockchain.
Though it provides numerous benefits to developers, exchanges, wallets and other smart contracts, it is not yet perfect.
It is about time that the developer community realizes this fact and shifts to other token standards.
Having said that, this is definitely not the end and you will surely see future posts of various other standards such as ERC223, ERC777 and ERC1155 too.
Clap, Clap, Clap 👏 🙌 !!
I am a Blockchain and Cryptocurrency enthusiast and I’m highly interested in solving real-world problems through technology and Entrepreneurship. Apart from writing, coding and reading books, I am also passionate about fitness and sports. You can check more of my posts here. Reach out to me on LinkedIn and Twitter, I’m waiting for your message!