How to Deploy Your First Smart Contract on the XDC Network Using Hardhat and TypeScript


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

TypeScript is a strongly-typed programming language that builds on TypeScript.

What you will learn

This guide will teach how to create a smart contract using Hardhat with TypeScript and how to deploy it on XDC Network.


  • 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)


First, you'll need to set up your hardhat project. Open the 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 with hardhat:

npx hardhat
  • Select Create a Typescript project with and Enter.

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

  • Input y for yes and n for no for adding a .gitignore file.

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

Write a Smart Contract

Now, you will have the following folder structure:

  • In the contract folder, create a new file and write your contract inside it. In the following example, we will show the code for Pizza.sol for reference:

// 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, check your contract and rectify it.


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

import { ethers } from "hardhat";

async function main() {
  // make sure to change the name of your contract
  const Pizza = await 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 myContract = await Pizza.deploy(4);

  await myContract.deployed();

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

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
  process.exitCode = 1;
  • Add the XDC testnet network to metamask (if you are using XDCPay, you can skip this step):

  • Add the XDC mainnet network to metamask (if you are using XDCPay, you can skip this step):

Set up your .env file

Make sure you do not push this file to github.

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


Depending on which network (Apothem or XinFin) you are deploying to you will need to use one of these Network URL's:


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 your terminal.

npm install --save-dev dotenv
  • Open the hardhat.config.ts file. You can now add the network url and private key of your wallet to this file so that you can deploy your contract. Your harhat.config.ts should look like this:

import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
import * as dotenv from "dotenv";


const config: HardhatUserConfig = {
  solidity: "0.8.17",
  networks: {
    xinfin: {
      url: process.env.XINFIN_NETWORK_URL,
      accounts: [process.env.XINFIN_PRIVATE_KEY!]

export default config;

Final Step

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

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

Voila! 🎉 You did it.

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

For mainnet: XinFin Scan For testnet: Apothem Scan

Last updated