How to Deploy a Smart Contract with HardHat

mbvissers.eth
Quick Programming
Published in
3 min readNov 10, 2021

--

A guide on deploying a smart contract on your favorite blockchains.

Photo by Stanislaw Zarychta on Unsplash

Smart contracts are all over the place, mostly for NFTs, but also for highly advanced decentralized financial organizations. You can program these in Solidity for Ethereum, which also works for solutions such as Polygon.

I’ve written a few articles about creating smart contracts for NFTs already so I won’t go into detail about creating the contract, but I will show you how I deploy these to the test and main networks using HardHat.

Let’s get started

HardHat is an “Ethereum development environment for professionals”. It has lots of tools and features that you might expect from Truffle like deploying, testing, compiling, and more. You can also use Truffle, but today’s article will be about HardHat.

To set up a new HardHat project, you need to have Node installed. You can run:

npx hardhat

And you will be presented with two options. A sample project, or an empty project. I will use a sample project, it will create a basic structure for us like the following:

contracts/
scripts/
test/
hardhat.config.js

In the contracts folder, we will put our Solidity contract, in scripts we will add our deploy-script, and test will not be used today.

In the hardhat.config.js you can configure the chain you want to deploy to. We will check it out as well.

Deploying

The sample-script.js already has most of the things we need. I always alter it a small bit to make it easier to read.

On line 4, where we have "YourContractName" , you should add the name of the Solidity contract. Not the file name.

On line 5, you need to set your constructor’s parameters, if you have them. A simple NFT contract most likely has none.

On line 7, we log the contract’s address when it’s deployed. It’s nice to save it somewhere, otherwise, you can find it on EtherScan from your own address.

To deploy, run a Hardhat node on one terminal using npx hardhat node , and in another terminal, run:

npx hardhat run --network <your-network> scripts/deploy-script.js

You can set your network in <your-network> , but how do you configure them? Let’s take a look at the config file.

Configuration

Adding a new network to deploy to isn’t difficult. The default sample project doesn’t add any code to do this, however, we can easily add it in the module.exports object in the hardhat.config.js .

By default, only the Solidity version is exported, but we can add some networks. Let’s add Polygon Mumbai and Polygon mainnet for now.

I also expanded the Solidity part and the paths. It’s not necessary, but it gives a bit more insight or helps when you’ve changed folder names. Now just run

npx hardhat run --network matic_testnet scripts/deploy-script.js

to deploy to Polygon Mumbai.

You can find the information for the Ethereum testnets all around the internet with a quick Google search. And you might need to add an Infura link as the node.

Conclusion

Deploying using Hardhat isn’t hard, it just requires you to do it once just so you know how it works. It isn’t any better than Truffle, or any other development tool, but it’s one of the most used out there.

Thank you so much for reading and have a wonderful day.

Support me by supporting Medium and becoming a member. It helps me out a lot, it won’t cost you extra, and you can read as many Medium articles as you like!

Follow me on Twitter to keep up with me.

Check out my latest NFT collection here.

--

--

mbvissers.eth
Quick Programming

I occasionally write about programming. Follow me on Twitter @0xmbvissers