diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c0d3e5da..28b713ec9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Current Master +- [#391](https://github.com/poanetwork/nifty-wallet/pull/391) - (Feature) Gas price oracles npm package integration - [#389](https://github.com/poanetwork/nifty-wallet/pull/389) - (Feature) Support 24 words mnemonic phrase - [#388](https://github.com/poanetwork/nifty-wallet/pull/388) - (Feature) "Send all" option for simple coin transfers - [#385](https://github.com/poanetwork/nifty-wallet/pull/385) - (Feature) Display value of current pending tx's nonce on send tx screen diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 3a610c20e..9704b230f 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -61,6 +61,7 @@ import nanoid from 'nanoid' const { importTypes } = require('../../old-ui/app/accounts/import/enums') const { LEDGER, TREZOR } = require('../../old-ui/app/components/connect-hardware/enum') const { ifPOA, ifRSK, getNetworkID, getDPath, setDPath } = require('../../old-ui/app/util') +const { GasPriceOracle } = require('gas-price-oracle') import { PhishingController, @@ -1967,23 +1968,30 @@ module.exports = class MetamaskController extends EventEmitter { */ getGasPriceFromOracles (networkId) { return new Promise(async (resolve, reject) => { - const gasPriceOracleETC = 'https://gasprice-etc.poa.network' - const gasPriceOracleETH = 'https://gasprice.poa.network' - const gasPriceOracle = networkId === CLASSIC_CODE ? - gasPriceOracleETC : networkId === MAINNET_CODE ? gasPriceOracleETH : null - - try { - if (gasPriceOracle) { - const response = await fetch(gasPriceOracle) + if (networkId === MAINNET_CODE) { + const oracle = new GasPriceOracle() + // optional fallbackGasPrices + const fallbackGasPrices = { + instant: 70, fast: 31, standard: 20, low: 7, + } + oracle.gasPrices(fallbackGasPrices).then((gasPrices) => { + gasPrices && (gasPrices.standard || gasPrices.fast) ? resolve(gasPrices.standard || gasPrices.fast) : reject() + }) + } else if (networkId === CLASSIC_CODE) { + const gasPriceOracleETC = 'https://gasprice-etc.poa.network' + try { + const response = await fetch(gasPriceOracleETC) const parsedResponse = await response.json() if (parsedResponse && (parsedResponse.standard || parsedResponse.fast)) { resolve(parsedResponse.standard || parsedResponse.fast) } else { - reject() + reject('Empty response from gas price oracle') } + } catch (error) { + reject(error) } - } catch (error) { - reject(error) + } else { + reject(`No gas price oracles for ${networkId}`) } }) } diff --git a/package-lock.json b/package-lock.json index 23a1ce76f..606f1a786 100644 --- a/package-lock.json +++ b/package-lock.json @@ -37924,6 +37924,30 @@ } } }, + "gas-price-oracle": { + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/gas-price-oracle/-/gas-price-oracle-0.1.4.tgz", + "integrity": "sha512-lIxzxu5LtkdUewaFl6MlBU2Me6rWL58ENeAOD2AM/KZYB0Os7EcIBq87ixTced4UF2zb9bzPcVPoEVuH7icOJQ==", + "requires": { + "axios": "^0.19.2", + "bignumber.js": "^9.0.0" + }, + "dependencies": { + "axios": { + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz", + "integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==", + "requires": { + "follow-redirects": "1.5.10" + } + }, + "bignumber.js": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.0.tgz", + "integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A==" + } + } + }, "gather-stream": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/gather-stream/-/gather-stream-1.0.0.tgz", diff --git a/package.json b/package.json index 97a3fdceb..705f2cace 100644 --- a/package.json +++ b/package.json @@ -143,6 +143,7 @@ "fast-levenshtein": "^2.0.6", "fuse.js": "^3.2.0", "gaba": "^1.9.3", + "gas-price-oracle": "^0.1.4", "human-standard-token-abi": "^2.0.0", "idb-global": "^2.1.0", "iframe-stream": "^3.0.0", diff --git a/test/unit/app/controllers/metamask-controller-test.js b/test/unit/app/controllers/metamask-controller-test.js index 6ceb5da81..aab54b8b8 100644 --- a/test/unit/app/controllers/metamask-controller-test.js +++ b/test/unit/app/controllers/metamask-controller-test.js @@ -137,7 +137,7 @@ describe('MetaMaskController', function () { }, } - const gasPrice = await metamaskController.getGasPrice() + const gasPrice = await metamaskController.getGasPriceFromBlocks(1) assert.equal(gasPrice, '0x174876e800', 'accurately estimates 65th percentile accepted gas price') metamaskController.recentBlocksController = realRecentBlocksController