Table of Content

Using HardHat IDE for Compiling and Deploying the Smart Contracts

Lesson Objectives

By the end of this lesson, you will be able to:

  • Use HardHat IDE for writing, compiling and deploying smart contracts on BNB Chain.
  • Verify deployed smart contract using HardHat IDE.


In this lesson, we provide a detailed demo on how to create, compile and deploy a simple smart contract on the BNB Smart Chain Testnet using Hardhat. Remember that, BNB Smart Chain is the component of the BNB Chain ecosystem that is equipped with the smart contract programmability and hence any smart contract or dapps are in essence deployed on the BNB Smart Chain.

What is Hardhat

Hardhat is a development environment to compile, deploy, test, and debug smart contracts.


For this hands-on guide, readers are encourged to perform the specified tasks along for a better understanding. Before starting off with the practical demo, it is necessary to make sure you have the following software requisites installed.

Sofware Prerequisites

  • Node.js v10+ LTS and npm (comes with Node)
  • Git
  • Create an empty project npm init --yes
  • Once your project is ready, run npm install --save-dev hardhat to install Hardhat.
  • Install hardhat toolbox npm install @nomicfoundation/hardhat-toolbox
  • To use your local installation of Hardhat, you need to use npx to run it (i.e. npx hardhat).

Create A Project

  • To create your Hardhat project run npx hardhat in your project folder to intialize your project.
  • Select Create an empty hardhat.config.js with your keyboard and hit enter.
$ npx hardhat
888    888                      888 888               888
888    888                      888 888               888
888    888                      888 888               888
8888888888  8888b.  888d888 .d88888 88888b.   8888b.  888888
888    888     "88b 888P"  d88" 888 888 "88b     "88b 888
888    888 .d888888 888    888  888 888  888 .d888888 888
888    888 888  888 888    Y88b 888 888  888 888  888 Y88b.
888    888 "Y888888 888     "Y88888 888  888 "Y888888  "Y888

Welcome to Hardhat v2.10.1

√ What do you want to do? · Create a JavaScript project
√ Hardhat project root: ·  Project-Directory
√ Do you want to add a .gitignore? (Y/n) · y

You need to install these dependencies to run the sample project:
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
  npm install --save-dev "hardhat@^2.10.1" "@nomicfoundation/hardhat-toolbox@^1.0.1"

Project created

See the file for some example tasks you can run

Give Hardhat a star on Github if you're enjoying it!

When Hardhat is run, it searches for the closest hardhat.config.js file starting from the current working directory. This file normally lives in the root of your project and an empty hardhat.config.js is enough for Hardhat to work. The entirety of your setup is contained in this file.

Create Smart Contract

You can write your own smart contract or download the BEP20 token smart contract template, place it in the contracts directory of your project and remane it as BEP20Token.sol. BEP-20 is a token standard on BNB Smart Chain (BSC) that extends ERC-20, the most common Ethereum token standard. BEP-20 is essentially BNB Smart Chain's standard for fungible tokens.

Configure Hardhat for BSC

  • Go to hardhat.config.js
  • Update the config with bsc-network-crendentials.

const { mnemonic } = require('./secrets.json');

// This is a sample Hardhat task. To learn how to create your own go to
task("accounts", "Prints the list of accounts", async () => {
  const accounts = await ethers.getSigners();

  for (const account of accounts) {

// You need to export an object to set up your config
// Go to to learn more

 * @type import('hardhat/config').HardhatUserConfig
module.exports = {
  defaultNetwork: "mainnet",
  networks: {
  	localhost: {
      url: ""
    hardhat: {
    testnet: {
      url: "",
      chainId: 97,
      gasPrice: 20000000000,
      accounts: {mnemonic: mnemonic}
    mainnet: {
      url: "",
      chainId: 56,
      gasPrice: 20000000000,
      accounts: {mnemonic: mnemonic}
  solidity: {
  version: "0.8.9",
  settings: {
    optimizer: {
      enabled: true
  paths: {
    sources: "./contracts",
    tests: "./test",
    cache: "./cache",
    artifacts: "./artifacts"
  mocha: {
    timeout: 20000

Note the above config file that it requires mnemonic to be passed in for Provider, this is the seed phrase for the account you'd like to deploy from. Create a new secrets.json file in root directory and enter the 12 word mnemonic seed phrase of your Metamask wallet. To get the seedwords from Metamask wallet you can go to Metamask Settings, then from the menu choose Security and Privacy where you will see a button that says reveal seed words.

Sample secrets.json

    "mnemonic": "Your_12_Word_MetaMask_Seed_Phrase"

Compile Smart Contract

To compile a Hardhat project, change to the root of the directory where the project is located and then type the following into a terminal:

npx hardhat compile

Deploy Smart Contract on BSC Network

  • Copy and paste the following content into the scripts/deploy.js file.
async function main() {
  const [deployer] = await ethers.getSigners();

  console.log("Deploying contracts with the account:", deployer.address);

  console.log("Account balance:", (await deployer.getBalance()).toString());

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

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

  .then(() => process.exit(0))
  .catch((error) => {

  • Run the following command in root of the project directory to run the deploy script. To select the network use the --network flag with the name of the network as specified in the hardhat.config file. In our case, its testnet.
$  npx hardhat run --network testnet scripts/deploy.js

Sample Output

$ npx hardhat run --network testnet scripts/deploy.js
Deploying contracts with the account: 0x27cf2CEAcdedce834f1673005Ed1C60efA63c081
Account balance: 100721709119999208161
Token address: 0xbF39886B4F91F5170934191b0d96Dd277147FBB2

Remember your address, transaction_hash and other details provided would differ, Above is just to provide an idea of structure.

🎉 Congratulations! 🎉

You have successfully deployed BEP20 Smart Contract. Now you can interact with the Smart Contract. You can check the deployment status here: or by searching through your contract address.

Verify with Hardhat

Hardhat has an Etherscan plugin: Hardhat Etherscan plugin that integrated with contract verification service.

Install the plugin

npm install --save-dev @nomiclabs/hardhat-etherscan

Configure the EthereScan plugin in hardhat.config.js

  • Step1: Add require("@nomiclabs/hardhat-etherscan");
  • Step2: Add your Bscscan API key. Register and obtain your API key from .
  • Step3: Always remember to set the solidity compiler version to match what was used for deploying the smart contract.

Warning: Keep your API key as secret and never it commit to version control

// hardhat.config.js
const { mnemonic, bscscanApiKey } = require('./secrets.json');

 * @type import('hardhat/config').HardhatUserConfig
module.exports = {

  networks: {
    testnet: {
      url: ``,
      accounts: {mnemonic: mnemonic}
    mainnet: {
      url: ``,
      accounts: {mnemonic: mnemonic}

  etherscan: {
    // Your API key for Etherscan
    // Obtain one at
    apiKey: bscscanApiKey 
  solidity: "0.8.9"

Verify Command

Warning: Remove any unnecessary contracts and clear the artifacts otherwise these will also be part of the verified contract.

Run the following command:

npx buidler verify --network mainnet DEPLOYED_CONTRACT_ADDRESS "Constructor argument 1"


$ npx hardhat  verify --network testnet 0xbF39886B4F91F5170934191b0d96Dd277147FBB2
Nothing to compile
Compiling 1 file with 0.5.16
Successfully submitted source code for contract
contracts/BEP20Token.sol:BEP20Token at 0xbF39886B4F91F5170934191b0d96Dd277147FBB2
for verification on Etherscan. Waiting for verification result...

Successfully verified contract BEP20Token on Etherscan.


This demo guided you through the basics of creating and deploying a simple smart contract using the Hardhat IDE. It also provides step-by-step guide on how to verify your deployed smart contract. This tutorial uses testnet, however, the exact same instructions and sequence will work on the mainnet as well.

Table of Content