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 the Ethereum network is ether. Still, it can also support other tokens that can operate as currencies, represent shares of a company, loyalty points, gold certificates, etc.
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. First, once a smart contract is deployed, it cannot be changed, so errors made cannot be rectified, which could be disastrous. For instance, a bug inside your contract’s code could make the 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 to 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 that 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 “balance of” method, which has to return the number of tokens a given address has.
Then there are the two transfer methods – “transfer,” which takes a certain amount of tokens from the total supply and gives them to a user, and “transfer from,” which can be used to transfer tokens between 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 a 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, forcing 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, you have to send some ether to the token contract to buy some tokens. But some people tried sending some other ERC-20 tokens instead. If the contract were not designed with this in mind, your tokens would be lost. To solve this, the community is working to extend the ERC-20 standard with the ERC223 standard. This warns token creators of the risks and offers some methods to find their way around them.