Comment on page
How To deploy your first smart contract on the XDC Network using Hardhat
Hardhat is and Ethereum development environment which can be used for editing, compiling, debugging and deploying your smart contracts and dApps.
This guide aims at teaching how to create a smart contract using Hardhat and deploying it on the XDC Network.
- Install and set up Hardhat
- Write and Deploy contract on the XDC Network
- Check the deployment status on Xinfin scan.
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
orCreate a Typescript project
according to your requirement. - Specify Hardhat Project root or press enter for already specified path.
- Specify
y
for yes andn
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
You will have a folder structure as shown below:

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

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

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. Yourharhat.config.js
should look like this:
require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config({ path: ".env" });