ERC-20

:::hint{type="info"}

📜 TL;DR:

  • This tutorial guides you through deploying an ERC-20 token using Hardhat, a comprehensive Ethereum development environment. Hardhat is recommended for its powerful features, but you can use any Ethereum development tool.

  • Ensure Node.js, Hardhat, and Solidity are set up correctly for effective ERC-20 token development and deployment. :::


🏁 Prerequisites

  1. Download VSCode or any other IDE

  2. Install Node.js


📚 Tutorial

1. Initialize Your NPM Project

  • In your project directory, initialize a Node.js project

npm init -y

2. Install Hardhat & Ethers.js Plugin

  • Install Hardhat along with the Ether.js plugin, and the OpenZeppelin contracts library.

npm install --save-dev hardhat @nomiclabs/hardhat-ethers ethers @openzeppelin/contracts

3. Create a HardHat Project

a) Inside your Node.js project, start a Hardhat project

npx hardhat init

b) Select Create an empty hardhat.config.js


4. Add Folder

In the root directory of your project, create contracts and scripts folders:

mkdir contracts && mkdir scripts

5. Write Your Contract

  • In the contracts folder, create a file named MyToken.sol . This will be your ERC-20 token contract. Here's a basic example:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MyToken is ERC20 {
    constructor(uint256 initialSupply) ERC20("MyToken", "MTK") {
        _mint(msg.sender, initialSupply);
    }
}
  • This code defines a simple ERC-20 token with an initial supply and basic ERC-20 functionalities.


6. Compile Your Contract

npx hardhat compile 

7. Secure Your Private Key for Deployment

a) Export your Private Key from MetaMask:

  • Open MetaMask, select your account icon, and go to Account Details

  • Select Show private key

  • Enter your password.

  • Select Confirm

  • Select the unlock button to reveal your password.

b) Create an .env file in the root directory of your project and add your private key.

PRIVATE_KEY=your_exported_private_key

:::hint{type="danger"} Ensure this file is never shared or committed to version control. :::

  • Sample .gitignore code below

node_modules/
.env
artifacts/
cache/

c) Install dotenv package

npm install dotenv

8. Configure Hardhat for the Testnet

a) Open hardhat.config.js in your project.

b) Configure Hemi Network

  • Add the required modules at the top of the config file

  • Add the Hemi Network settings inmodule.exports. Ensure you include the network's URL, Chain ID, and the accounts array with your private key (stored in an environment variable for security).

Here's an example configuration:

/** @type import('hardhat/config').HardhatUserConfig */
require('dotenv').config()
require("@nomiclabs/hardhat-ethers");

module.exports = {
  solidity: "0.8.20",
  networks: {
    hemi: {
      url: "https://testnet.rpc.hemi.network/rpc",
      chainId: 743111,
      accounts: [`0x${process.env.PRIVATE_KEY}`],
    },
  }
};

9. Write a Deployment Script

In the scripts folder, create a file named deploy.js to write a script for deploying your contract.

const { ethers } = require("hardhat");

async function main() {
    const [deployer] = await ethers.getSigners();
    const initialSupply = ethers.utils.parseUnits("1000", "ether");

    const Token = await ethers.getContractFactory("MyToken");
    const token = await Token.deploy(initialSupply);

    console.log("Token deployed to:", token.address);
}

main().catch((error) => {
    console.error(error);
    process.exit(1);
});

This script is deploying MyToken with an initial supply (customize the supply as needed).


10. Deploy the Contract

npx hardhat run scripts/deploy.js --network hemi

If the deployment is successful, you will see messages indicating the token was deployed to

Token deployed to: 0x5fc5c2265E4f77E63e82f7F10FE803d04Cc53D82

To view the details of your deployed contract, enter the contract address from your success message into the HEMI testnet explorer. This will provide you with information about the contract's transactions and state.

Contract details we just deployed:

Last updated