How To deploy your first smart contract on the XDC Network using Hardhat

Overview

Hardhat is and Ethereum development environment which can be used for editing, compiling, debugging and deploying your smart contracts and dApps.

What you will learn

This guide aims at teaching how to create a smart contract using Hardhat and deploying it on the XDC Network.

Steps

  • Install and set up Hardhat

  • Write and Deploy contract on the XDC Network

  • Check the deployment status on Xinfin scan.

Installation Prerequisites

Metamask wallet (Extension link) or XDC Pay Wallet (Extension link)

Don't know how to create a wallet? Click here

Node.js. (Download link)

Setup

First, we need to set up our Hardhat project. Open terminal and follow these commands.

Make a folder with you project name and go to the project directory.

mkdir xdc-hardhat
cd xdc-hardhat

Initialize the project and install Hardhat.

npm init --yes
npm install --save-dev hardhat

In the same directory, run:

npx hardhat
  • Select Create a Javascript project or Create a Typescript project according to your requirement.

  • Specify Hardhat Project root or press enter for already specified path.

  • Specify y for yes and n for no for adding a .gitignore

  • Press enter for Do you want to install this sample project's dependencies with npm (@nomicfoundation/hardhat-toolbox)?

Note: If you are on windows, you should install this:

npm install --save-dev @nomicfoundation/hardhat-toolbox

Write Smart Contract

  • In the contract folder, create a new file and write your contract inside it. For reference, see the code for Pizza.sol below.

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

  contract Pizza {
      uint256 public PIZZA_SIZE;
      uint256 public slices;

      constructor(uint256 _pizzaSize) {
          PIZZA_SIZE = _pizzaSize;
          slices = 0;
          slices += PIZZA_SIZE;
      }

      function eatSlice() public {
          require(slices > 0, "No Slices Left. Lets Bake a new Pizza!");
          slices -= 1;
      }

      function bakeNewPizza() public {
          require(slices == 0, "There still slices from a previous Pizza!");
          slices += PIZZA_SIZE;
      }
  }
  • To compile the contract, write the following in terminal:

npx hardhat compile

If this returns errors, double-check your contract and rectify any issues.

Deployment

To write the script to deploy the contract, create deploy.js in scripts folder, if it is already not there. Copy the following code in the deploy.js.

const hre = require("hardhat");

async function main() {
  // make sure to change the name of your contract
  const Pizza = await hre.ethers.getContractFactory("Pizza");
  // 4 in the bracket is to give the value to the parameter(_pizzaSize) in the constructor of the smart contract contract.
  const pizza = await Pizza.deploy(4);

  await pizza.deployed();

  console.log("pizza contract address:", pizza.address);
}

// Call the main function and catch if there is any error
main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });
  • To add the XDC testnet network to metamask, do the following (you can skip this step if using XDCPay):

  • To add the XDC mainnet network to metamask, do the following (you can skip this step if using XDCPay):

Setup .env file

Make sure you do not push this file to github.

  • Make a .env at the root of the project to store the private key and network url.

XINFIN_NETWORK_URL="enter-network-rpc-url-here"
XINFIN_PRIVATE_KEY="enter-your-private-key-here"

Click on the Export Private key inside the popup.

Enter the password that you used while creating the account.

Copy your private key. This key will be used to sign transactions when deploying the contract through hardhat.

  • To be able to import env file variables, please install dotenv from terminal.

npm install dotenv
  • Open the hardhat.config.js file. Now we will add the network url and private key of your wallet to this file, so that you can deploy your contract. Your harhat.config.js should look like this:

require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config({ path: ".env" });

const XINFIN_NETWORK_URL = process.env.XINFIN_NETWORK_URL;
const XINFIN_PRIVATE_KEY = process.env.XINFIN_PRIVATE_KEY;

module.exports = {
  solidity: "0.8.9",
  networks: {
    xinfin: {
      url: XINFIN_NETWORK_URL,
      accounts: [XINFIN_PRIVATE_KEY],
    },
  },
};

Final Step

After writing code for the setup and contract, go back to terminal. Make sure you are in your project directory and type:

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

Voila! 🎉 You did it.

Copy the contract address or transaction hash(incase of testnet) and check it's deployment status on the block scan.

For mainnet: XinFin Scan For testnet: Apothem Scan

Last updated