Skip to content
This repository has been archived by the owner on Mar 17, 2024. It is now read-only.

Commit

Permalink
Replace request-promise-native with axios / avoid default price API c…
Browse files Browse the repository at this point in the history
…alls (#299)
  • Loading branch information
cgewecke committed Sep 30, 2023
1 parent 92e3806 commit ce6b202
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 108 deletions.
51 changes: 25 additions & 26 deletions README.md
Expand Up @@ -74,35 +74,34 @@ Complete [set-up guide here](https://github.com/cgewecke/eth-gas-reporter/blob/m
:warning: **CoinMarketCap API change** :warning:

Beginning March 2020, CoinMarketCap requires an API key to access currency market
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.
price data. The reporter uses an unprotected . You can get your own API key [here][55] 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.

| Option | Type | Default | Description |
| ----------------- | ---------------------- | -------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 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 |
| forceConsoleOutput| _Boolean_ | false | Print report output on console |
| 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[]_ | [] | Contract names to exclude from report. Ex: `['Migrations']` |
| 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) |
**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 |
| ------------------ | ---------------------- | -------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 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 |
| forceConsoleOutput | _Boolean_ | false | Print report output on console |
| 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[]_ | [] | Contract names to exclude from report. Ex: `['Migrations']` |
| 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) |

[55]: https://coinmarketcap.com/api/pricing/

Expand Down
3 changes: 1 addition & 2 deletions lib/config.js
Expand Up @@ -12,8 +12,7 @@ class Config {
this.gasPriceApi =
options.gasPriceApi ||
"https://api.etherscan.io/api?module=proxy&action=eth_gasPrice";
this.coinmarketcap =
options.coinmarketcap || "d25b5576-a4ee-41be-bb2b-aca2ba3ae5d8";
this.coinmarketcap = options.coinmarketcap || null;
this.ethPrice = options.ethPrice || null;
this.gasPrice = options.gasPrice || null;
this.outputFile = options.outputFile || null;
Expand Down
31 changes: 17 additions & 14 deletions lib/utils.js
@@ -1,6 +1,6 @@
const fs = require("fs");
const parser = require("@solidity-parser/parser");
const request = require("request-promise-native");
const axios = require("axios");
const path = require("path");
const read = require("fs-readdir-recursive");
const colors = require("colors/safe");
Expand Down Expand Up @@ -175,25 +175,29 @@ const utils = {
* @param {Object} config
*/
setGasAndPriceRates: async function(config) {
if (config.ethPrice && config.gasPrice) return;
if ((config.ethPrice && config.gasPrice) || !config.coinmarketcap) return;

const token = config.token.toUpperCase();
const gasPriceApi = config.gasPriceApi;
const coinmarketcap =
`https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/` +
`latest?symbol=${token}&CMC_PRO_API_KEY=${config.coinmarketcap}&convert=`;

const axiosInstance = axios.create({
baseURL: `https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/`
});

const requestArgs = `latest?symbol=${token}&CMC_PRO_API_KEY=${
config.coinmarketcap
}&convert=`;

const currencyKey = config.currency.toUpperCase();
const currencyPath = `${coinmarketcap}${currencyKey}`;
const currencyPath = `${requestArgs}${currencyKey}`;

// Currency market data: coinmarketcap
if (!config.ethPrice) {
try {
let response = await request.get(currencyPath);
response = JSON.parse(response);
config.ethPrice = response.data[token].quote[currencyKey].price.toFixed(
2
);
let response = await axiosInstance.get(currencyPath);
config.ethPrice = response.data.data[token].quote[
currencyKey
].price.toFixed(2);
} catch (error) {
config.ethPrice = null;
}
Expand All @@ -202,10 +206,9 @@ const utils = {
// Gas price data: etherscan (or `gasPriceAPI`)
if (!config.gasPrice) {
try {
let response = await request.get(gasPriceApi);
response = JSON.parse(response);
let response = await axiosInstance.get(gasPriceApi);
config.gasPrice = Math.round(
parseInt(response.result, 16) / Math.pow(10, 9)
parseInt(response.data.result, 16) / Math.pow(10, 9)
);
} catch (error) {
config.gasPrice = config.defaultGasPrice;
Expand Down

0 comments on commit ce6b202

Please sign in to comment.