DEV Community

Cover image for Deploy and Verify a Smart Contract to Testnet using Hardhat
Robert
Robert

Posted on • Edited on

Deploy and Verify a Smart Contract to Testnet using Hardhat

In the following post will be learn how to deploy and verify a smart contract to Rinkeby testnet using hardhat.

NOTE: Rinkeby will be deprecated soon, click here if you want to know more about it, anyways this example can work for any other testnet like Goerli, Kovan, etc.

What is Hardhat?

According to official documentation Hardhat is a development environment for Ethereum software. It consists of different components for editing, compiling, debugging and deploying your smart contracts and dApps, all of which work together to create a complete development environment.

Prerequisites

Creating Our Project

  1. open your terminal and type the following commands
  2. mkdir hardhat-tutorial
  3. cd hardhat-tutorial
  4. npm init --y
  5. Open the project with your preferred code editor

Dependencies

  • dotenv
  • hardhat
  • @nomiclabs/hardhat-etherscan
  • @nomicfoundation/hardhat-toolbox

To install dependencies go to your project folder and open a terminal and type the following commands:

npm i dotenv
Enter fullscreen mode Exit fullscreen mode
npm i hardhat --save-dev hardhat @nomiclabs/hardhat-etherscan @nomicfoundation/hardhat-toolbox
Enter fullscreen mode Exit fullscreen mode

Now execute the following command

npx hardhat
Enter fullscreen mode Exit fullscreen mode

You can use the JavaScript option for this example.

hardhat CLI

Project File Structure

hardhat-tutorial/
├── contracts/
├── node_modules/
├── scripts/
├── test/
├── .env
├── hardhat.config.js
└── package.json
Enter fullscreen mode Exit fullscreen mode

Table of Contents

  1. Config our .env file
  2. Set Up hardhat.config.js
  3. Creating the contract to deploy
  4. Deploy Script
  5. Deploying and Verifying
  6. Conclusion

1. Config our .env file

Let's create a .env file in case you haven't already and open it.

We will need to set the following variables:

INFURA_API_KEY = your infura API key
METAMASK_ACCOUNT = your metamask account
ETHERSCAN_API_KEY = your etherscan API key
Enter fullscreen mode Exit fullscreen mode

In case you don't know how to get your Metamask account, just follow the following steps:

  1. Open your Metamask browser extension, click on the three dots.

step-one

  1. Click on Account details .

step-two

  1. Click on Export Private Key.

step-three

  1. Finally type your password.

step-four

2. Set Up hardhat.config.js

First we need to set up the configuration file, so let's open hardhat.config.js, you will see something similar to this

require("@nomicfoundation/hardhat-toolbox");
require("@nomiclabs/hardhat-etherscan");

module.exports = {
  solidity: "0.8.9",
};
Enter fullscreen mode Exit fullscreen mode

We need to specify the networks we want to use, for this example we are only going to use Rinkeby.

require("@nomicfoundation/hardhat-toolbox");
require("@nomiclabs/hardhat-etherscan");
//dotenv
require("dotenv").config();

module.exports = {
  solidity: "0.8.9",
  networks: {
    rinkeby: {
      url: `https://rinkeby.infura.io/v3/${process.env.INFURA_API_KEY}`,
      accounts: [`${process.env.METAMASK_ACCOUNT}`]
    }
  },
  etherscan: {
    apiKey: process.env.ETHERSCAN_API_KEY
  }
};
Enter fullscreen mode Exit fullscreen mode

3. Creating the contract to deploy

For this example will be using a Dummy contract called Greeter.sol, so let's open it.

//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

  import "hardhat/console.sol";

  contract Greeter {
      string public greeting = 'Hello World';

      function setGreeting(string memory _greeting) public {
          console.log("Changing greeting from '%s' to '%s'", greeting, _greeting);
          greeting = _greeting;
      }
  }
Enter fullscreen mode Exit fullscreen mode

4. Deploy Script

Go to scripts folder and open deploy.js, delete everything inside.

  1. First let's require ethers from hardhat.
  2. Then we will create a function that will be executed when the script is executed.
  3. we will use ethers.getSigners() to get a object with our ethereum account information .
  4. then we with getContractFactory() we are going to abstract the contract information, in this case of Greeter.sol.
  5. we will deploy that information with deploy().
  6. Finally we wait for the contract to be deployed and get the address of the contract.
const { ethers } = require("hardhat");

async function main(){
  const [deployer] = await ethers.getSigners();

  console.log("Deploying contracts with the account: ", deployer.address);

  const DummyContract = await ethers.getContractFactory("Greeter");
  const dummyContract = await DummyContract.deploy();

  await dummyContract.deployed();

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

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

5. Deploying and Verifying

Finally we will execute the following commands to deploy and verify our contract.

npx hardhat run scripts/deploy.js --network rinkeby
Enter fullscreen mode Exit fullscreen mode

You will get something similar to this:

DummyContract deployed to:  0x5c2E1BD82EDC0695cbA5AE64dA7aE34f073E167C
Enter fullscreen mode Exit fullscreen mode

Then to verify the contract type the following command:

The address used in the next command will be the value that you previously get from the console.

Address

npx hardhat verify --network rinkeby 0x5c2E1BD82EDC0695cbA5AE64dA7aE34f07
Enter fullscreen mode Exit fullscreen mode

Finally you will get a message similar to this:

Successfully verified contract Greeter on Etherscan
https://rinkeby.etherscan.io/address/0x5c2E1BD82EDC0695cbA5AE64dA7aE34f073E167C#code
Enter fullscreen mode Exit fullscreen mode

Now if you visit that link you will be able to see your contract as well as the code.

deployed-contract

6. Conclusion

We learned how to deploy and verify a smart contract using Hardhat.

I really hope you have been able to follow the post without any trouble, otherwise I apologize, please leave me your doubts or comments.

If you're looking for a blockchain dev position, or more content related, you should check codenjobs web

You can also visit the Spanish version of this post

You can contact me by telegram if you need to hire a Full Stack developer.

You can also find me on Discord as Appu#9136

Thank you for your time.

Top comments (0)