diff --git a/README.md b/README.md index adedc6c..4af6096 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ This plugin overrides the built-in `test` task. Gas reports are generated by def npx hardhat test ``` -## Options +### Options :warning: **CoinMarketCap API change** :warning: @@ -85,25 +85,32 @@ Beginning March 2020, CoinMarketCap requires an API key to access currency marke price data. The reporter uses an unprotected free tier key by default (10k reqs/mo). You can get your own API key [here][55] and set it with the `coinmarketcap` option. -| Option | Type | Default | Description | -| ----------------- | ---------------------- | --------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +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. + +| Option | Type | Default | Description | +| ----------------- | ---------------------- | -------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | enabled | _Boolean_ | true | Always generate gas reports when running the hardhat test command. | -| currency | _String_ | 'EUR' | National currency to represent gas costs in. Exchange rates loaded at runtime from the `coinmarketcap` api. Available currency codes can be found [here](https://coinmarketcap.com/api/documentation/v1/#section/Standards-and-Conventions). | -| coinmarketcap | _String_ | (unprotected API key) | [API key][55] to use when fetching current market price data. (Use this if you stop seeing price data) | -| gasPrice | _Number_ | (varies) | Denominated in `gwei`. Default is loaded at runtime from the `eth gas station` api | -| outputFile | _String_ | stdout | File path to write report output to | -| noColors | _Boolean_ | false | Suppress report color. Useful if you are printing to file b/c terminal colorization corrupts the text. | -| onlyCalledMethods | _Boolean_ | true | Omit methods that are never called from report. | -| rst | _Boolean_ | false | Output with a reStructured text code-block directive. Useful if you want to include report in RTD | -| rstTitle | _String_ | "" | Title for reStructured text header (See Travis for example output) | -| showTimeSpent | _Boolean_ | false | Show the amount of time spent as well as the gas consumed | +| currency | _String_ | 'EUR' | National currency to represent gas costs in. Exchange rates loaded at runtime from the `coinmarketcap` api. Available currency codes can be found [here](https://coinmarketcap.com/api/documentation/v1/#section/Standards-and-Conventions). | +| coinmarketcap | _String_ | (unprotected API key) | [API key][55] to use when fetching current market price data. (Use this if you stop seeing price data) | +| gasPrice | _Number_ | (varies) | Denominated in `gwei`. Default is loaded at runtime from the `eth gas station` api | +| token | _String_ | 'ETH' | The reference token for gas price | +| gasPriceApi | _String_ | [Etherscan](https://api.etherscan.io/api?module=proxy&action=eth_gasPrice) | The API endpoint to retrieve the gas price. Find below other networks. | +| outputFile | _String_ | stdout | File path to write report output to | +| noColors | _Boolean_ | false | Suppress report color. Useful if you are printing to file b/c terminal colorization corrupts the text. | +| onlyCalledMethods | _Boolean_ | true | Omit methods that are never called from report. | +| rst | _Boolean_ | false | Output with a reStructured text code-block directive. Useful if you want to include report in RTD | +| rstTitle | _String_ | "" | Title for reStructured text header (See Travis for example output) | +| showTimeSpent | _Boolean_ | false | Show the amount of time spent as well as the gas consumed | | excludeContracts | _String[]_ | [] | Contracts (or folders) to exclude from report. Ex: `['Migrations.sol', 'Wallets/']`. (See [v1.0.3 release notes][45] for additional usage help) | -| src | _String_ | "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" | -| url | _String_ | `http://localhost:8545` (or network value) | RPC client url | -| proxyResolver | _Function_ | none | Custom method to resolve identity of methods managed by a proxy contract. | -| showMethodSig | _Boolean_ | false | Display complete method signatures. Useful when you have overloaded methods you can't tell apart. | -| maxMethodDiff | _Number_ | undefined | Codechecks failure threshold, triggered when the % diff for any method is greater than `number` (integer) | -| maxDeploymentDiff | _Number_ | undefined | Codechecks failure threshold, triggered when the % diff for any deployment is greater than `number` (integer) | +| src | _String_ | "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" | +| url | _String_ | `web3.currentProvider.host` | RPC client url (ex: "http://localhost:8545") | +| proxyResolver | _Function_ | none | Custom method to resolve identity of methods managed by a proxy contract. | +| artifactType | _Function_ or _String_ | "truffle-v5" | Compilation artifact format to consume. (See [advanced use](https://github.com/cgewecke/eth-gas-reporter/blob/master/docs/advanced.md).) | +| showMethodSig | _Boolean_ | false | Display complete method signatures. Useful when you have overloaded methods you can't tell apart. | +| maxMethodDiff | _Number_ | undefined | Codechecks failure threshold, triggered when the % diff for any method is greater than `number` (integer) | +| maxDeploymentDiff | _Number_ | undefined | Codechecks failure threshold, triggered when the % diff for any deployment is greater than `number` (integer) | | remoteContracts | _RemoteContract[]_ | `[]` | Contracts pre-deployed to a (forked) network which the reporter should collect gas usage data for. (See [RemoteContract type][44] and [usage example][47]) | [44]: https://github.com/cgewecke/hardhat-gas-reporter/blob/master/src/types.ts#L27 @@ -111,6 +118,21 @@ your own API key [here][55] and set it with the `coinmarketcap` option. [47]: https://github.com/cgewecke/hardhat-gas-reporter/issues/46#issuecomment-728639165 [55]: https://coinmarketcap.com/api/pricing/ +#### `token` and `gasPriceApi` options example + +| Network | token | gasPriceApi | +| ------------------ | ----- | ---------------------------------------------------------------------- | +| Ethereum (default) | ETH | https://api.etherscan.io/api?module=proxy&action=eth_gasPrice | +| Binance | BNB | https://api.bscscan.com/api?module=proxy&action=eth_gasPrice | +| Polygon | MATIC | https://api.polygonscan.com/api?module=proxy&action=eth_gasPrice | +| Avalanche | AVAX | https://api.snowtrace.io/api?module=proxy&action=eth_gasPrice | +| Heco | HT | https://api.hecoinfo.com/api?module=proxy&action=eth_gasPrice | +| Moonriver | MOVR | https://api-moonriver.moonscan.io/api?module=proxy&action=eth_gasPrice | + +These APIs have [rate limits](https://docs.etherscan.io/support/rate-limits). Depending on the usage, it might require an [API Key](https://docs.etherscan.io/getting-started/viewing-api-usage-statistics). + +> NB: Any gas price API call which returns a JSON-RPC response formatted like this is supported: `{"jsonrpc":"2.0","id":73,"result":"0x6fc23ac00"}`. + ## Documentation diff --git a/package.json b/package.json index 6acedf5..393aeef 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,7 @@ }, "dependencies": { "array-uniq": "1.0.3", - "eth-gas-reporter": "^0.2.20", + "eth-gas-reporter": "^0.2.23", "sha1": "^1.1.1" } } diff --git a/src/index.ts b/src/index.ts index 957defd..5044fcd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,7 +29,9 @@ import "./type-extensions" import { EthGasReporterConfig, EthGasReporterOutput, RemoteContract } from "./types"; import { TASK_GAS_REPORTER_MERGE, TASK_GAS_REPORTER_MERGE_REPORTS } from "./task-names"; import { mergeReports } from "./merge-reports"; -const { parseSoliditySources } = require('eth-gas-reporter/lib/utils'); + +const { parseSoliditySources, setGasAndPriceRates } = require('eth-gas-reporter/lib/utils'); +const InternalReporterConfig = require('eth-gas-reporter/lib/config'); let mochaConfig; let resolvedQualifiedNames: string[] @@ -176,10 +178,14 @@ async function getResolvedRemoteContracts( */ subtask(TASK_TEST_RUN_MOCHA_TESTS).setAction( async (args: any, hre, runSuper) => { - const options = getOptions(hre); + let options = getOptions(hre); options.getContracts = getContracts.bind(null, hre.artifacts, options.excludeContracts); if (options.enabled) { + // Fetch data from gas and coin price providers + options = new InternalReporterConfig(options); + await setGasAndPriceRates(options); + mochaConfig = hre.config.mocha || {}; mochaConfig.reporter = "eth-gas-reporter"; mochaConfig.reporterOptions = options; diff --git a/yarn.lock b/yarn.lock index fe9688d..fe6fb98 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1179,9 +1179,12 @@ resolved "https://registry.yarnpkg.com/@solidity-parser/parser/-/parser-0.11.1.tgz#fa840af64840c930f24a9c82c08d4a092a068add" integrity sha512-H8BSBoKE8EubJa0ONqecA2TviT3TnHeC4NpgnAHSUiuhZoQBfPB4L2P9bs8R6AoTW10Endvh3vc+fomVMIDIYQ== -"@solidity-parser/parser@^0.8.2": - version "0.8.2" - resolved "https://registry.yarnpkg.com/@solidity-parser/parser/-/parser-0.8.2.tgz#a6a5e93ac8dca6884a99a532f133beba59b87b69" +"@solidity-parser/parser@^0.14.0": + version "0.14.0" + resolved "https://registry.yarnpkg.com/@solidity-parser/parser/-/parser-0.14.0.tgz#d51f074efb0acce0e953ec48133561ed710cebc0" + integrity sha512-cX0JJRcmPtNUJpzD2K7FdA7qQsTOk1UZnFx2k7qAg9ZRvuaH5NBe5IEdBMXGlmf2+FmjhqbygJ26H8l2SV7aKQ== + dependencies: + antlr4ts "^0.5.0-alpha.4" "@szmarczak/http-timer@^1.1.2": version "1.1.2" @@ -1558,6 +1561,11 @@ ansi-styles@^3.2.0, ansi-styles@^3.2.1: dependencies: color-convert "^1.9.0" +antlr4ts@^0.5.0-alpha.4: + version "0.5.0-alpha.4" + resolved "https://registry.yarnpkg.com/antlr4ts/-/antlr4ts-0.5.0-alpha.4.tgz#71702865a87478ed0b40c0709f422cf14d51652a" + integrity sha512-WPQDt1B74OfPv/IMS2ekXAKkTZIHl88uMetg6q3OTqgFxZ/dxDXI0EWLyZid/1Pe6hTftyg5N7gel5wNAGxXyQ== + anymatch@~3.1.1: version "3.1.1" resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-3.1.1.tgz#c55ecf02185e2469259399310c173ce31233b142" @@ -3394,12 +3402,13 @@ eth-ens-namehash@2.0.8, eth-ens-namehash@^2.0.0, eth-ens-namehash@^2.0.8: idna-uts46-hx "^2.3.1" js-sha3 "^0.5.7" -eth-gas-reporter@^0.2.20: - version "0.2.20" - resolved "https://registry.yarnpkg.com/eth-gas-reporter/-/eth-gas-reporter-0.2.20.tgz#431c144f00cd01cc29ce211a10a4e5a539a84e25" +eth-gas-reporter@^0.2.23: + version "0.2.23" + resolved "https://registry.yarnpkg.com/eth-gas-reporter/-/eth-gas-reporter-0.2.23.tgz#7a2a412b41285298cdad810cf54adac11d406208" + integrity sha512-T8KsVakDEupvQxW3MfFfHDfJ7y8zl2+XhyEQk4hZ3qQsAh/FE27BfFHM9UhqNQvrJLz8zVWnPZWNcARwLT/lsA== dependencies: "@ethersproject/abi" "^5.0.0-beta.146" - "@solidity-parser/parser" "^0.8.2" + "@solidity-parser/parser" "^0.14.0" cli-table3 "^0.5.0" colors "^1.1.2" ethereumjs-util "6.2.0"