Explaining ERC20
You could be the next Satoshi Nakamoto
Okay, maybe not as god-tier, but you could definitely bring yourself to be in the general realm of his coding abilities.
A distant, distant portion of that realm — but in the general vicinity.
I created my own cryptocurrency. I made a video about it as well 🔽
In this article, I’ll explain the actual code required for creating an ERC20 Token, but more importantly, a behind-the-scenes look into the ERC20 Standard on the Ethereum network and how it’s used.
My crypto-spidey senses are tingling with excitement. Are yours?
Alright, first things first — what is ERC20?
To begin with the bare basics, Ethereum is a blockchain platform that is capable of storing transactions and contains a virtual machine that runs smart contracts.
Woah woah woah — virtual machine? Smart contract?
Let’s take a step back for a moment here.
There are two steps to understanding the process here: implementation and execution.
A smart contract is code that facilitates the exchange of stores of value. Its relationship to ERC20, specifically ERC20 Tokens, is that it creates them. This is the implementation.
All ERC20 Tokens come from a smart contract.
In order to get ERC20 Tokens, a user will send Ether (more on this later) to the smart contract, which will output a given number of tokens.
These smart contracts are used to create tokens, but also manage transactions of the token while keeping track of the balances various token holders have at any given time.
They are also based on IFTTT logic ▶️ If This, Then That logic
Simply, this is a string of conditional statements. Something like your settings for a smart home device, for example, has logical settings that are analogous to IFTTT.
The actual execution of the tokens, now that they’ve been created (post-implementation), is done on the EVM.
The EVM is the Ethereum Virtual Machine; it is a network of nodes and is a Turing complete software.
I think Turing completeness is an interesting concept, so I’ll delve into it a little bit here, though all you really need to know about Turning completeness is that it describes a system that is composed of data-manipulation rules.
Alan Turing, who was one of the most notable mathematicians and computer scientists of the 20th century, developed the idea of a machine that could run any program. This was as opposed to the machines of the time which could only run one algorithm for one specific output at the time.
The first theoretical computer if you will.
So, when we say that a programming language is Turing complete, we mean that using this language any application could be developed. If it is not Turing complete, it is restricted in solving any computational language.
Solidity is one such example. Therefore EVM, which runs on the network supporting smart contracts, which are coded using solidity, is called Turing complete. It is capable of running any program with enough time and memory.
Perhaps the most notable feature of Ethereum is its DApps. To better explain, let’s take the example of a trip to the movies.
You drive to a mall and decide to visit the movie theatre. The tickets were paid for using money, right? Well, once you entered the movie theatre, you got in using the ticket. The mall is the Ethereum network, the theatre is the DApp, the money is Ether and the tickets are ERC20 tokens.
The rent that is being paid by the movie theatre to the mall complex is analogous to developers paying the Ethereum network in Ether using the blockchain to host their DApps.
When Ethereum began to explode with its ability to support DApps, developers were flocking to the platform.
This is when a ton of ICO’s (Initial Coin Offerings) was launched.
An ICO is essentially the cryptocurrency equivalent to crowdfunding. These are the simplest way for developers to get the funding they need. However, because of the huge popularity of ICOs and its success in funding projects, many different tokens were created as a means of transferring value.
The issue with various types of tokens being created is that each causes a reinvention of the wheel in order to be compliant because each token contract was entirely different.
Ruhani, what do you mean by “compliant”?
Each new token created was unique; different functions and capabilities. Because of this, if the token was to become available on an exchange or wallets, these services would have to write custom code in order to talk to this unique token’s contract. This created the issue of interoperability.
Without some sort of rubric or basic criterion on which these tokens could be based, supporting various tokens in the community and on other platforms would have been very difficult. To better explain, I’ll compare this to creating a credit card.
Most credit cards now have a black swiping strip, some have a chip on the front, etc. There is a generalized format; security code, expiration date, and owner name are all in specific, standard positions so that things like scanners and cameras can recognize them.
Now imagine someone decided to use a QR code rather than that black swiping stripe. Stores would not accept the card because it would be incompatible with the prolific infrastructure.
This is where the ERC20 Standard came in; it is a set of rules and regulations that create a blueprint for all Ethereum based tokens. All ICO’s had to be built under this standard, allowing them to interact with each other easily. Now, most smart contracts are ERC20 compliant, making for transparency between contracts, accounts, and wallets; each knows exactly what each token is and what to do with it.
With the standard, things like exchanges and wallets only need to implement code once in order to recognize this standard of token. It helps the external users to find user balances and transfer funds with blockchain authority. The standard has also made it ridiculously easy for anyone really, to deploy their own contract and create an ERC20 token.
Currently, Etherscan hosts over 36,000 ERC20 tokens — don’t be fooled. Many of these are fake. Though, over 4 billion dollars were raised by selling tokens in an ICO in 2017.
The standard solved a massive problem and allowed blockchain-based marketplaces to tailor to a standardized collection of commands in communicating with a singularly based token.
It has simplified interaction rules between tokens and between buyers and sellers — the purchase guidelines and regulations.
Of course, there are issues with the ERC20 standard; users may implement the required factors to the extent that they wish. For example, in order to actually receive tokens, one must send Ether to a token contract.
Some users attempted to send ERC20 tokens-to get-like-more ERC20 tokens. In this case, their input tokens were lost. By December of 2017, it’s estimated that over 3 million were lost because of this singular flaw.
For this reason, the ERC20 Standard is being expanded by the community, who are working on the ERC223 Standard.
For now, though, let’s take a closer look at the key aspects of the code.
Recall that all ERC20 tokens are created by a contract. There are three optional functions the contract might include
string public constant name
i.e. Bitcoin
2. string public constant symbol
i.e. BTC
3. uin8 public constant decimals
i.e. 8 decimal points ▶️ smallest unit of Bitcoin is the Satoshi = 1 hundredth of a millionth Bitcoin (1 Satoshi = 0.00000001 BTC)
The most important part of the contract, however, are the six mandatory functions. These are the basis of the ERC20 Token transfers and the ERC20 Standard.
1. totalSupply - this is the method defining how many will be generated in totality2. balanceOf - the total tokens that a given address (user) has3. transfer - take a certain number of tokens from total supply and gives to another user4. transferFrom - transfers token between two users with pre-existing balances5. approve - verifies contract can give user a certain number of tokens given totalSupply6. allowance - verifies if a user has enough balance to send certain amount to another user
Another important feature defined in the contract is the following two events:
event Approvalevent Transfer
Events are Solidity’s method of allowing clients (e.g. your application front-end) to be notified of specific occurrences within the contract. These events are invoked when users are granted the right to withdraw tokens from an account after they are transferred.
When coding the token, we being with mapping. This creates a database where anyone can see the balance of the tokens. It also gives the creator (owner) all the tokens to begin with.
mapping (address => uint256) balances;mapping (address => mapping (address => uint256)) allowed;
The first mapping object here, balances, holds the token balance for owner accounts. The second mapping object, allowed, has the accounts that have been approved to withdraw from a given account as well as the granted withdrawal sum.
In order to set up the number of tokens in the ICO at token creation, the simplest method is to set the totalSupply and begin by assigning all the tokens to the contract owner — the account deploying the token contract.
Transferring tokens to other accounts involved the transfer function to move numTokens to a receiver. The require statement verifies that the accounts in question have adequate funds for the transaction.
I hope this technical account of ERC20 Standard and ERC20 Tokens provided some insight into the world of Ethereum. Getting a sufficient understanding of the workings behind technology is critical to ever hoping for technological improvement.
P.S. stay tuned for more on that ‘improvement’ piece 😉
If you enjoyed this article, be sure to give it a few 👏s and follow me on Medium! Feel free to connect with me on LinkedIn