Getting Started with Gas Profiling
Begin by creating a smart contract named Inventory with the following configuration:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;
contract Inventory {
address public manager;
uint256 public totalProducts;
struct Product {
uint256 id;
string description;
uint256 price;
}
mapping(uint256 => Product) public products;
constructor() {
manager = msg.sender;
}
function addProduct(string memory description, uint256 price) external {
require(msg.sender == manager, "invalid manager");
totalProducts++;
products[totalProducts] = Product(
totalProducts,
description,
price
);
}
}
Add a test file named Inventory.js to evaluate the gas reporter plugin. This file should include the following content:
Run npx hardhat test to execute the tests. A gas usage report will be generated as shown below:

PreviousConfiguring the Hardhat Gas Reporter PluginNextTypical Approaches to Reduce Contract Size and Gas Costs
Last updated