Creating an ERC20 Token.

Grace Mugoiri
3 min readJul 9, 2021

What is ERC20 Token? In full Ethereum Request for Comment, the 20 is just a standard number that was proposed. It is a guideline to create a token in the Ethereum network.

Little ERC20 history.

ERC20 came about because everyone who needed to write a token had to write their own templates which brought about so much confusion as one had to read the whole documentation of each different token that was created. To make it easier for the community, the users standardized ERC20 as the token that would be used to guide all other tokens across the world. As of 2015 reports.

The code itself…

In order for these guidelines to be adhered to, there are 6 mandatory functions written in Solidity language which you basically copy/paste :-). Then there are 3 optional ones and others which you can add if need be.

Below is a code snippet I wrote to explain what the mandatory functions do.

6 mandatory functions to use when writing an ERC20 token. (.sol)

totalSupply — total number of tokens that exist. Takes no arguments and returns a uint.

balanceOF — gets the balance of the specified token. Takes an address as an argument and returns a uint.

transfer transfer tokens to a specified address. Takes address and uint as key arguments and returns a bool(true or false) factor.

approve — allows the chosen address to spend amount as specified on behalf of msg.sender in token form. Takes two arguments, address and uint and they returnsa bool.

transferFrom transfer tokens from one address to another. Three arguments are taken from this function, 2 addresses(to and from) and uint. Returns a bool.

allowance — checks the number of tokens that an owner allowed to a spender. This one allows two addresses as well as arguments and returns a uint.

3 Optional functions.

When writing an ERC20 token, these other functions are sensible but not mandatory. They are:-

  • name(eg GeminiCoin)
  • symbol(eg GMNC)
  • separation(in decimals which range up to 18)

There can be additional functions as per ones liking like buyTokens, approveCall, etc.

SafeMath

SafeMath is a library with four main functions that can be very helpful when creating an ERC20 token and needs calculations.

  • sub
  • add
  • mul
  • div
code snippet of the four math functions.

It can be imported to your file from https://docs.openzeppelin.com/contracts/4.x/erc20 or write down the file itself and use it on your contract by Using SafeMath for uint method.

The functions can be used internal or private depending on the method you choose to use the library itself.

ERC20 Token’s purpose is to standardized the code for understanding purposes. It not only creates the tokens themselves but also manages transactions of the token as well as tracks each tokens holders’ balances.

--

--