How to deploy a smart contract to Etherscan.io using Hardhat and Kovan testnet

Nwokocha Wisdom Maduabuchi
Coinmonks

--

Deploying your smart contract with Hardhat is easy because of the flexibility and room for easy modification of your project. Hardhat is an Ethereuem development environment that compiles and deploys a smart contract within a few minutes. It supports more than three development networks like Kovan, Ropsten, Goerli, and Rinkeby, and it has plugins that help integrate solhint, web3, waffle, ganache, truffle5, etherscan, ether to your project.

Etherscan is an Analytics Platform and Ethereum Block Explorer that helps you view data regarding any pending or confirmed Ethereum blockchain transactions. Since Ethereum is a public, open blockchain, whenever anyone interacts with it, the action is recorded into the transaction history, and it is available for anyone to see.

In this tutorial, you will use alchemyapi.io which is a blockchain supernode to create your Kovan network.

Follow the steps below to deploy your first smart contract

The first step is the installation of Hardhat

First step:

npm install --save-dev hardhat

Second step:

To create your Hardhat project run npx hardhat in your project folder

Third step:

change the solidity code in the “contract” folder with your own code and Modify the hardhat config file:

module.exports = {defaultNetwork: "kovan",networks: {hardhat: {},kovan: {url: "kovan url https",accounts: ["Kovan metamask private key"]}},etherscan: {// Your API key for Etherscan// Obtain one at https://etherscan.io/apiKey: "etherscan apikey"},solidity: "0.8.4",};

also, change the sample script:

// We get the contract to deployconst Ecommerce = await ethers.getContractFactory("name of your contract");const myEcommerce = await Ecommerce.deploy();await myEcommerce.deployed();console.log("MyFirstContract deployed to:", myEcommerce.address);

Note: make sure the contract name is same as the name you have in the getContractFactory = ethers.getContractFactory(“ecommerce”)

Fourth step:

Install Hardhat etherscan using the command: npm install --save-dev @nomiclabs/hardhat-etherscan

And add the following statement to your hardhat.config.js:

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

Fifth step:

Run the following command:

npx hardhat compile

Sixth step:

Run the following command

npx hardhat run scripts/sample-script.js

sample-script is located in the scripts folder, so change the name to your desired name and run it with the new name.

For example: you changed the script name to “Escript.js”, your command should look like this:

npx hardhat run scripts/Escript.js

after typing the command above you should have the following information

MyFirstContract deployed to: 0xc91e8DeFdEe5952BD51Ad500a6A82a49821b83Bc

then go to https://kovan.etherscan.io/ to check the address

Seventh step:

use the below command to verify the address

npx hardhat verify --network kovan  (address)npx hardhat verify --network kovan  0x9ee0a566f83*******

then you will have the following information below

Compiling 1 file with 0.8.4
Successfully submitted source code for contract
contracts/Ecommerce.sol:ecommerce at 0xc91e8DeFdEe5952BD51Ad500a6A82a49821b83Bc
for verification on Etherscan. Waiting for verification result…

Successfully verified contract ecommerce on Etherscan.
https://kovan.etherscan.io/address/0xc91e8DeFdEe5952BD51Ad500a6A82a49821b83Bc#code

Eighth step:

To be able to interact with your smart contract on etherscan you will need some ethers, to get free ethers for testing use the below link:

Get a free faucet for Kovan, https://faucets.chain.link/

or watch my video on how to get free ethers https://www.youtube.com/watch?v=5TsCD0cTlQI

Note: you will use your Kovan testnet address for getting the free ether.

Then go to https://kovan.etherscan.io/ for testing

Ninth step:

To be able to write a contract on etherscan, you will need to connect it to “Metamask”

Conclusion

Hardhat makes it super easy to deploy your smart contract to etherscan using Kovan, Ropsten, Gorlie, and Rinkeby testnet.

Full project: https://github.com/wise4rmgod/Ecommerce

Let’s connect on
Twitter: https://twitter.com/Joklinztech
LinkedIn: https://www.linkedin.com/in/wisdom-nwokocha-76212a77/
Youtube channel: https://www.youtube.com/channel/UCGh4vu4cVz72cvgFG2QHnWQ

Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing

Also Read

--

--

Nwokocha Wisdom Maduabuchi
Coinmonks

A software engineer with considerable experience in mobile development, native Android, and IOS development(Xcode), flutter dev, technical writing and community