Hardhat plugin to track gas on the transaction level

Overview

hardhat-gas-trackooor

Hardhat plugin to track gas on the transaction level.

Example report

Example

Installation

npm install hardhat-gas-trackooor --save-dev

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

require("hardhat-gas-trackooor");

Or, if you are using TypeScript, add this to your hardhat.config.ts:

import "hardhat-gas-trackooor"

Usage

Inside your tests, wrap your contract with the GasTracker class:

it("Should return the new greeting once it's changed", async function () {
    const Greeter = await ethers.getContractFactory("Greeter");
    const greeter = new GasTracker(await Greeter.deploy("Hello, world!"), {
      logAfterTx: true,
    })
    await greeter.setGreeting("Hola, mundo!");
    expect(await greeter.greet()).to.equal("Hola, mundo!");
  });

Then run

npx hardhat test

Example Output: output

Or, if you would like to specify which functions you want to get the gas for, you can use GetGas(transaction):

  it("Should return the new greeting once it's changed", async function () {
    const Greeter = await ethers.getContractFactory("Greeter");
    const greeter = await Greeter.deploy("Hello, world!");
    const gas = await GetGas(await greeter.setGreeting("Hola, mundo!"));
    console.log('Gas used: '+gas);
    expect(await greeter.greet()).to.equal("Hola, mundo!");
  });

NOTE: GetGas() does not work with a GasTracker wrapped contract at the moment.

Output: output

If you do not want to log gas to the console but still want to take advantage of the gas logging, gas data from transactions are logged to [wrappedContract].GasData:

it("Should return the new greeting once it's changed", async function () {
    const Greeter = await ethers.getContractFactory("Greeter");
    const greeter = new GasTracker(await Greeter.deploy("Hello, world!"), {
      logAfterTx: false, // set to false so it doesn't log
    })
    await greeter.setGreeting("Hola, mundo!");
    expect(await greeter.greet()).to.equal("Hola, mundo!");

    // access gas data through greeter.GasData;
    const gasData = greeter.gasData;
    console.log(gasData);
});

Options

There are not many options yet, feel free to make an issue or pull request if you want to add any.

Option Type Default Description
logAfterTx Boolean false Log gas after each transaction
You might also like...

Eth-explorers-extension - Chrome extension to open Ethereum addresses & transaction hash from any page on popular explorers + dashboards

Eth-explorers-extension - Chrome extension to open Ethereum addresses & transaction hash from any page on popular explorers + dashboards

eth-explorers-extension(s) This repository contains two folders with two extensions that work for address and transactions respectively. 1. eth-addres

Jan 6, 2023

Transaction tracker for Defi Kingdoms and more!

DFK Balances DFK Balances is a super customizable and extensible Financial tracker for the DeFi Kingdoms ecosystem. It allows users to easily add thei

Feb 22, 2022

Octane is a gasless transaction relayer for Solana.

Octane ⛽ What is Octane? How does it work? Is this secure? What does Octane want? How do I use it? What is Octane? Octane is a gasless transaction rel

Jan 1, 2023

Bruh. Generalized Meta transaction dispatcher.

metuh A generalized meta-transaction library for Ethereum-flavour chains. Not overengineered like all of OpenZeppelin. Inspired heavily by the Lens Di

Mar 22, 2022

A fast & reliable transaction API for web3 Games, Bridges and other projects

Gelato Relay SDK SDK to integrate into Gelato Multichain Relay Table of Contents Installation Introduction Quick Start Payment Types Request Types Sen

Dec 31, 2022

IPLD transaction as CAR buffer [for use in databases]

car-transaction IPLD transaction as CAR buffer that can be used as a database transaction. Usage import Transaction from 'car-transaction' const run

Sep 17, 2022

🔆🔎👀 Smart Contract Storage Viewer, DataType Guesser, Toolbox & Transaction Decoder

🔆🔎👀  Smart Contract Storage Viewer, DataType Guesser, Toolbox & Transaction Decoder

🔆 🔎 👀 Smart Contract Storage HexViewer Demo Target - the target contract API Endpoint - your infura (or equivalent) api key Retrieves smart contrac

Nov 27, 2022

Hardhat plugin for integration with hethers.js

hardhat-hethers Hardhat plugin for integration with hethers.js. What This plugin brings to Hardhat the Hedera library hethers.js, which allows you to

Sep 21, 2022

Build a Full Stack Marketplace on Ethereum with React, Solidity, Hardhat, and Ethers.js

Build a Full Stack Marketplace on Ethereum with React, Solidity, Hardhat, and Ethers.js

Building a Digital Marketplace on Ethereum The technologies used in this workshop are React, Next.js, Tailwind CSS, HardHat, Solidity, and Ethers. Get

Nov 15, 2022
Comments
  • Add leading spaces to output

    Add leading spaces to output

    UI suggestion - add 5 leading spaces to the output string and change the red box to a green battery.

    Adding spacing will fix indentation of output to align with the test hierarchy.

    Red fuel station looks alarming, changing to a relaxed green battery would look better.

    Instead of

          console.log(
            '\x1b' + gasColor,
            `⛽ ${prop} (${gas} gas) ${gas === 69420 ? 'nice.' : ''}`,
          );
    
    Screen Shot 2022-06-10 at 11 44 35

    change to -

          console.log(
            '\x1b' + gasColor,
            `     🔋 ${prop} (${gas} gas) ${gas === 69420 ? 'nice.' : ''}`,
          );
    
    Screen Shot 2022-06-10 at 11 44 54
    opened by ko0f 6
  • Display fix - indentation, colors and bullets

    Display fix - indentation, colors and bullets

    1/ changed gas usage output to have a static indentation; 2/ replaced alarming red fuel pump with a relaxed bullet, or ⚠️ in case of gasUsed > this.highGasThreshold; 3/ removed text colors, high gas usage doesn't necessarily require red text;

    opened by ko0f 1
  • TypeError: GasTracker is not a constructor

    TypeError: GasTracker is not a constructor

    I can't seem to find how to access GasTracker.

    The docs only mention to use

    require("hardhat-gas-trackooor");
    

    but that doesn't define GasTracker, so looking at the package source code it extends hre

    extendEnvironment((hre: HardhatRuntimeEnvironment) => {
      hre.GasTracker = GasTracker;
      hre.GetGas = GetGas;
    });
    

    so I tried

    require("hardhat-gas-trackooor");
    const hre = require("hardhat");
    ...
    test = new hre.GasTracker(await Test.deploy());
    

    but when executed using npx hardhat test it throws TypeError: GasTracker is not a constructor and hre.GasTracker is undefined.

    Also tried switching the require lines so that require("hardhat-gas-trackooor"); is after const hre = require("hardhat"); but it didn't help.

    opened by ko0f 1
  • It does not work

    It does not work

    Even with your own Greeter example, the resulting object does not have any of its original properties.

    TS2339: Property 'setGreeting' does not exist on type 'GasTracker'.

    opened by ddnexus 2
Owner
solidity engineer | Repos are private!
null
This project demonstrates single transaction and batch transaction use case

Batch Transaction Fullstack ( Localhost:8545 ) This project demonstrates single transaction and batch transaction use case. It comes with a transactio

Lemonde Shazai  39 Dec 27, 2022
🛠️ Easily track & compare gas costs estimated by Foundry on each of your Pull Requests!

?? ??️ Foundry Gas Diff Reporter Easily compare gas reports generated by Foundry automatically on each of your Pull Requests! How it works Everytime s

Romain Milon 133 Dec 28, 2022
Foundry-Hardhat plugins: Use Foundry for Hardhat projects

This repo contains hardhat plugins to use foundry tools in hardhat environments. Installation See in each plugin anvil forge foundryup Documentation F

Foundry 77 Nov 3, 2022
Ethereum smart contract gas cost waste pattern detection and patching tool

Ethereum smart contract gas cost waste pattern detection and patching tool

ibelab 4 Mar 23, 2022
An implementation of ERC1155D, a record setter for minting and transfer gas efficiency.

ERC1155D An implementation of ERC1155D, a record setter for minting and transfer gas efficiency. This contract is in alpha stage and has not been audi

null 72 Dec 26, 2022
Gas-efficient on-chain batched shuffling for NFTs

Batch NFT reveal This repository contains a gas-efficient implementation of an on-chain shuffling algorithm used to reveal NFTs in batches. The main b

Tubby Cats 97 Dec 12, 2022
A prototype snap for injecting gas fee prices into a confirmation window that also serves as the basis for a 5-minute Snaps tutorial

@Montoya/gas-fee-snap A simple Snap example based on @MetaMask/template-snap. Read below for a tutorial! Prerequisites Before you begin, make sure you

Christian Montoya 18 Dec 8, 2022
Eigen ZK-ZKRollup, Low gas-fee, better privacy-enhancement, high composable

ZKZRU: Eigen ZK-ZKRollup Eigen ZK-ZKRollup provides confidential transaction for users with low gas cost. The ZK-Rollup is an extention of RollupNC an

Eigen Labs 25 Dec 22, 2022
Today began learn about MEAN stack first with Nodejs that is a server side form of javascript. Repository for track of Day by Day improvement track in new Skill.

NodeJs Today began learn about MEAN stack first with Nodejs that is a server side form of javascript. Repository for track of Day by Day improvement t

Rahul Bhati 4 Oct 14, 2022
NodeJs, tron transaction checker

Tron node-Explorer Recommended requirements Node v14.17.5. npm 6.14.14 https://nodejs.org Development Install dependencies npm install Running applica

null 3 Aug 21, 2022