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.
Initialize the project and install Hardhat.
In the same directory, run:
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 .gitignorePress 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:
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.
To compile the contract, write the following in terminal:
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
.
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.
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.
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:
Final Step
After writing code for the setup and contract, go back to terminal. Make sure you are in your project directory and type:
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