How to mint ERC20 tokens using Open-Zeppelin

How to mint ERC20 tokens using Open-Zeppelin

Table of contents

As the continuous buzz going on for NFTs, Have you had a thought on how these NFTs are created? In this blog, I am gonna explain a wide range of topics that will answer your questions like

  1. What is an ERC20 Standard?

  2. What are NFTs and their uses?

  3. How to use OpenZeppelin's smart contracts

ERC20: ERC20 stands for "Ethereum Request for Comment-20". It is a standard that should be followed by a fungible token. These fungible tokens can represent anything in the real world like a plot of land, a collectable, fiat currencies etc. ERC20 is commonly used to mint NFTs.

OpenZeppelin smart contracts: The token should implement the necessary functions to match the ERC20 standard. OpenZeppelin is a popular open-source framework for writing smart contracts on the Ethereum blockchain. One of the contracts included in the OpenZeppelin library is the ERC20 contract, which is a standardized interface for implementing tokens on the Ethereum blockchain. These rules include functions for transferring tokens, checking balances, and other common operations.

Let's begin with the steps to write a simple smart contract that mints ERC20 Tokens.

Project Setup:

For this project, we will use the hardhat framework to interact and deploy our smart contract in the goerli-testnet. First, you need to have Nodejs on your computer. You can download and install the latest version of Node.js from the official website (nodejs.org/en).

Type the following command in your terminal to know whether Nodejs is installed or not

node --version

Open a new folder and type the following command in your terminal to install hardhat.

yarn add hardhat

The hardhat will be installed and a file named "package.json" will be created.

yarn hardhat

This will start the build of the boilerplate for the hardhat code. Select the sample project using Javascript option for a sample project.

Now In the folder named contracts. Create a file named "Token.sol".

Now we need to install OpenZeppelin's library to use the ERC20 standard. Paste the below command in your root directory to install the library.

yarn add @openzeppelin/contracts
// SPDX-License-Identifier: MIT
pragma solidity ^8.6.0;
import "@openzeppelin/contracts/token/ERC721/ERC20.sol";

contract goldcoin is ERC20{
    constructor() ERC20("GoldCoin","GC"){
            _mint("10000000000000000000000",msg.sender);
    }    
}

The first line indicates the license identifier.

The second line indicates the solidity version that is being used.

Next import the "ERC20.sol" file from the openZeppelin library. This file consists of functions that a token should follow to be used as an NFT or cryptocurrency etc.

By using Inheritance we will be using the functions in ERC20.sol file in our contract.

I have named my token "GoldCoin"** which has the symbol GC. This contract will initially mint 10,000 ERC20 tokens. The reason for these excess zeroes is that solidity does not support decimals. we need to either override the decimal function in the ERC20.sol file or include the default 10^18 zeroes behind your actual count. Refer OpenZeppelin's docs to know more about this. The keyword msg.sender refers to the address that will be deploying the contract. _mint function is used to mint the tokens. The 10,000 ERC20 tokens will be deposited in the deployer's account.

Hope you gained some knowledge about the questions asked at the beginning of the blog. The next update will be on how to deploy it in a testnet and verify the tokens deposited in our account using Metamask.

To know more about follow me on Twitter for more updates.