How to Deploy Your First Smart Contract on the XDC Network Using Hardhat and TypeScript
Overview
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.
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, 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:
Initialize the project with hardhat:
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)?
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:
To compile the contract, write the following in terminal:
If this returns errors, check your contract and rectify it.
Deployment
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
:
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.
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:
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:
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