Measuring the gas costs of your smart contract functions is essential for optimizing performance and reducing transaction fees on the Ethereum network. In this guide, we will explore how to measure gas costs using Hardhat, a popular Ethereum development environment. We will cover various methods, including using the Hardhat Gas Reporter plugin and manual gas measurement within tests.

1. Using Hardhat Gas Reporter

The Hardhat Gas Reporter is a plugin that automatically measures and reports the gas usage of your contract functions during testing. This tool is invaluable for identifying which functions consume the most gas and require optimization.

Step 1: Install Hardhat Gas Reporter

To get started, you need to install the Hardhat Gas Reporter plugin. Run the following command in your project directory:

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

Step 2: Configure the Gas Reporter

Next, you need to configure the Gas Reporter in your hardhat.config.js file. Add the following code:

require("hardhat-gas-reporter");

module.exports = {
solidity: "0.8.0",
gasReporter: {
enabled: true,
currency: 'USD', // You can change this to your preferred currency
gasPrice: 21, // Set the gas price in gwei
},
};

Step 3: Write Tests for Your Contract

Now, write tests for your contract functions in the test directory. Here’s an example of a simple contract and its tests:

// contracts/MyToken.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract MyToken {
mapping(address => uint256) public balances;

function mint(address to, uint256 amount) public {
balances[to] += amount;
}

function transfer(address to, uint256 amount) public {
require(balances[msg.sender] >= amount, "Insufficient balance");
balances[msg.sender] -= amount;
balances[to] += amount;
}
}

// test/MyToken.test.js
const { expect } = require("chai");

describe("MyToken", function () {
let MyToken;
let myToken;

beforeEach(async function () {
MyToken = await ethers.getContractFactory("MyToken");
myToken = await MyToken.deploy();
await myToken.deployed();
});

it("should mint tokens", async function () {
await myToken.mint("0xYourAddress", 100);
});

it("should transfer tokens", async function () {
await myToken.mint("0xYourAddress", 100);
await myToken.transfer("0xAnotherAddress", 50);
});
});

Step 4: Run Your Tests

Finally, run your tests to see the gas usage for each function:

npx hardhat test

After running the tests, you will see output similar to the following, showing the gas costs for each function:

  MyToken
✓ should mint tokens (X gas)
✓ should transfer tokens (Y gas)

2. Manual Gas Measurement in Tests

In addition to using the Hardhat Gas Reporter, you can manually measure gas costs within your test cases. This approach gives you more control over the measurement process.

Example of Manual Gas Measurement

Here’s how you can manually measure gas costs in your tests:

// test/MyToken.test.js
const { expect } = require("chai");

describe("MyToken", function () {
let MyToken;
let myToken;

beforeEach(async function () {
MyToken = await ethers.getContractFactory("MyToken");
myToken = await MyToken.deploy();
await myToken.deployed();
});

it("should mint tokens and measure gas", async function () {
const tx = await myToken.mint("0xYourAddress", 100);
const receipt = await tx.wait();
console.log("Mint gas used:", receipt.gasUsed.toString());
});

it("should transfer tokens and measure gas", async function () {
await myToken.mint("0xYourAddress", 100);
const tx = await myToken.transfer("0xAnotherAddress", 50);
const receipt = await tx.wait();
console.log("Transfer gas used:", receipt.gasUsed .toString());
});
});

Conclusion

Measuring gas costs is a crucial aspect of smart contract development, and Hardhat provides effective tools to achieve this. By using the Hardhat Gas Reporter, you can automatically track gas usage during tests, while manual measurement allows for more granular control. Both methods help identify gas-intensive functions, enabling developers to optimize their contracts for better performance and lower transaction fees on the Ethereum network.