Creating the ERC20 Token contract

We will start by creating a .sol file in your contracts directory. Let's name this file MoolahCoin.sol:

  1. We'll begin writing our contract by first declaring the Solidity compiler version with the help of the following command:
pragma solidity ^0.5.2;
  1. Next, we will import the dependent sample contract templates from the openzeppelin library, as shown in the following code block:
import "openzeppelin-solidity/contracts/token/ERC20/ERC20Detailed.sol";
import "openzeppelin-solidity/contracts/token/ERC20/ERC20Capped.sol";
import "openzeppelin-solidity/contracts/ownership/Ownable.sol";

The imported contracts implement the following functionalities:

  • ERC20Detailed and ERC20Capped are sample contracts that allow us to encapsulate the inner workings of the ERC20 token. They are also used to initialize the parameters used to describe the ERC20 token during contract deployment.
  • ERC20Detailed allows us to define the token name, symbol, and the number of decimals the token is divisible up to.
  • ERC20Capped allows us to define the total supply, the total number of tokens that will be issued in the contract's lifetime. It also implements a mint method. The mint method permits us to issue new tokens and transfer them to any Ethereum address on the network.
  • Ownable is a sample contract that allows us to implement access controls on the smart contract. 
  1. The contract name is the same as our token name. The contract inherits the ERC20Detailed, ERC20Capped, and Ownable smart contracts from the openzeppelin library:
contract MoolahCoin is ERC20Detailed, ERC20Capped, Ownable {
  1. Next, as shown in the following code block, we will create a constructor for our token contract. During contract deployment, it initializes the descriptive parameters of our payment token:
constructor()
ERC20Detailed("Moolah Coin", "MC", 4)
ERC20Capped(10000000000)
payable public {}
}

Our constructor inherits the constructor methods for the contracts ERC20Detailed and ERC20Capped from the Open Zeppelin library. The following code demonstrates the ERC20Detailed constructor:

//ERC20Detailed Constructor
constructor (string memory name, string memory symbol, uint8 decimals) public {
_name = name;
_symbol = symbol;
_decimals = decimals;
}

The ERC20Detailed constructor sets the name of the token as Moolah Coin, the token symbol as MC, and the number of decimals the token is divisible up to (7 decimal places after zero).

The following code demonstrates the ERC20Capped constructor:

//ERC20Capped constructor
constructor (uint256 cap) public {
require(cap > 0, "ERC20Capped: cap is 0");
_cap = cap;
}

The ERC20Capped constructor initializes the total supply of Moolah Coin (10,000,000,000 MC tokens in the token's lifetime) when the contract is deployed.

Putting it all together, MoolahCoin.sol will look something like this:

pragma solidity ^0.5.2;

import "openzeppelin-solidity/contracts/token/ERC20/ERC20Detailed.sol";
import "openzeppelin-solidity/contracts/token/ERC20/ERC20Capped.sol";
import "openzeppelin-solidity/contracts/ownership/Ownable.sol";

contract MoolahCoin is ERC20Detailed, ERC20Capped, Ownable {

constructor()
ERC20Detailed("Moolah Coin", "MC", 4)
ERC20Capped(10000000000)
payable public {}

}
  1. We will compile our contract using Truffle. Copy and paste the contract file (MoolahCoin.sol) to the directory /contracts in your truffle workspace. Before bringing the Truffle console online, check whether your Ganache blockchain is running and the Solidity compiler version is set to 0.5.2 in the Truffle configuration file.

To change the Solidity compiler version, open the truffle-config.js file. Under module.exports, add the compiler version and settings as shown in the following code block. Make sure the tags are uncommented and enabled. Also, make sure the development network tags are uncommented and enabled and the port tag is set to 8545. Refer to the following code block:

module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: "*",
}},
compilers: {
solc: {
version: "0.5.2",
settings: {
optimizer: {
enabled: false,
runs: 1000,
},
}}}}
  1. Now, let's run the Truffle console with the help of the following command:
truffle console
  1. Next, in the command line, we will enter compile to compile the contract as shown in the following code:
truffle(development)>> compile
You can check out the original ERC20 smart contract standard approved by the Ethereum Improvement Program here (https://theethereum.wiki/w/index.php/ERC20_Token_Standard). OpenZeppelin implements an encapsulated and safer version of the same contract. It's always recommended that you read the inherited template contracts in the openzeppelin contracts directory. You can locate the contract by following the folder path given in the import statement.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.77.98