Socket
Socket
Sign inDemoInstall

eth-gas-reporter

Package Overview
Dependencies
360
Maintainers
2
Versions
63
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    eth-gas-reporter

Mocha reporter which shows gas used per unit test.


Version published
Weekly downloads
84K
increased by2.14%
Maintainers
2
Install size
45.2 MB
Created
Weekly downloads
 

Readme

Source

eth-gas-reporter

npm version Build Status Codechecks buidler

A Mocha reporter for Ethereum test suites:

  • Gas usage per unit test.
  • Metrics for method calls and deployments.
  • National currency costs of deploying and using your contract system.
  • CI integration with codechecks
  • Simple installation for Truffle and Buidler
  • Use ETH, BNB, MATIC, AVAX, HT or MOVR price to calculate the gas price.

Example output

Screen Shot 2019-06-24 at 4 54 47 PM

Installation and Config

Truffle

npm install --save-dev eth-gas-reporter
/* truffle-config.js */
module.exports = {
  networks: { ... },
  mocha: {
    reporter: 'eth-gas-reporter',
    reporterOptions : { ... } // See options below
  }
};

Buidler

npm install --save-dev buidler-gas-reporter
/* buidler.config.js */
usePlugin('buidler-gas-reporter');

module.exports = {
  networks: { ... },
  gasReporter: { ... } // See options below
};

Other

This reporter should work with any build platform that uses Mocha and connects to an Ethereum client running as a separate process. There's more on advanced use cases here.

Continuous Integration (Travis and CircleCI)

This reporter comes with a codechecks CI integration that displays a pull request's gas consumption changes relative to its target branch in the Github UI. It's like coveralls for gas. The codechecks service is free for open source and maintained by MakerDao engineer @krzkaczor.

Complete set-up guide here (it's easy).

Screen Shot 2019-06-18 at 12 25 49 PM

Options

:warning: CoinMarketCap API change :warning:

Beginning March 2020, CoinMarketCap requires an API key to access currency market price data. The reporter uses an unprotected . You can get your own API key here and set it with the coinmarketcap option. (This service's free tier allows 10k reqs/mo)

In order to retrieve the gas price of a particular blockchain, you can configure the token and gasPriceApi (API key rate limit may apply).

NOTE: HardhatEVM and ganache-cli implement the Ethereum blockchain. To get accurate gas measurements for other chains you may need to run your tests against development clients developed specifically for those networks.

OptionTypeDefaultDescription
currencyString'EUR'National currency to represent gas costs in. Exchange rates loaded at runtime from the coinmarketcap api. Available currency codes can be found here.
coinmarketcapString(unprotected API key)API key to use when fetching current market price data. (Use this if you stop seeing price data)
gasPriceNumber(varies)Denominated in gwei. Default is loaded at runtime from the eth gas station api
tokenString'ETH'The reference token for gas price
gasPriceApiStringEtherscanThe API endpoint to retrieve the gas price. Find below other networks.
outputFileStringstdoutFile path to write report output to
forceConsoleOutputBooleanfalsePrint report output on console
noColorsBooleanfalseSuppress report color. Useful if you are printing to file b/c terminal colorization corrupts the text.
onlyCalledMethodsBooleantrueOmit methods that are never called from report.
rstBooleanfalseOutput with a reStructured text code-block directive. Useful if you want to include report in RTD
rstTitleString""Title for reStructured text header (See Travis for example output)
showTimeSpentBooleanfalseShow the amount of time spent as well as the gas consumed
excludeContractsString[][]Contract names to exclude from report. Ex: ['Migrations']
srcString"contracts"Folder in root directory to begin search for .sol files. This can also be a path to a subfolder relative to the root, e.g. "planets/annares/contracts"
urlStringweb3.currentProvider.hostRPC client url (ex: "http://localhost:8545")
proxyResolverFunctionnoneCustom method to resolve identity of methods managed by a proxy contract.
artifactTypeFunction or String"truffle-v5"Compilation artifact format to consume. (See advanced use.)
showMethodSigBooleanfalseDisplay complete method signatures. Useful when you have overloaded methods you can't tell apart.
maxMethodDiffNumberundefinedCodechecks failure threshold, triggered when the % diff for any method is greater than number (integer)
maxDeploymentDiffNumberundefinedCodechecks failure threshold, triggered when the % diff for any deployment is greater than number (integer)
token and gasPriceApi options example
NetworktokengasPriceApi
Ethereum (default)ETHhttps://api.etherscan.io/api?module=proxy&action=eth_gasPrice
BinanceBNBhttps://api.bscscan.com/api?module=proxy&action=eth_gasPrice
PolygonMATIChttps://api.polygonscan.com/api?module=proxy&action=eth_gasPrice
AvalancheAVAXhttps://api.snowtrace.io/api?module=proxy&action=eth_gasPrice
HecoHThttps://api.hecoinfo.com/api?module=proxy&action=eth_gasPrice
MoonriverMOVRhttps://api-moonriver.moonscan.io/api?module=proxy&action=eth_gasPrice

These APIs have rate limits. Depending on the usage, it might require an API Key.

NB: Any gas price API call which returns a JSON-RPC response formatted like this is supported: {"jsonrpc":"2.0","id":73,"result":"0x6fc23ac00"}.

Advanced Use

An advanced use guide is available here. Topics include:

  • Getting accurate gas data when using proxy contracts like EtherRouter or ZeppelinOS.
  • Configuring the reporter to work with non-truffle, non-buidler projects.

Example Reports

Usage Notes

  • Requires Node >= 8.
  • You cannot use ganache-core as an in-process provider for your test suite. The reporter makes sync RPC calls while collecting data and your tests will hang unless the client is launched as a separate process.
  • Method calls that throw are filtered from the stats.
  • Contracts that are only ever created by other contracts within Solidity are not shown in the deployments table.

Troubleshooting

Contributions

Feel free to open PRs or issues. There is an integration test and one of the mock test cases is expected to fail. If you're adding an option, you can vaildate it in CI by adding it to the mock options config located here.

Credits

All the ideas in this utility have been borrowed from elsewhere. Many thanks to:

  • @maurelian - Mocha reporting gas instead of time is his idea.
  • @cag - The table borrows from / is based his gas statistics work for the Gnosis contracts.
  • Neufund - Block limit size ratios for contract deployments and euro pricing are borrowed from their ico-contracts test suite.

Contributors

Keywords

FAQs

Last updated on 30 Sep 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc