Creating a Token Smart Contracts for the Blockchain Part 3

Now that we have the basic implementation of the ERC-20 token created, let's create a few smart contracts that we can use to add more functionality to the token smart contract we will be creating. At the end of this chapter, we will be ready to compile and deploy our token smart contract.

In the last chapter we created the ERC20.sol smart contract. This file imported the libraries and interface that we created and has all of the requirements of the ERC-20 token standard.

Let's take another look at the smart contracts that are required to write the token smart contract for this book once again.

As per the ERC-20 Token Standard:

Interfaces:

  • IERC20.sol // necessary

Libraries:

  • SafeMath.sol // necessary

  • Context.sol // necessary

  • Role.sol // plays a role with ERC20Mintable.sol

  • MinterRole.sol // plays a role with ERC20Mintable.sol

Contracts:

  • ERC20.sol // necessary

  • ERC20Detailed.sol // introduces additional capabilities

  • ERC20Burnable.sol // introduces additional capabilities

  • ERC20Mintable.sol // introduces additional capabilities

Contracts we will deploy:

  • ERC20Combined.sol // combines everything together

In this chapter, we will be adding additional functionality to our ERC-20 token smart contract with the ERC20Detailed.sol, ERC20Burnable.sol, and ERC20Mintable.sol smart contracts. Once we write the contracts mentioned, we will combined it all together with ERC20Combined.sol.

In addition to the contracts mentioned, we will also need to introduce a few more libraries so that the ERC20Mintable.sol smart contract works as desired.

ERC20Detailed.sol#

This contract implements the optional parameters of an ERC-20 token, including name, symbol and decimals.

To kick things off, let's create the contract declaration in a new file called ERC20Detailed.sol (in your /contracts folder). We will have to import the interface IERC20.sol because we want to ensure that contracts that inherit the ERC20Detailed contract also implement the methods in IERC20.sol.

Within the contract brackets we need to create three private variables name, symbol and decimals, and three public methods to read them.

A few notes on the decimals method:

  • Solidity can't handle decimals and performs all math required using fixed-point numbers

  • if decimals is equal to 2, a balance of 505 tokens should be displayed to the user as 505 / 10 ** 2

  • tokens usually use a value of 18 for decimals because it follows the relationship between wei and ether (1 ** 18 wei = 1 ether)

  • this is for display purposes only - does not affect the math of the contract (e.g. balanceOf, transfer, etc.)

The last thing we need for this contract is a constructor method, to initialize the variables we just created. It will take in two string values and a uint8 value so that we can update the variables that we are tracking in this contract. This method will run whenever the contract is deployed. Upon deploying, we will need to pass in the name, symbol, and decimals we want for this token.

Typically, constructors should be placed before the other methods of the smart contract so our final ERC20Detailed.sol contract will look like this: