How to deploy smart contract on conflux network using Hardhat
In this tutorial, we'll guide you through deploying a simple "Hello World" smart contract on the Conflux eSpace testnet. You'll learn how to write the contract, deploy it using deploy.js
.
Prerequisites for Deploying Smart Contracts on the Conflux Network Using Hardhat
Before you start deploying smart contracts on the Conflux Network, ensure you have the following prerequisites in place:
Node.js
npm (Node Package Manager)
Hardhat
Basic Understanding of Solidity
Note: For RPC URL AND CHAINID Please refer the chainList: https://chainlist.org/?search=conf&testnets=true
Step 1: Writing the Hello World Contract
First, let's write the Solidity contract. Create a file named HelloWorld.sol
:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract HelloWorld {
string public greeting;
constructor() {
greeting = "Hello, World!";
}
function setGreeting(string memory _greeting) public {
greeting = _greeting;
}
function getGreeting() public view returns (string memory) {
return greeting;
}
}
This contract includes a message that can be updated and retrieved.
Step 2: Setting Up deploy.js
In the scripts
directory, create a new file deploy.js
and add the following code:
const hre = require("hardhat");
// const fs = require('fs');
async function main() {
const Helooworld = await hre.ethers.getContractFactory("HelloWorld")
const hellooworld = await Helooworld.deploy();
await hellooworld.deployed();
console.log("HelloWorld deployed to:", hellooworld.address);
// fs.writeFileSync('./config.js', `export const marketplaceAddress = "${nftMarketplace.address}"`)
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Step 3: Configure Hardhat
Create and configure your hardhat.config.js
file to connect to the Conflux network. Here’s an example configuration:
require ('@nomiclabs/hardhat-waffle');
module.exports = {
solidity: "0.8.10",
defaultNetwork: "confluxTestnet",
networks:{
hardhat:{},
confluxTestnet: {
url: "https://evmtestnet.confluxrpc.com " || "",
chainId:71 ,
accounts: ['Your Private Key']
},
}
};
Note: Replace Private key with yours account private key
Step 6: Deploy Your Contract
Deploy your smart contract to the Conflux network with the following command:
npx hardhat run scripts/deploy.js --network confluxTestnet
If everything is configured correctly, you will see the deployment process in the console output, including the deployed contract address.
Now you can go to the explorer and check the stats and contract deployment on testnet explorer.
Here's the Link: https://evmtestnet.confluxscan.io/address/0x2f9eb56e3b8e7208d5562feb95d1bc5ef432f232
Additional Resources
For a detailed walkthrough, you can check out my YouTube video and the full code repository on GitHub https://github.com/Vikash-8090-Yadav/Conflux-Tutorial/tree/main/HelloWorld.
By following these steps, you can easily deploy smart contracts on the Conflux network using Hardhat. Happy coding!
Feel free to ask any questions or share your experiences in the comments below.