Deploy and Verify a Smart Contract to Testnet using Hardhat
Tags: blockchain, javascript, tutorial, beginners
Published: 2022-08-08 21:28:00

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](https://blog.ethereum.org/2022/06/21/testnet-deprecation/) 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](https://hardhat.org/docs) 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

- [Node.js](https://nodejs.org/en/download/) installed
- [Metamask Wallet](https://metamask.io/)
- [Infura API Key](https://infura.io/dashboard)
- [Etherscan Account](https://etherscan.io/register)
- Have some Rinkeby funds in your wallet, in case you need some can use this [faucet](https://faucets.chain.link/rinkeby)


## 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:

```console
npm i dotenv
```

```console
npm i hardhat --save-dev hardhat @nomiclabs/hardhat-etherscan @nomicfoundation/hardhat-toolbox
```

Now execute the following command

```console
npx hardhat
```

You can use the JavaScript option for this example.

![hardhat CLI](https://res.cloudinary.com/codenjobs/image/upload/v1659824835/user/file/epzwb7eglvlxhu7dy8kh.png)

## Project File Structure

```
hardhat-tutorial/
├── contracts/
├── node_modules/
├── scripts/
├── test/
├── .env
├── hardhat.config.js
└── package.json
```


## Table of Contents

1. [Config our .env file](#env-file)
2. [Set Up hardhat.config.js](#set-up-hardhat-config-file)
3. [Creating the contract to deploy](#Dummy-contract)
4. [Deploy Script](#deploy-script)
5. [Deploying and Verifying](#deploying-and-verifying)
6. [Conclusion](#conclusion)

---

## 1. Config our .env file <a name="env-file"></a>

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

We will need to set the following variables:

```js
INFURA_API_KEY = your infura API key
METAMASK_ACCOUNT = your metamask account
ETHERSCAN_API_KEY = your etherscan API key
```

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](https://res.cloudinary.com/codenjobs/image/upload/v1659805057/user/file/sy3tanfmhlnt1mcohvhd.png)

2. Click on **Account details** .

![step-two](https://res.cloudinary.com/codenjobs/image/upload/v1659805155/user/file/gknllnmhkydnzycebszw.png)

3. Click on **Export Private Key**.

![step-three](https://res.cloudinary.com/codenjobs/image/upload/v1659805174/user/file/gdar3hdswjlkfn7alarw.png)

4. Finally type your password.

![step-four](https://res.cloudinary.com/codenjobs/image/upload/v1659805186/user/file/finpjv1cs4jafzjcofzw.png)


## 2. Set Up hardhat.config.js <a name="set-up-hardhat-config-file"></a>


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


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

module.exports = {
  solidity: "0.8.9",
};
```

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


```js
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
  }
};
```


## 3. Creating the contract to deploy <a name="Dummy-contract"></a>

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

```js
//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;
      }
  }
```

## 4. Deploy Script <a name="Deploy-script"></a>

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.


```js
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);
  })
```


## 5. Deploying and Verifying <a name="deploying-and-verifying"></a>

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

```console
npx hardhat run scripts/deploy.js --network rinkeby
```

You will get something similar to this:

```console
DummyContract deployed to:  0x5c2E1BD82EDC0695cbA5AE64dA7aE34f073E167C
```

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](https://res.cloudinary.com/codenjobs/image/upload/v1659824940/user/file/d1sp2y5dr7xd6il5xbcp.png)

```console
npx hardhat verify --network rinkeby 0x5c2E1BD82EDC0695cbA5AE64dA7aE34f07
```


Finally you will get a message similar to this:

```console
Successfully verified contract Greeter on Etherscan
https://rinkeby.etherscan.io/address/0x5c2E1BD82EDC0695cbA5AE64dA7aE34f073E167C#code
```

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

![deployed-contract](https://res.cloudinary.com/codenjobs/image/upload/v1659824794/user/file/rcsbgtxclyw3qajhpi7r.png)


## 6. Conclusion <a name="conclusion"></a>

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](https://www.codenjobs.com/) web

[You can also visit the Spanish version of this post](https://www.codenjobs.com/blog?&title=C%C3%B3mo-desplegar-y-verificar-un-smart-contract-con-Hardhat&id=83bc155d-2f32-402e-8725-5554d972e82c)

[You can contact me by telegram if you need to hire a Full Stack developer.](https://t.me/rtagliajs)

You can also find me on **Discord as Appu#9136**


Thank you for your time.