Deploy and verify an ERC-20 token

Step 1

First weโ€™re going to create a new folder and install hardhat:

$ mkdir mock-token$ cd mock-token
$ npm init
$ npm install --save-dev hardhat

Now letโ€™s bootstrap a hardhat project. Select the option "Create an empty hardhat.config.js":

$ npx hardhat 

Letโ€™s also create a couple of folders for our files:

$ mkdir scripts contracts

Now install some dependencies to our project:

$ npm install --save-dev @nomiclabs/hardhat-ethers ethers @nomiclabs/hardhat-waffle ethereum-waffle chai
$ npm install --save-dev @nomiclabs/hardhat-etherscan

Step 2

Letโ€™s now create a first Solidity contract at contracts/Token.sol:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
 
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
 
contract MockToken is ERC20 {
    constructor() ERC20("Mock Token", "Mock") {
        _mint(msg.sender, 1000000 * 10**decimals());
    }
}

We need to install openzeppelin library to our file:

$ npm install @openzeppelin/contracts

Try to compile the contract with:

$ npx hardhat compile

Step 3

We should prepare our hardhat.config.js file as:

require("@nomiclabs/hardhat-etherscan");
require("@nomiclabs/hardhat-waffle");
 
const PRIVATE_KEY = "PRIVATEKEY";
 
module.exports = {
    solidity: "0.8.2",
    defaultNetwork: "fuji",
    networks: {
      mainnet: {
        url: `https://api.avax.network/ext/bc/C/rpc`,
        accounts: [PRIVATE_KEY]
      },
      fuji: {
        url: `https://api.avax-test.network/ext/bc/C/rpc`,
        accounts: [PRIVATE_KEY]
      }
    },
    etherscan: {
      apiKey: {
        fuji: "avascan" // apiKey is not required, just set a placeholder
      },
      customChains: [
        {
          network: "fuji",
          chainId: 43113,
          urls: {
            apiURL: "https://api.avascan.info/v2/network/testnet/evm/43113/etherscan",
            browserURL: "https://testnet.avascan.info/blockchain/c"
          }
        }
      ]
    },
};

Step 4

Now letโ€™s add the deploy script. To keep it simple weโ€™ll simply reuse the deploy script from the Hardhat tutorial, so letโ€™s create the file scripts/deploy.js:

const { ethers } = require("hardhat");
 
async function main() {
    const [deployer] = await ethers.getSigners();
 
    const Token = await ethers.getContractFactory("MockToken");
    const token = await Token.deploy();
    console.log("Contract address:", token.address);
}
 
main()
    .then(() => process.exit(0))
    .catch((error) => {
        console.error(error);
        process.exit(1);
    });

After wrote our script, we can call deploy file:

$ npx hardhat run scripts/deploy.js --network fuji
$ npx hardhat verify --contract contracts/Token.sol:MockToken --network fuji <contractaddress>

Last updated