8 Commits

Author SHA1 Message Date
Danil Kovtonyuk
be905f2b71 fix: mainnet oracles 2022-03-18 05:19:29 +10:00
Danil Kovtonyuk
4373641a30 fix: normalize result values 2021-11-16 03:46:43 +10:00
Danil Kovtonyuk
76f8cb036d fix: default values 2021-11-16 01:51:22 +10:00
Danil Kovtonyuk
64c5a68639 fix: update dependencies 2021-11-15 16:39:20 +10:00
dependabot[bot]
1dbd778166 Bump axios from 0.19.2 to 0.21.2
Bumps [axios](https://github.com/axios/axios) from 0.19.2 to 0.21.2.
- [Release notes](https://github.com/axios/axios/releases)
- [Changelog](https://github.com/axios/axios/blob/master/CHANGELOG.md)
- [Commits](https://github.com/axios/axios/compare/v0.19.2...v0.21.2)

---
updated-dependencies:
- dependency-name: axios
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2021-11-15 16:38:05 +10:00
dependabot[bot]
47089200af Bump glob-parent from 5.1.1 to 5.1.2
Bumps [glob-parent](https://github.com/gulpjs/glob-parent) from 5.1.1 to 5.1.2.
- [Release notes](https://github.com/gulpjs/glob-parent/releases)
- [Changelog](https://github.com/gulpjs/glob-parent/blob/main/CHANGELOG.md)
- [Commits](https://github.com/gulpjs/glob-parent/compare/v5.1.1...v5.1.2)

---
updated-dependencies:
- dependency-name: glob-parent
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2021-11-15 16:37:53 +10:00
dependabot[bot]
4cf2521880 Bump lodash from 4.17.20 to 4.17.21
Bumps [lodash](https://github.com/lodash/lodash) from 4.17.20 to 4.17.21.
- [Release notes](https://github.com/lodash/lodash/releases)
- [Commits](https://github.com/lodash/lodash/compare/4.17.20...4.17.21)

Signed-off-by: dependabot[bot] <support@github.com>
2021-11-15 16:37:41 +10:00
dependabot[bot]
cd5bca4963 Bump y18n from 4.0.0 to 4.0.1
Bumps [y18n](https://github.com/yargs/y18n) from 4.0.0 to 4.0.1.
- [Release notes](https://github.com/yargs/y18n/releases)
- [Changelog](https://github.com/yargs/y18n/blob/master/CHANGELOG.md)
- [Commits](https://github.com/yargs/y18n/commits)

Signed-off-by: dependabot[bot] <support@github.com>
2021-11-15 16:37:19 +10:00
5 changed files with 119 additions and 74 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "gas-price-oracle",
"version": "0.4.1",
"version": "0.4.5",
"description": "Gas Price Oracle library for Ethereum dApps.",
"main": "lib/index.js",
"homepage": "https://github.com/peppersec/gas-price-oracle",
@@ -47,7 +47,7 @@
"typescript": "^4.0.3"
},
"dependencies": {
"axios": "^0.19.2",
"axios": "^0.21.2",
"bignumber.js": "^9.0.0"
},
"files": [

View File

@@ -18,31 +18,31 @@ const etherchain: OffChainOracle = {
fastPropertyName: 'fast',
standardPropertyName: 'standard',
lowPropertyName: 'slow',
denominator: 1,
denominator: 1e9,
additionalDataProperty: 'data',
};
const blockscout: OffChainOracle = {
name: 'blockscout',
url: 'https://blockscout.com/eth/mainnet/api/v1/gas-price-oracle',
instantPropertyName: 'fast',
fastPropertyName: 'average',
standardPropertyName: 'slow',
lowPropertyName: 'slow',
denominator: 1,
additionalDataProperty: null,
};
// const blockscout: OffChainOracle = {
// name: 'blockscout',
// url: 'https://blockscout.com/eth/mainnet/api/v1/gas-price-oracle',
// instantPropertyName: 'fast',
// fastPropertyName: 'average',
// standardPropertyName: 'slow',
// lowPropertyName: 'slow',
// denominator: 1,
// additionalDataProperty: null,
// };
const anyblock: OffChainOracle = {
name: 'anyblock',
url: 'https://api.anyblock.tools/ethereum/latest-minimum-gasprice',
instantPropertyName: 'instant',
fastPropertyName: 'fast',
standardPropertyName: 'standard',
lowPropertyName: 'slow',
denominator: 1,
additionalDataProperty: null,
};
// const anyblock: OffChainOracle = {
// name: 'anyblock',
// url: 'https://api.anyblock.tools/ethereum/latest-minimum-gasprice',
// instantPropertyName: 'instant',
// fastPropertyName: 'fast',
// standardPropertyName: 'standard',
// lowPropertyName: 'slow',
// denominator: 1,
// additionalDataProperty: null,
// };
const chainlink: OnChainOracle = {
name: 'chainlink',
@@ -53,8 +53,8 @@ const chainlink: OnChainOracle = {
export const offChainOracles: OffChainOracles = {
ethgasstation,
anyblock,
blockscout,
// anyblock,
// blockscout,
etherchain,
};

View File

@@ -17,8 +17,8 @@ import {
const defaultFastGas = 22;
export class GasPriceOracle {
lastGasPrice: GasPrice;
offChainOracles: OffChainOracles;
onChainOracles: OnChainOracles;
offChainOracles: OffChainOracles = {};
onChainOracles: OnChainOracles = {};
configuration: Config = {
chainId: ChainId.MAINNET,
defaultRpc: 'https://api.mycryptoapi.com/eth',
@@ -36,6 +36,8 @@ export class GasPriceOracle {
Object.assign(this.configuration, options);
}
this.configuration.defaultFallbackGasPrices = this.normalize(this.configuration.defaultFallbackGasPrices);
const network = NETWORKS[this.configuration.chainId];
if (network) {
@@ -225,38 +227,37 @@ export class GasPriceOracle {
: await this.fetchGasPricesOffChain();
return this.lastGasPrice;
} catch (e) {
console.log('Failed to fetch gas prices from offchain oracles. Trying onchain ones...');
console.log('Failed to fetch gas prices from offchain oracles...');
}
}
if (Object.keys(this.onChainOracles).length > 0) {
try {
const fastGas = await this.fetchGasPricesOnChain();
this.lastGasPrice = {
instant: fastGas * 1.3,
fast: fastGas,
standard: fastGas * 0.85,
low: fastGas * 0.5,
};
this.lastGasPrice = this.categorize(fastGas);
return this.lastGasPrice;
} catch (e) {
console.log('Failed to fetch gas prices from onchain oracles. Trying from default RPC...');
console.log('Failed to fetch gas prices from onchain oracles...');
}
}
try {
const fastGas = await this.fetchGasPriceFromRpc();
this.lastGasPrice = {
instant: fastGas * 1.3,
fast: fastGas,
standard: fastGas * 0.85,
low: fastGas * 0.5,
};
this.lastGasPrice = this.categorize(fastGas);
return this.lastGasPrice;
} catch (e) {
console.log('Failed to fetch gas prices from default RPC. Last known gas will be returned');
}
return this.lastGasPrice;
return this.normalize(this.lastGasPrice);
}
categorize(gasPrice: number): GasPrice {
return this.normalize({
instant: gasPrice * 1.3,
fast: gasPrice,
standard: gasPrice * 0.85,
low: gasPrice * 0.5,
});
}
addOffChainOracle(oracle: OffChainOracle): void {

View File

@@ -2,6 +2,7 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import chai from 'chai';
import mockery from 'mockery';
import BigNumber from 'bignumber.js';
import { ChainId, NETWORKS } from '../src/config';
import { GasPriceOracle } from '../src/index';
@@ -240,6 +241,63 @@ describe('fetchMedianGasPriceOffChain', function () {
});
});
describe('normalize result values', function () {
const wrongDecimalsGas = {
instant: 1.1,
fast: 2.12345678901,
standard: 3.12345678901,
low: 3.1234567890123456789,
};
const checkDecimals = (gas: GasPrice) => {
const gasPrices: number[] = Object.values(gas);
for (const gas of gasPrices) {
new BigNumber(gas).dp().should.be.at.most(9);
}
};
it('default fallback should be normalized', function () {
mockery.enable({ useCleanCache: true, warnOnUnregistered: false });
const { GasPriceOracle } = require('../src/index');
oracle = new GasPriceOracle({
defaultFallbackGasPrices: wrongDecimalsGas,
});
const { configuration } = oracle;
checkDecimals(configuration.defaultFallbackGasPrices);
mockery.disable();
});
it('fallback should be normalized', async function () {
mockery.enable({ useCleanCache: true, warnOnUnregistered: false });
const { GasPriceOracle } = require('../src/index');
oracle = new GasPriceOracle();
const gas = await oracle.gasPrices(wrongDecimalsGas);
checkDecimals(gas);
mockery.disable();
});
it('rpc fallback should be normalized', async function () {
const { GasPriceOracle } = require('../src/index');
oracle = new GasPriceOracle({ chainId: ChainId.ARBITRUM, defaultRpc: 'https://arb1.arbitrum.io/rpc' });
const { onChainOracles, offChainOracles } = oracle;
Object.keys(onChainOracles).forEach(chainOracle => oracle.removeOnChainOracle(chainOracle));
Object.keys(offChainOracles).forEach(chainOracle => oracle.removeOffChainOracle(chainOracle));
const gas = await oracle.gasPrices();
checkDecimals(gas);
});
});
describe('askOracle', function () {
const chains = Object.keys(NETWORKS).map(id => Number(id));

View File

@@ -261,12 +261,12 @@ astral-regex@^1.0.0:
resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-1.0.0.tgz#6c8c3fb827dd43ee3918f27b82782ab7658a6fd9"
integrity sha512-+Ryf6g3BKoRc7jfp7ad8tM4TtMiaWvbF/1/sQcZPkkS7ag3D5nMBCe2UfOTONtAkaG0tO0ij3C5Lwmf1EiyjHg==
axios@^0.19.2:
version "0.19.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.2.tgz#3ea36c5d8818d0d5f8a8a97a6d36b86cdc00cb27"
integrity sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==
axios@^0.21.2:
version "0.21.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.2.tgz#21297d5084b2aeeb422f5d38e7be4fbb82239017"
integrity sha512-87otirqUw3e8CzHTMO+/9kh/FSgXt/eVDvipijwDtEuwbkySWZ9SBm6VEubmJ/kLKEoLQV/POhxXFb66bfekfg==
dependencies:
follow-redirects "1.5.10"
follow-redirects "^1.14.0"
balanced-match@^1.0.0:
version "1.0.0"
@@ -428,13 +428,6 @@ debug@3.2.6:
dependencies:
ms "^2.1.1"
debug@=3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==
dependencies:
ms "2.0.0"
debug@^4.0.1, debug@^4.1.1:
version "4.2.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.2.0.tgz#7f150f93920e94c58f5574c2fd01a3110effe7f1"
@@ -757,12 +750,10 @@ flatted@^2.0.0:
resolved "https://registry.yarnpkg.com/flatted/-/flatted-2.0.2.tgz#4575b21e2bcee7434aa9be662f4b7b5f9c2b5138"
integrity sha512-r5wGx7YeOwNWNlCA0wQ86zKyDLMQr+/RB8xy74M4hTphfmjlijTSSXGuH8rnvKZnfT9i+75zmd8jcKdMR4O6jA==
follow-redirects@1.5.10:
version "1.5.10"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a"
integrity sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==
dependencies:
debug "=3.1.0"
follow-redirects@^1.14.0:
version "1.14.4"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.4.tgz#838fdf48a8bbdd79e52ee51fb1c94e3ed98b9379"
integrity sha512-zwGkiSXC1MUJG/qmeIFH2HBJx9u0V46QGUe3YR1fXG8bXQxq7fLj0RjLZQ5nubr9qNJUZrH+xUcwXEoXNpfS+g==
fs.realpath@^1.0.0:
version "1.0.0"
@@ -800,9 +791,9 @@ get-stdin@^6.0.0:
integrity sha512-jp4tHawyV7+fkkSKyvjuLZswblUtz+SQKzSWnBbii16BuZksJlU1wuBYXY75r+duh/llF1ur6oNwi+2ZzjKZ7g==
glob-parent@^5.0.0, glob-parent@^5.1.0, glob-parent@~5.1.0:
version "5.1.1"
resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-5.1.1.tgz#b6c1ef417c4e5663ea498f1c45afac6916bbc229"
integrity sha512-FnI+VGOpnlGHWZxthPGR+QhR78fuiK0sNLkHQv+bL9fQi57lNNdquIbna/WrfROrolq8GK5Ek6BiMwqL/voRYQ==
version "5.1.2"
resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-5.1.2.tgz#869832c58034fe68a4093c17dc15e8340d8401c4"
integrity sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==
dependencies:
is-glob "^4.0.1"
@@ -1033,9 +1024,9 @@ locate-path@^3.0.0:
path-exists "^3.0.0"
lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19:
version "4.17.20"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.20.tgz#b44a9b6297bcb698f1c51a3545a2b3b368d59c52"
integrity sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
log-symbols@3.0.0:
version "3.0.0"
@@ -1116,11 +1107,6 @@ mockery@^2.1.0:
resolved "https://registry.yarnpkg.com/mockery/-/mockery-2.1.0.tgz#5b0aef1ff564f0f8139445e165536c7909713470"
integrity sha512-9VkOmxKlWXoDO/h1jDZaS4lH33aWfRiJiNT/tKj+8OGzrcFDLo8d0syGdbsc3Bc4GvRXPb+NMMvojotmuGJTvA==
ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
integrity sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=
ms@2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.1.tgz#30a5864eb3ebb0a66f2ebe6d727af06a09d86e0a"
@@ -1609,9 +1595,9 @@ write@1.0.3:
mkdirp "^0.5.1"
y18n@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/y18n/-/y18n-4.0.0.tgz#95ef94f85ecc81d007c264e190a120f0a3c8566b"
integrity sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==
version "4.0.1"
resolved "https://registry.yarnpkg.com/y18n/-/y18n-4.0.1.tgz#8db2b83c31c5d75099bb890b23f3094891e247d4"
integrity sha512-wNcy4NvjMYL8gogWWYAO7ZFWFfHcbdbE57tZO8e4cbpj8tfUcwrwqSl3ad8HxpYWCdXcJUCeKKZS62Av1affwQ==
yargs-parser@13.1.2, yargs-parser@^13.1.2:
version "13.1.2"