eth-gas-reporter: Gas Usage for Ethereum Tests
eth-gas-reporter is a Mocha reporter designed to provide detailed gas usage metrics for Ethereum smart contract unit tests. It helps developers optimize contract efficiency by displaying gas consumption per test, method calls, and deployments. The current stable version is 0.2.27 as of April 2026. While an explicit release cadence isn't stated, the changelog shows consistent updates over time, indicating active maintenance. Key differentiators include its integration with popular Ethereum development environments like Truffle and Hardhat (via a plugin), the ability to calculate real-world national currency costs, and CI integration via Codechecks for tracking gas changes across pull requests. It supports various tokens for price calculation like ETH, BNB, MATIC, AVAX, HT, or MOVR.
Common errors
-
CoinMarketCap API Key is not set or invalid.
cause The reporter attempted to fetch cryptocurrency prices but found a missing or invalid CoinMarketCap API key.fixObtain a valid API key from CoinMarketCap and set it using the `coinmarketcap` option in your `reporterOptions` configuration or as an environment variable. -
Error: Reporter 'eth-gas-reporter' not found.
cause Mocha could not locate the `eth-gas-reporter` package, often due to incorrect installation or an improper path.fixEnsure `eth-gas-reporter` is installed as a dev dependency (`npm install --save-dev eth-gas-reporter`) and that your Mocha configuration correctly specifies its name: `reporter: 'eth-gas-reporter'`. -
Error: Cannot find module 'hardhat-gas-reporter'
cause When using Hardhat/Buidler, the dedicated plugin `hardhat-gas-reporter` is missing, not `eth-gas-reporter` directly.fixInstall the specific plugin for Hardhat/Buidler: `npm install --save-dev hardhat-gas-reporter` and include it in your configuration file using `require('hardhat-gas-reporter');`.
Warnings
- breaking Starting March 2020, the CoinMarketCap API requires a valid API key for fetching cryptocurrency market price data. Using the reporter for national currency conversions without this key will result in failures.
- breaking The underlying Mocha dependency was updated to v10. This update may introduce compatibility issues with older Mocha test suites or custom Mocha configurations if not managed correctly.
- gotcha When integrating with Hardhat (formerly Buidler), the `eth-gas-reporter` package is not configured directly. Instead, you must install and utilize the separate `hardhat-gas-reporter` plugin.
- breaking Internal changes in v0.2.27 removed `@ethersproject/abi` in favor of `ethers.utils`. This primarily affects maintainers or developers who might have been interacting with the reporter's internal `ethers` dependencies directly.
- gotcha The reporter makes synchronous RPC calls during data collection. Running your test suite with an in-process provider like `ganache-core` can cause tests to hang.
Install
-
npm install eth-gas-reporter -
yarn add eth-gas-reporter -
pnpm add eth-gas-reporter
Imports
- reporter configuration string
import EthGasReporter from 'eth-gas-reporter'
mocha: { reporter: 'eth-gas-reporter' } - reporterOptions object
import { reporterOptions } from 'eth-gas-reporter'mocha: { reporterOptions: { currency: 'USD', token: 'ETH' } } - Hardhat/Buidler Plugin
require('eth-gas-reporter'); // directly in hardhat.config.jsrequire('hardhat-gas-reporter'); // in hardhat.config.js or buidler.config.js
Quickstart
/* truffle-config.js */
const HDWalletProvider = require('@truffle/hdwallet-provider'); // Example dependency for network config
module.exports = {
networks: {
development: {
host: '127.0.0.1',
port: 8545,
network_id: '*',
},
goerli: {
provider: () => new HDWalletProvider(
process.env.MNEMONIC ?? 'your_mnemonic_here',
`https://goerli.infura.io/v3/${process.env.INFURA_PROJECT_ID ?? ''}`
),
network_id: 5,
gasPrice: 10000000000, // 10 Gwei
confirmations: 2,
timeoutBlocks: 200,
skipDryRun: true
},
},
mocha: {
reporter: 'eth-gas-reporter',
reporterOptions : {
currency: 'USD',
token: 'ETH',
gasPrice: 20, // Example fixed gas price in Gwei
coinmarketcap: process.env.COINMARKETCAP_API_KEY ?? '', // Required for currency conversions
outputFile: 'gas-report.txt',
noColors: false,
rst: true, // Use ReStructuredText format for output
rstTitle: 'Gas Usage Report'
}
},
compilers: {
solc: {
version: '0.8.0', // Specify your Solidity compiler version
},
},
};