ERC-20 tokens exist on the Ethereum platform, which consists of a blockchain that stores transactions, and a virtual machine capable of running smart contracts. It is important to understand that tokens rely on the Ethereum blockchain, they benefit from its technology, and are not independent. The native currency on Ethereum network is ether, but it can also support other tokens which can operate as currencies, represent shares of a company, loyalty point, gold certificates and so on.
A token is created by smart contracts that are also responsible for managing transactions of the tokens, and keeping track of each token holder’s balance.
To receive tokens one has to send some ether to the smart contract, which will then provide a certain amount of tokens in return. Therefore, when new tokens are created, a smart contract is written that can create a token, transfer them and keep track of people’s balances. Despite the process sounding very easy, it is quite risky. To begin with, once a smart contract is deployed, it cannot be changed, and so errors made cannot be rectified and that could be disastrous. For instance, a bug inside your contracts code could make token susceptible to theft or loss in any other way.
There is the problem of interoperability to consider. Since token contracts can be completely different from the other, exchanges have to write a custom code so that it can communicate with your contract and allow people to trade. The same thing goes for wallet providers. Such a system would make it extremely complex and time-consuming to support hundreds of tokens.
ERC-20 and its functions –
To overcome these a standard called ERC-20 (which stands for Ethereum Requests for Comments, and 20 is just the number they assigned for identification) was introduced. ERC-20 is a guideline which defines 6 mandatory functions that a smart contract should implement and three optional ones. To start with, you can optionally give the new token a name, a symbol, and you can control how dividable your token is by specifying how many decimals it supports.
The Mandatory Functions
The mandatory functions are a bit more complex. For starters, one has to create a method that defines the total supply of the token. When this limit is reached the smart contract will refuse to create new tokens.
Next is the “balanceOf” method which has to return the number of tokens a given address has.
Then there are the two transfer methods – “transfer” that takes a certain amount of tokens from the total supply and gives them to a user, and “transferFrom” which can be used to transfer tokens between any two users who have them. Finally, there are the “approve” and “allowance” methods. “Approve” verifies that your contract can give a certain amount of tokens to a user, taking into account the total supply. The “allowance” method is very similar except that it checks if one user has high enough balance to send a certain amount to someone else.
If you are familiar with object-oriented programming, you could compare ERC-20 to an interface. If you want your token to be an ERC-20 token you have to implement the ERC-20 interface, and that forces you to implement these six methods. Before there was the ERC-20 standard, everyone who wanted to create a token had to reinvent the wheel. This meant that each token contract was slightly different and exchanges and wallets had to write custom code to support your token. With ERC-20 however, exchanges and wallet providers only have to implement this code once.
Drawbacks of ERC-20 –
ERC-20 is a great standard that has propelled the use of tokens. But ERC-20 itself is not perfect. It is only a guideline and people are free to implement the required functions whichever way they like. That has led to some interesting problems, for instance, to buy some tokens you have to send some ether to the token contract. But some people tried sending some other ERC-20 token instead. If the contract was not designed with this in mind, your tokens will be lost. To solve this, the community is working to extend the ERC-20 standard with ERC223 standard. This warns token creators of the risks and offers some methods to find their way around it.