DEV Community

Cover image for Deploying smart contracts with the new Hardhat update
Murat Can Yüksel
Murat Can Yüksel

Posted on

Deploying smart contracts with the new Hardhat update

To all #blockchain #web3 #devs out there who are trying to deploy via #hardhat and encountering errors: With the recent update, the deployment script changed slightly. Now, I will show you how we used to write our scripts/deploy.js file and then I will show the updated version. The first one will be for a referral contract (old) and the new, updated one will be for an NFT contract:

Old version =>

const { ethers } = require("hardhat");

async function main() {
const referralContract = await ethers.getContractFactory("Referral");
const referral = await referralContract.deploy();
await referral.deployed();

console.log("referral deployed to:", referral.address);
}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Enter fullscreen mode Exit fullscreen mode

With the update, instead of importing ethers, we're importing hre. Instead of getContractFactory, we have deployContract, and if we want to see the deployment address, instead of looking for the address keyword, we need to check the target keyword.

Now the new version =>

const hre = require("hardhat");

async function main() {
const nftContract = await hre.ethers.deployContract("NFT", [], {});

await nftContract.waitForDeployment();

console.log("NFT contract deployed to:", nftContract.target);
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});

Enter fullscreen mode Exit fullscreen mode

Do not get frustrated, stay safe and read the official docs.

Top comments (0)