Comment on page
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.
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.
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

Hardhat init
- Select
Create a Typescript project
with↓
andEnter
. - Specify Hardhat Project root or press enter for already specified path.
- Input
y
for yes andn
for no for adding a.gitignore
file. - Press
Enter
forDo you want to install this sample project's dependencies with npm (@nomicfoundation/hardhat-toolbox)?
Now, you will have the following folder structure:

Screenshot
- 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) => {
console.error(error);
process.exitCode = 1;
});
- Add the XDC testnet network to metamask (if you are using XDCPay, you can skip this step):

Screenshot 2022-09-13 at 12 31 31 PM
- Add the XDC mainnet network to metamask (if you are using XDCPay, you can skip this step):

Screenshot 2022-09-13 at 12 35 05 PM
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.
XINFIN_NETWORK_URL="enter-network-rpc-url-here"
XINFIN_PRIVATE_KEY="enter-your-private-key-here"
Depending on which network (Apothem or XinFin) you are deploying to you will need to use one of these Network URL's:
XINFIN_NETWORK_URL=https://erpc.xinfin.network
APOTHEM_NETWORK_URL=https://erpc.apothem.network
- Dont know how to get your private key? Open your XDCPay wallet extension and click on the three dots on the top-left. This will open a popup.
Click on the
Export Private key
inside the popup.
Screenshot 2022-09-18 at 12 31 05 AM
Enter the password that you used while creating the account.

Screenshot 2022-09-18 at 12 31 20 AM
Copy your private key. This key will be used to sign transactions when deploying the contract through hardhat.

Screenshot 2022-09-18 at 12 31 35 AM
- 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. Yourharhat.config.ts
should look like this:
import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
import * as dotenv from "dotenv";
dotenv.config();
const config: HardhatUserConfig = {
solidity: "0.8.17",
networks: {
xinfin: {
url: process.env.XINFIN_NETWORK_URL,
accounts: [process.env.XINFIN_PRIVATE_KEY!]
}
}
};
export default config;
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.