1

ERC20 Tokens: A Comprehensive Origin Story

It is hard to put into words the sheer impact that ERC20 tokens have had on the crypto-world. They are single-handedly responsible for the billion-dollar ICO industries and they have played a big role in making cryptocurrencies more mainstream. Before we get into what they are and how they have impacted, a little history lesson is in order.

 

The Road to Smart Contracts

The blockchain technology and cryptocurrencies came to the forefront when an anonymous developer calling himself/herself/themselves Satoshi Nakamoto created Bitcoin. Bitcoin was revolutionary for a number of reasons. For the first time ever we had a currency system which was owned by the people. You can send a Bitcoin to anyone with a Bitcoin wallet without having to go through a bank.

For introducing the world to the blockchain technology, Bitcoin is commonly known as a “First-Generation Blockchain.” However, there is a huge restriction in the way Bitcoin conducts its transactions.

Turns out, that transactions need not always be so simple.

If you want to send some money to your friend in a simple one-on-one transaction, then Bitcoin will be ideal in this scenario. However, what if you want to send some money to your friend only when he finishes a certain amount of tasks in a particular deadline. How can you manipulate the transaction code to account for such complex transactions?

This is the question that Vitalik Buterin provided a solution to by creating Ethereum.

Ethereum and Smart Contracts

Vitalik Buterin realized that the blockchain technology had far more utility than just being a payment system. According to their website.

“Ethereum is a decentralized platform that runs smart contracts: applications that run exactly as programmed without any possibility of downtime, censorship, fraud or third-party interference. These apps run on a custom built blockchain, an enormously powerful shared global infrastructure that can move value around and represent the ownership of property.”

In simpler terms, Ethereum is planning to become a decentralized super-computer wherein anyone, anywhere can rent out some computational power and create decentralized applications (Dapps) which can run on top of the Ethereum platform.

So, how do you make these Dapps? For that, we need to look into Smart Contracts

Smart contracts are automated contracts. They are self-executing with specific instructions written on its code which get executed when certain conditions are made.

ERC20 Tokens: The Origin Story

You can learn more about smart contracts in our in-depth guide here.

Smart contracts are how things get done in the Ethereum ecosystem. When someone wants to get a particular task done in Ethereum they initiate a smart contract with one or more people.

Let’s go back to our previous section.

Smart Contracts help you code complicated transactional details in a simplified format, and they were conceptualized by Nick Szabo. According to him, the best real-life working example of the smart contract is a vending machine.

When you want something from the vending machine, you simply deposit your cash into the machine, then you choose what you want, and the machine gives it to you. You are directly interacting with the machine without any involvement from a third person. As long as you follow the process which is:

  • Depositing your money
  • Choosing your product

You will get what you ask for.

Suppose you and your friend Alice want to interact with each other using a smart contract.

The smart contract also works in the same way. You lock up some of your Ether (the Ethereum token) inside the smart contract, the condition being that as soon as Alice finishes up some tasks, the contract will unlock the funds and send it to her wallet.

Smart Contracts and ICOs

Smart contracts opened a whole new world for developers to create and innovate. However, these developers needed a way to finance their projects.

The solution? ICOs.

ICOs or Initial Coin Offerings are the cryptocurrency version of the Initial Public Offerings or IPOs. However, when compared to IPOs, ICOs are a lot more entrepreneur-friendly than IPOs for the following reasons

  • Firstly, it is far simpler to present your project in ICOs. All that you have to do is to present a whitepaper of your project.
  • Anyone can become invested in a project they are interested in by purchasing the tokens of that particular DAPP and become a part of the project themselves.

There is one more thing that you need to know before we get into how an ICO works. The developers need to give something in return for the investments. This “something” is called tokens.

 To give you a very wide, non-generalized definition, a token is a representation of something in its particular ecosystem. It could value, stake, voting right, or anything. A token is not limited to one particular role; it can fulfill a lot of roles in its native ecosystem.

So, what are these “roles” that the tokens can take?

  • Toll: A token can act as a gateway to the Dapp. Basically, in order to access the Dapp, you will need to hold the tokens.
  • Voting Rights: The tokens may also qualify the holders to have certain voting rights. Think of EOS, holding EOS tokens will allow you to vote for block producers.
  • Value Exchange: This is one of the more common roles of tokens within the ecosystem. Tokens can help create an internal economic system within the application.
  • User Experience Enhancement: The token can also enable the holders to enrich the user experience inside the confines of the particular environment. Eg. In Brave (a web browser), holders of BAT (tokens used in Brave) will get the rights to enrich customer experience by using their tokens to add advertisements or other attention based services on the Brave platform.
  • Currency: Can be used as a store of value which can be used to conduct transactions both inside and outside the given ecosystem.

Ok, so till now we have told you how smart contracts, ICOs, and tokens work. However, here is where we hit out the first obstacle. Let us take an example of an arcade.

The Video Game Arcade

ERC20 Tokens: The Origin Story

Think of an old-school video game arcade. What are all the steps that you need to take before you get to play your game:

  1. You take your fiat money and convert them into the arcade coins.
  2. You use the arcade coins to play games in the machines by inserting them inside the slot. The slots are designed to accept circular shaped coins.
  3. After you are done, you take the leftover coins, if any, and convert them to fiat money.

So, can you make the parallels between the arcade and smart contract platform?

The video game arcade machines are the Dapp while the arcade coins issued are your native tokens. You will need to be in possession of those coins in order to access the services of your Dapp.

There is one problem area of this system though.

In order the fit into the slot of the machine, the coins need to be of a specific dimension. Now, what if certain machines came about who didn’t accept the circular coin designs and instead preferred a squarish coin shape?

In order to run the business smoothly, the arcade owner must lay down a ground rule. All machines must be built to accept circular arcade coins. No other shape must be accepted.

That, in essence, is what ERC20 guideline does.

The Problem with Different Token Design

In the early days, it seemed that each and every ICO token was trying to “re-invent the wheel” as far as the execution was concerned. Each one of those tokens had their own list of functions. Now, this led to a whole host of new problems.

To create a healthy ecosystem on top of Ethereum, it is absolutely essential that the Dapps built on top of it can interact with one another seamlessly. However, what will happen if we have two tokens, say Token Alpha and Token Beta, and both of them have different smart contract structures?

For that to happen, the developers will need careful study both these contracts and map out exactly how these tokens are going to interact with each other.

Now, this doesn’t really bode well for scalability now, does it?

If there are 100 different tokens with 100 different contracts, then to narrow down on all the qualifications and conditions required to make sure that transfers can go through between all these tokens will need a humongous amount of complex calculations. Which is not an ideal scenario at all.

Something needed to be done and on November 19, 2015, Fabian Vogelsteller came up with an ingenious solution.

Enter ERC20 Tokens

ERC20 Tokens: The Origin Story

 

According to Wikipedia, ERC20 is a

list of rules that an Ethereum token has to implement, giving developers the ability to program how new tokens will function within the Ethereum ecosystem. The ERC-20 token standard became popular with crowdfunding companies working on initial coin offering (ICO) cases due to the simplicity of deployment, together with its potential for interoperability with other Ethereum token standards.”

To keep it simple, ERC20 is a guide of rules and regulations that will help create a blueprint for Ethereum-based smart contracts to create their tokens. The “ERC” stands for to Ethereum Request for Comment, while the number ’20’ is the number assigned to this request.

So, now that you know what ERC20 is, let’s dissect it even further and see what goes on behind the scenes.

Anatomy of ERC20 Tokens

They follow a list of rules so that they can be shared, exchanged for other tokens, or transferred to a crypto-wallet. The ERC20 standard consists of 3 optional rules and 6 mandatory rules.

The mandatory rules are as follows:

  • totalSupply
  • balanceOf
  • transfer
  • transferFrom
  • approve
  • allowance

On the other hand, the optional rules are:

  • Token Name
  • Symbol
  • Decimal (up to 18)

Let’s expand into the optional rules first before looking into the 6 mandatory rules.

The 3 Optional Rules

Even though it is not necessary to name your tokens, it is still important to give it an identity.  The name is declared like this:

string public constant name = “Token Name”;

Up next we have symbols. We simply cannot understate their importance. The perfect example of this is OmiseGO. People are far more familiar with OMG than OmiseGO. A catchy symbol helps with better branding. The way they are declared is:

string public constant symbol = “SYM”;

Finally, we have the divisibility which will help us determine the lowest possible value of the token. A divisibility of 0 will mean that the lowest value of the token is 1. A divisibility of 2, on the other hand, means its lowest value will be 0.01. The maximum number of decimal places allowed is 18. It is declared like this:

uint8 public constant decimals = 18;

Alright, now let’s go into the 6 rules.

The 6 Mandatory Rules

#1 totalSupply

[totalSupply] identifies the total number of ERC-20 tokens created. The purpose of this method is to determine the total number of tokens floating around the ecosystem.

The code looks like this:

contract MyERCToken {

uint256 _totalSupply = 1000000;

 

 function totalSupply() constant returns (uint256 theTotalSupply) {

    theTotalSupply = _totalSupply;

   return theTotalSupply;

 }

}

#2 balanceOf

The balanceOf function returns the number of tokens that a particular address, in this case, the contract owner, has in their account.

Code looks like this:

contract MyERCToken {

// Create a table so that we can map addresses

 // to the balances associated with them

mapping(address => uint256) balances;



 // Owner of this contract



 address public owner;

 

 function balanceOf(address _owner) constant returns (uint256 balance) {

 

 // Return the balance for the specific address



   return balances[_owner];

 }

}

#3 approve()

Once the balance has been checked, the contract owner can give their approval to the user to collect the required number of tokens from the contract’s address. The approve function also checks the transaction against the total supply of tokens to make sure that there are none missing or extra. In other words, it makes sure that counterfeiting is impossible.

Note: “msg.sender” is the address of the contract owner.

contract MyERCToken {

// Create a table so that we can map

 // the addresses of contract owners to

 // those who are allowed to utilize the owner's contract



 mapping(address => mapping (address => uint256)) allowed;

 

 function approve(address _spender, uint256 _amount) returns (bool success) {

   allowed[msg.sender][_spender] = _amount;



   // Fire the event "Approval" to execute any logic

   // that was listening to it



   Approval(msg.sender, _spender, _amount);

   return true;

 }

}

#4 transfer()

So, now that all the checking has been done and the contract knows that the user has the required amount of tokens needed to fulfill the transaction, the contract owner can send them tokens using the transfer() function.

This function lets the owner of the contract send a given amount of the token to another address just like a conventional cryptocurrency transaction., allows a certain number of tokens to be transferred from the total supply to a user account. Before the game can start, the players must receive their BLU from the dealer.

contract MyERCToken {

 mapping(address => uint256) balances;

 

 // Note: This function returns a boolean value

 //       indicating whether the transfer was successful

 

function transfer(address _to, uint256 _amount) returns (bool success) {



   // If the sender has sufficient funds to send

   // and the amount is not zero, then send to

   // the given address



   if (balances[msg.sender] >= _amount

     && _amount > 0

     && balances[_to] + _amount > balances[_to]) {



     balances[msg.sender] -= _amount;

     balances[_to] += _amount;



     // Fire a transfer event for any

     // logic that's listening

     

Transfer(msg.sender, _to, _amount);

       return true;

     } else {

       return false;

     }

  }

}

#5 transferFrom()

Hmmm..wait hold up.

We have already covered the transfer function, why do we have another?

Well, let’s take an example to see why transferFrom is such a brilliant addition to the ERC20 contract.

We all have to pay some amount of money every month like clockwork. It could be your rent, the bills, etc. You don’t really need to pay all these amounts hands-on by yourself. You can always set up an automatic payment system with your banks to take care of these payments.

That’s what transferFrom() allows you to do. It helps you automate payment transfers to a specific account.

Code:

contract MyERCToken {

 mapping(address => uint256) balances;

 

 function transferFrom(address _from, address _to, uint256 _amount) returns (bool success) {

   if (balances[_from] >= _amount

     && allowed[_from][msg.sender] >= _amount

     && _amount > 0

     && balances[_to] + _amount > balances[_to]) {

   balances[_from] -= _amount;

   balances[_to] += _amount;

   Transfer(_from, _to, _amount);

     return true;

   } else {

     return false;

   }

 }

}

Note: Even though it is one of the most popular tokens out there, the golem GNT token is not considered ERC20 because it doesn’t have the approve() and transferFrom() functions in its contract.

#6 allowance()

In order to carry out a transaction one of the most important data that the contract should know is the balance of the user. After all, the user should have the minimum amount of tokens required to do the transaction.

This is why, the ERC20 contract also includes the allowance() function. If the user doesn’t have the minimum required number of tokens, the function cancels the transaction.

Code:

function allowance(address _owner, address _spender) constant returns (uint256 remaining) {

   return allowed[_owner][_spender];

 }

Bringing The Code Together

So, now that we have seen how the individual functions work, let’s take a peek at the ERC20 token contract.

The code has been taken from GitHub.

pragma solidity ^0.4.15;



contract MyERCToken

{



 // Create a table so that we can map addresses

 // to the balances associated with them



 mapping(address => uint256) balances;



 // Create a table so that we can map

 // the addresses of contract owners to

 // those who are allowed to utilize the owner's contract



 mapping(address => mapping (address => uint256)) allowed;



 // In this case, the total supply

 // of MyERCToken is fixed, but

 // it can very much be changed

 uint256 _totalSupply = 1000000;

 // Owner of this contract



 address public owner;
 

function totalSupply() constant returns (uint256 theTotalSupply) {



   // Because our function signature

   // states that the returning variable

   // is "theTotalSupply", we'll just set that variable

   // to the value of the instance variable "_totalSupply"

   // and return it



   theTotalSupply = _totalSupply;



   return theTotalSupply;

 }

 

 function balanceOf(address _owner) constant returns (uint256 balance) {

   return balances[_owner];

 }

 

 function approve(address _spender, uint256 _amount) returns (bool success) {

   allowed[msg.sender][_spender] = _amount;

 

 // Fire the event "Approval" to execute any logic

   // that was listening to it



   Approval(msg.sender, _spender, _amount);

   return true;



 }

 

 // Note: This function returns a boolean value

 //       indicating whether the transfer was successful



 function transfer(address _to, uint256 _amount) returns (bool success) {



   // If the sender has sufficient funds to send

   // and the amount is not zero, then send to

   // the given address

 

 if (balances[msg.sender] >= _amount

     && _amount > 0

     && balances[_to] + _amount > balances[_to]) {

     balances[msg.sender] -= _amount;

     balances[_to] += _amount;



     // Fire a transfer event for any

     // logic that's listening



     Transfer(msg.sender, _to, _amount);

       return true;

     } else {

       return false;

     }

  }

  

  function transferFrom(address _from, address _to, uint256 _amount) returns (bool success) {

   if (balances[_from] >= _amount

     && allowed[_from][msg.sender] >= _amount

     && _amount > 0

     && balances[_to] + _amount > balances[_to]) {

   balances[_from] -= _amount;

   balances[_to] += _amount;

   Transfer(_from, _to, _amount);

     return true;

   } else {

     return false;

   }

 }

 

 function allowance(address _owner, address _spender) constant returns (uint256 remaining) {

   return allowed[_owner][_spender];

 }



 // Triggered whenever approve(address _spender, uint256 _value) is called.

 event Approval(address indexed _owner, address indexed _spender, uint256 _value);

 // Triggered when tokens are transferred.

event Transfer(address indexed _from, address indexed _to, uint256 _value);

}

 

Benefits of ERC20 Tokens

Now that we know what ERC20 is, what are the main benefits of using it?

  • Firstly, the convenience. As we have already stated earlier, if everyone was creating their own tokens with its own functions then it would have been an interoperability nightmare. Plus, saving those tokens in wallets and listing them on exchanges would have been a pain. Transfer of tokens could have broken contracts and made them vulnerable to hackers.
  • Since the ERC20 regulations present a proper blueprint for developers to follow. It is easy for them to come up with tokens instead of working from scratch.
  • An important factor that is critical for the overall valuation of the Ethereum network is the liquidity of these ERC20 tokens. If the projects on top of Ethereum are active and interacting with each other, then that is going to invariably bring more projects and more users to the Ethereum network.

Flaws of ERC20

Flaw #1: The transfer() bug

Even though ERC20 tokens have a lot of good qualities, they have garnered plenty of criticisms as well. In Ethereum, there are two kinds of accounts, externally owned accounts (EOA), controlled by private keys, and contract accounts, controlled by their contract code.

If you want to interact with another EOA account then you can use the transfer() function to send the required tokens. However, if you want to send the tokens to a contract account using the transfer() function, then you will be facing a bug which has led to the loss of nearly millions of dollars.

The big problem with the transfer() function is that once it is executed the recipient will not be notified of the transfer even if the transaction goes through successfully

A developer who goes by the name of “Dexaran” was the one who brought this to everyone’s attention.

According to him:

“Transaction of a token is a change in the internal variables of the contract (the `balance` of the sender will be decreased and the `balance` of the recipient will be increased).

As a result, if the recipient is a contract, users must transfer their tokens using the `approve` +` transferFrom` mechanism, and if the recipient is an externally owned account address, users must transfer their tokens via the `transfer` function. If a user will make a mistake and choose a wrong function then the token will get stuck inside contract (contract will not recognize a transaction). There will be no way to extract stuck tokens.

Does anyone of ERC20 token developers believe that users will never make such mistakes when developing their tokens?”

So, this makes a pretty good case for the approval() and transferFrom() combination right? However, there lies a problem as well.

It is an insecure operation and hence leads to the possibility of a double spent attack.

Flaw #2: The Entry Point is Too Low

Another big criticism is that ERC20 has made it too easy for people to create their own tokens. As a result of this, the market is flooded with needless and, even worse, scammy ICOs and tokens.

Future of ERC20 Tokens

While it is true that ERC20 tokens have been important for the growth of the crypto ecosystem, the fact remains that they may have overstayed their welcome. People are already experimenting with newer standards like ERC223, ERC 777 etc. but, as of right now, ERC20 isn’t going anywhere.

Dexaran points out the following reasons as to why ERC20 is still the preferred standard of choice:

  • Because of the criminal irresponsibility of token developers for their deeds.
  • Because Ethereum Foundation is still promoting the ERC20 token standard even when it is known to contains bugs. The same situation as it was with TheDAO previously. They need to say “Stop this now” but they will not.
  • Because the main reason for token development is fund grabbing, rather than product creation.
  • Because using a different standard will lead to higher network effects. This is not what we really need given that the Ethereum network already has scalability issues.

Change is always slow and it looks like we will definitely need to wait a little bit more before we can move onto other standards. However, let’s not make the mistake of “criminalizing” the ERC20 standard. It certainly deserves all the respect for the profound impact it has had on the crypto-space.

Like what you read? Give us one like or share it to your friends

10
1

Related Guides

Join Blockgeeks

Create an account to access our exclusive point system, get instant notifications for new courses, workshops, free webinars and start interacting with our enthusiastic blockchain community. Don’t miss out and join right now!

Already have an account? Sign In

Comments