DEV Community

Cover image for Hardhat : Deployment and Upgradable Contract πŸ‘·
Independence DEV
Independence DEV

Posted on

Hardhat : Deployment and Upgradable Contract πŸ‘·

Deploying your smart contracts in a professional way is important to maintain them properly. Hardhat is the perfect solution for that!

Installation & configuration

To install and initialize a HardHat project, use these commands :
npm install hardhat
npx hardhat

You will have a file "Greeter.sol" in the "contracts" folder.
You can compile this Smart Contract with :
npx hardhat compile
In the "hardhat.config.js" file we will set the path of the artfifacts (Smart contracts compiled) and the BNB Smart chain testnet network for the future deployments.
paths: {
artifacts: './src/artifacts',
},
networks: {
bsctestnet:
{
url: "https://speedy-nodes-nyc.moralis.io/c96c432c75339d23af5d7364/bsc/testnet",
chainId: 97,
accounts: [secret.key]
}

In the "secret.json" file I put the private key of my wallet.
{
"key": "myPrivateKey"
}

Deployment of a smart contract

To deploy the Smart Contract, we will have to create a script in the "scripts" folder. We have the "sample-script.js".
const hre = require("hardhat");
async function main() {
const Greeter = await hre.ethers.getContractFactory("Greeter");
const greeter = await Greeter.deploy("Hello, Hardhat!");
await greeter.deployed();
console.log("Greeter deployed to:", greeter.address);
}

Use this command to launch it :
npx hardhat run scripts/sample-script.js --network bsctestnet

Interaction with the smart contract

Now we deployed the Smart Contract and have the Address on the Blockchain, so we can interact with it.
Example of script to set new value :
const hre = require("hardhat");
async function main() {
const GreeterContract = await ethers.getContractFactory("Greeter");
const Greeter = await GreeterContract.attach("0xC9f9f144f58d590B136D526d43E9eDE8aa24Cd91");
const greet = await Greeter.setGreeting("Nouvelle valeur");
const newValue = await Greeter.greet();
console.log(newValue);
}

Upgradable Smart Contract

A Smart Contract is immutable but we can use proxy to solve this problem. HardHat provide all the tools to do it easily.

Deployment with proxy

Script to deploy a Smart Contract with a proxy.
const { ethers, upgrades } = require("hardhat");
async function main() {
const Greeter = await ethers.getContractFactory("Greeter");
const greeter = await upgrades.deployProxy(Greeter, ["Hello, Hardhat!"]);
await greeter.deployed();
console.log("Greeter deployed to:", greeter.address);
}

Upgrade with proxy

Script to change the implementation of the Smart Contract behind the proxy. (Need the proxy address)
const { ethers, upgrades } = require("hardhat");
async function main() {
const Greeter = await ethers.getContractFactory("Greeter");
const greeter = await upgrades.upgradeProxy("0xC9f9f144f58d590B136D526d43E9eDE8aa24Cd91", Greeter);
console.log("Greeter upgraded");
}

Verify Smart Contract

To verify a Smart Contract on the explorer (bscscan.com) we need to install the package "@nomiclabs/hardhat-etherscan" and get an API key from BscScan.
Add this code in the "hardhat.config.js" :
etherscan: {
apiKey: {
bsctestnet: "yourApiKey"
}
}

And you just have to launch this command :
npx hardhat verify --network bsctestnet

Top comments (0)