Hemi
DiscordGithub
  • 🏠Main
    • Welcome to the Hemi Docs
    • Getting Started
      • Developer Quickstart
      • Enthusiast Quickstart
      • Miner Quickstart
    • Network Details
  • ⛰️Foundational Topics
    • The Architecture
      • Consensus and Security Protocols
      • Ethereum Rollups
        • Pros & Cons
        • Decentralized Rollups
      • Sequencer Consensus
        • Proof-of-Stake (PoS)
        • PoS-Only Pitfalls
        • PoS Solutions
      • Proof-of-Proof (PoP)
        • Proof-of-Proof Consensus & Bitcoin Finality
        • Proof-of-Proof vs. Merged Mining
        • Running a PoP Mining
      • Tunnels
        • Ethereum Tunnel
        • Bitcoin Tunnel
      • Ethereum Virtual Machine (EVM)
      • Blocks
      • Transactions
      • Gas
    • Nodes & Clients
      • Node Guides
    • Wallet Support
  • 📖How-To Tutorials
    • Using Hemi
      • Wallet Setup
        • EVM Wallet Setup
        • BTC Wallet Setup
          • Switch Bitcoin Networks
      • Tunnel from Ethereum
        • Tunnel ERC20s via Native Tunnel
        • Tunnel ERC20s via 3rd Party
          • Tunnel via Stargate
      • Tunnel from Bitcoin
        • Tunnel BTC via Native Tunnel
        • Tunnel BTC via 3rd Party
      • Stake
      • Developer Tooling
        • Set Up a Safe Wallet
        • Create a Capsule
      • PoP Mining
        • CLI PoP Miner
          • (Testnet) Add tHEMI to MetaMask
          • (Mainnet) Add PoPPoints to Metamask
        • Web PoP Miner (deprecated)
    • Developing on Hemi
      • General
        • HelloWorld.sol
        • Deploy an ERC-20 Token
      • hVM & hBK
        • Using the hBK Demo App
        • Get Bitcoin Balance with Remix
  • ⚙️Building Bitcoin Apps
    • Introduction
    • Hemi Virtual Machine (hVM)
      • Motivation
      • Feature Summary
      • Deploy on hVM
    • Hemi Bitcoin Kit (hBK)
      • Overview
      • hBK Smart Contract
  • ⚙️Tooling
    • viem
    • Contract Addresses
    • Contract Verification
    • Data Indexing
    • Oracles
  • 📝Incentives
    • Points
    • Grants
    • Retroactive Funding
    • One-Off Spends
  • ⚡Additional Resources
    • Partners
    • FAQ
    • Official Links
    • Brand Kit
  • 📨Send Feedback
    • Bug Report
    • Contact Us
Powered by GitBook
On this page
  • 🏁 Prerequisites
  • 📚 Tutorial
  • Video
  • 1. Create A Project Directory
  • 2. Initialize Your NPM Project
  • 3. Install Hardhat & Ethers.js Plugin
  • 4. Create a HardHat Project
  • 5. Add Folder
  • 6. Write Your Contract
  • 7. Compile Your Contract
  • 8. Secure Your Private Key for Deployment
  • 9. Configure Hardhat for the Testnet
  • 10. Write a Deployment Script
  • 11. Deploy the Contract

Was this helpful?

Edit on GitHub
Export as PDF
  1. How-To Tutorials
  2. Developing on Hemi
  3. General

Deploy an ERC-20 Token

PreviousHelloWorld.solNexthVM & hBK

Last updated 3 months ago

Was this helpful?

📜 TL;DR:

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

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

  • Earn and track Hemi for completing this tutorial!


🏁 Prerequisites

  1. Download (or any other IDE)

  2. Install or update it to


📚 Tutorial

Video

1. Create A Project Directory

  • Create a folder on your computer and name it TestToken.

  • This folder will serve as your project directory, containing all the elements and code needed to deploy your ERC-20 token.


2. Initialize Your NPM Project

  • In your TestToken project directory, open a terminal window and initialize a Node.js project

npm init -y

3. 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

4. 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


5. Add Folder

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

mkdir contracts && mkdir scripts

6. 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.


7. Compile Your Contract

npx hardhat compile 

8. Secure Your Private Key for Deployment

Your private key provides access to your wallet and your funds. Never share your private key with anyone. Ensure this file is never shared or committed to version control.

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) Install dotenv package

npm install dotenv

c) Run the command touch .env to create an .env file in the root directory of your project.

touch .env

d) Run the command nano .env to open the CLI editor

nano .env

e) Add your private key to the .env file.

PRIVATE_KEY=your_exported_private_key

9. 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://rpc.hemi.network/rpc",
      chainId: 43111,
      accounts: [`0x${process.env.PRIVATE_KEY}`],
    },
  }
};

10. 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).


11. 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

Contract details we just deployed:

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

📖
Hardhat
Foundry
points
VSCode
Node.js
current version
MetaMask Wallet Setup
Tunnel ETH to Hemi
Hemi Testnet explorer