How To Verify Cost Of Gas Used In A Contract Using Hardhat Gas Reporter.

How To Verify Cost Of Gas Used In A Contract Using Hardhat Gas Reporter.

Introducing hardhat gas reporter.

In this tutorial, I hope to show you how to show you how to visualize the cost of a smart contract. This will help you along your journey on creating the most gas optimized contract!

Let's begin.

First lets create a hardhat contract directory by installing and initiating hardhat.

npm install --save-dev hardhat

Initialize hardhat

npx hardhat

Your screen should display this. You can create a basic sample project for starters.

Screen Shot 2022-06-11 at 11.21.15 AM.png

Once in file directory, lets install hardhat gas reporter

npm install --save-dev hardhat-gas-reporter

Once installed, let's add all we need into our hardhat.config.js file .

Screen Shot 2022-06-11 at 11.23.49 AM.png

In your hardhat.config.js file, add

require("hardhat-gas-reporter")

In module.exports add your gas reporter.

  gasReporter: {
    enabled: true,
    outputFile: "gas-report.txt",
    noColors: true,
    currencey: "USD",
  }

Of course you want to

npx hardhat compile

After that you are ready to run your test!! let's do it!

npx hardhat test

Should output a file similar to mine! All depends on what is is written in your test file!

Screen Shot 2022-06-11 at 11.30.19 AM.png

Hope this helped!!! Feedback is greatly appreciated!