Configuring a Hardhat Project for Deploying a Smart Contract on the CrossFi Blockchain: A complete guide.
Deploying a smart contract on the CrossFi blockchain using Hardhat is a systematic process. This guide walks you through the complete setup, from initializing your Hardhat project to deploying the contract on the CrossFi blockchain.
Prerequisites
Ensure you have the following installed:
Node.js
Yarn or npm
Metamask wallet with enough XFI token on CrossFi testnet
Your wallet private key
See guide on how to claim XFI faucet here
Step 1: set up your hardhat project
Create project directory and navigate to the directory.
mkdir crossfi-hardhat-project
cd crossfi-hardhat-project
Install hardhat by running the following commands in your project directory;
yarn init -y
yarn add --dev hardhat
or
npm init -y
npm install --save-dev hardhat
Initialize Hardhat;
npx hardhat init
Once you do this, you will get the prompt below;
Select typescript project and install the necessary dependencies as seen below;
Step 2: Update hardhat.config.ts
Get the Alchemy API key and modify the configuration file to include the CrossFi network:
import { HardhatUserConfig, vars } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
const ALCHEMY_API_KEY = vars.get("ALCHEMY_API_KEY");
const config: HardhatUserConfig = {
solidity: "0.8.28",
networks: {
crossfiTestnet: {
url: `https://crossfi-testnet.g.alchemy.com/v2/${ALCHEMY_API_KEY}`,
accounts: vars.has("PRIVATE_KEY") ? [vars.get("PRIVATE_KEY")] : [],
},
},
};
export default config;
We did not configure etherscan API key because contracts verification on CrossFi blockchain is not publicly available right now due to the Explorer API being under active development. So we will just deploy the contract
Step 3: configure the environment variable
To configure the variables, use the following hardhat CLI;
$ npx hardhat vars set ALCHEMY_API_KEY
✔ Enter value: ********************************
$ npx hardhat vars set PRIVATE_KEY
✔ Enter value: ********************************
To displays a configuration variable's value:
$ npx hardhat vars get ALCHEMY_API_KEY
1234abcd1234abcd1234abcd1234abcd
Step 4: Deploy the contract
We are just going to deploy the Lock.sol contract that came with the hardhat project and run the deployment script on the CrossFi network:
npx hardhat ignition deploy ignition/modules/Lock.ts --network crossfiTestnet --verify
Once your contract is deployed, you will see the details;
You can then copy the contract address to https://test.xfiscan.com/dashboard and check the details of the just deployed contract.
Summary
In this guide, we initialized a Hardhat project, configured the CrossFi Testnet, and deployed the Lock.sol
smart contract.