Hardhat and Ethers Smart Contract Deployment and Verification.

Joe
4 min readSep 12, 2023

--

So in this article, I will be showing you how to set up hardhat, deploy a simple contract to base goerli network, verify the contract using hardhat, ethers and typescript.

Before I begin, it will be nice if you have basic knowledge about solidity and typescript. With such knowledge, you will be able to understand what I will be doing.

First of all, Let’s set up our project folder. Open your terminal, navigate to where you want to create your folder and enter the following command

mkdir harhat_scripting

Enter the following command, to open your folder with VSCode.

code .

using your terminal, init your project with a package.json file using the following command.

npm init

So now we will be installing hardhat

npm install --save-dev hardhat

We will be setting an hardhat project with all needed libraries, with one command in our terminal

npx hardhat

You should have the above image in your terminal, if you did everything correctly. Follow the next steps in the terminal, and you will have set up an hardhat project. Since we will be working with typescript, you can choose Typescript.

So we are done setting up hardhat, let’s move into deploying a smart contract using hardhat. You will see a contract folder (after setting up hardhat) inside your folder (hardhat_scripting) you created. A file called Lock.sol (set up when you added hardhat).

The Lock contract, simply allows a person set an unlock time in the future to withdraw the contract ethers balance. Like I said earlier, You will need knowledge about solidity to understand the Lock contract.

Before, we proceed to deploying our smart contract to base goerli network, we will need to configure up our hardhat config file.

Let’s add another dependency called dotenv, dotenv is a zero-dependency module that loads environment variables from a .env file into process.env. Storing configuration in the environment separate from code is based on The Twelve-Factor App methodology. Inside our .env , we will add our private key and RPC Link.

npm install dotenv --save

This command installs dotenv. Add a file call .env at your root folder. You will then update the file content with the code snippet below. You will replace the private key with yours, as well as the Alchemy RPC.

ALCHEYBASERPC=ALCHEMY_BASE_GOERLI_RPC
PRIVATEKEY=YOUR PRIVATE KEY

Please make sure that before you create an env file, you have a .gitignore file in your root folder that will ignore your .env file when pushing your code. When you set up hardhat, you had the option of allowing a .gitignore file to be added.

The .gitignore content will look like the mage below;

Let’s configure our hardhat by editing the hardhat.config.ts file. Replace the content of the file with the code below;

import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
require("dotenv").config();

const config: HardhatUserConfig = {
solidity: "0.8.19",
networks: {
"base-goerli": {
url: process.env.ALCHEYBASERPC,
accounts: [process.env.PRIVATE_KEY as string],
gasPrice: 1000000000,
},
},

So what we are doing above, we are setting our networks by adding base-goerli, as that’s where we will be deploying our contract to. You can read up on how to set up for other networks like mainnet, sepolia, goerli, etc. But for this article we are focused on base-goerli.

Inside scripts folder, we have a deploy.ts file already provided when we set up hardhat. We will be using this file to deploy our Lock contract to base-goerli.

npx hardhat run scripts/deploy.ts --network base-goerli

The above command in the terminal will compile and run the deploy.ts file. If successful, you should see an output like the image below

So as you can see, we have deployed our Lock contract to base goerli network. You can confirm using your output address.

How about verifying our contract. We can verify our smart contracts, so that others can view and interact with it using basescan.

Before we do that, let’s update our hardhat.config file with the code snippet below; add it after the networks object;

etherscan: {
apiKey: {
"base-goerli": "PLACEHOLDER_STRING"
},
customChains: [
{
network: "base-goerli",
chainId: 84531,
urls: {
apiURL: "https://api-goerli.basescan.org/api",
browserURL: "https://goerli.basescan.org"
}
}
]
},
npx hardhat verify --network base-goerli <contract address> <constructor argument>

By running the command, your contract will be verified on base-goerli. You can then proceed to basescan to confirm.

You should get see something like the image above. With a success checkmark by the “Contract” tab.

We have successfully set up hardhat, deployed and verified a contract using hardhat, ethers and typescript.

--

--

Joe

Smart Contract. Pisces. Music. Books over People. above all, Gigantura Indica.