fix: update oracles

This commit is contained in:
Danil Kovtonyuk 2021-11-15 16:17:34 +10:00
parent b13a4252ab
commit 6be967e8c9
10 changed files with 169 additions and 98 deletions

@ -9,9 +9,8 @@ A library that has a collection of onchain and offchain gas price oracle URLs
Current offchain list: Current offchain list:
- https://ethgasstation.info/json/ethgasAPI.json - https://ethgasstation.info/json/ethgasAPI.json
- https://www.etherchain.org/api/gasPriceOracle - https://etherchain.org/api/gasnow
- https://gasprice.poa.network/ - https://blockscout.com/eth/mainnet/api/v1/gas-price-oracle
- https://www.gasnow.org/api/v3/gas/price
Current onchain list: Current onchain list:
@ -21,13 +20,13 @@ Current onchain list:
Current offchain list: Current offchain list:
- https://bscgas.info/ - https://ztake.org/
### xDAI Chain ### xDAI Chain
Current offchain list: Current offchain list:
- https://www.xdaichain.com/for-developers/developer-resources/gas-price-oracle - https://blockscout.com/xdai/mainnet/api/v1/gas-price-oracle
### Polygon (Matic) Network ### Polygon (Matic) Network
@ -35,6 +34,18 @@ Current offchain list:
- https://gasstation-mainnet.matic.network/ - https://gasstation-mainnet.matic.network/
### Arbitrum One
Current offchain list:
- https://ztake.org/
### Avalanche Mainnet
Current offchain list:
- https://ztake.org/
## Installation ## Installation
`npm i gas-price-oracle` `npm i gas-price-oracle`

@ -1,6 +1,6 @@
{ {
"name": "gas-price-oracle", "name": "gas-price-oracle",
"version": "0.4.0", "version": "0.4.1",
"description": "Gas Price Oracle library for Ethereum dApps.", "description": "Gas Price Oracle library for Ethereum dApps.",
"main": "lib/index.js", "main": "lib/index.js",
"homepage": "https://github.com/peppersec/gas-price-oracle", "homepage": "https://github.com/peppersec/gas-price-oracle",

23
src/config/arbitrum.ts Normal file

@ -0,0 +1,23 @@
import { OffChainOracle, OffChainOracles, OnChainOracles } from '../types';
const ztake: OffChainOracle = {
name: 'ztake',
url: 'https://blockchains.ztake.org/api/h6WnmwNqw9CAJHzej5W4gD6LZ9n7v8EK/gasprice/arb/',
instantPropertyName: 'percentile_90',
fastPropertyName: 'percentile_80',
standardPropertyName: 'percentile_60',
lowPropertyName: 'percentile_30',
denominator: 1,
additionalDataProperty: null,
};
export const offChainOracles: OffChainOracles = {
ztake,
};
export const onChainOracles: OnChainOracles = {};
export default {
offChainOracles,
onChainOracles,
};

23
src/config/avalanche.ts Normal file

@ -0,0 +1,23 @@
import { OffChainOracle, OffChainOracles, OnChainOracles } from '../types';
const ztake: OffChainOracle = {
name: 'ztake',
url: 'https://blockchains.ztake.org/api/h6WnmwNqw9CAJHzej5W4gD6LZ9n7v8EK/gasprice/avax/',
instantPropertyName: 'percentile_90',
fastPropertyName: 'percentile_80',
standardPropertyName: 'percentile_60',
lowPropertyName: 'percentile_30',
denominator: 1,
additionalDataProperty: null,
};
export const offChainOracles: OffChainOracles = {
ztake,
};
export const onChainOracles: OnChainOracles = {};
export default {
offChainOracles,
onChainOracles,
};

@ -1,23 +0,0 @@
import { OffChainOracle, OffChainOracles, OnChainOracles } from '../types';
const bscgas: OffChainOracle = {
name: 'bscgas',
url: 'https://bscgas.info/gas',
instantPropertyName: 'instant',
fastPropertyName: 'fast',
standardPropertyName: 'standard',
lowPropertyName: 'slow',
denominator: 1,
additionalDataProperty: null,
};
export const offChainOracles: OffChainOracles = {
bscgas,
};
export const onChainOracles: OnChainOracles = {};
export default {
offChainOracles,
onChainOracles,
};

23
src/config/bsc.ts Normal file

@ -0,0 +1,23 @@
import { OffChainOracle, OffChainOracles, OnChainOracles } from '../types';
const ztake: OffChainOracle = {
name: 'ztake',
url: 'https://blockchains.ztake.org/api/h6WnmwNqw9CAJHzej5W4gD6LZ9n7v8EK/gasprice/bsc/',
instantPropertyName: 'percentile_90',
fastPropertyName: 'percentile_80',
standardPropertyName: 'percentile_60',
lowPropertyName: 'percentile_30',
denominator: 1,
additionalDataProperty: null,
};
export const offChainOracles: OffChainOracles = {
ztake,
};
export const onChainOracles: OnChainOracles = {};
export default {
offChainOracles,
onChainOracles,
};

@ -1,20 +1,26 @@
import { NetworkConfig } from '../types'; import { NetworkConfig } from '../types';
import mainnetOracles from './mainnet'; import mainnetOracles from './mainnet';
import binanceOracles from './binance'; import bscOracles from './bsc';
import xdaiOracles from './xdai'; import xdaiOracles from './xdai';
import polygonOracles from './polygon'; import polygonOracles from './polygon';
import arbitrumOracles from './arbitrum';
import avalancheOracles from './avalanche';
export enum ChainId { export enum ChainId {
MAINNET = 1, MAINNET = 1,
BINANCE = 56, BSC = 56,
XDAI = 100, XDAI = 100,
POLYGON = 137, POLYGON = 137,
ARBITRUM = 42161,
AVALANCHE = 43114,
} }
export const networks: NetworkConfig = { export const NETWORKS: NetworkConfig = {
[ChainId.MAINNET]: mainnetOracles, [ChainId.MAINNET]: mainnetOracles,
[ChainId.BINANCE]: binanceOracles, [ChainId.BSC]: bscOracles,
[ChainId.XDAI]: xdaiOracles, [ChainId.XDAI]: xdaiOracles,
[ChainId.POLYGON]: polygonOracles, [ChainId.POLYGON]: polygonOracles,
[ChainId.ARBITRUM]: arbitrumOracles,
[ChainId.AVALANCHE]: avalancheOracles,
}; };

@ -13,37 +13,26 @@ const ethgasstation: OffChainOracle = {
const etherchain: OffChainOracle = { const etherchain: OffChainOracle = {
name: 'etherchain', name: 'etherchain',
url: 'https://www.etherchain.org/api/gasPriceOracle', url: 'https://etherchain.org/api/gasnow',
instantPropertyName: 'fastest',
fastPropertyName: 'fast',
standardPropertyName: 'standard',
lowPropertyName: 'safeLow',
denominator: 1,
additionalDataProperty: null,
};
const poa: OffChainOracle = {
name: 'poa',
url: 'https://gasprice.poa.network/',
instantPropertyName: 'instant',
fastPropertyName: 'fast',
standardPropertyName: 'standard',
lowPropertyName: 'slow',
denominator: 1,
additionalDataProperty: null,
};
const gasNow: OffChainOracle = {
name: 'gasNow',
url: 'https://www.gasnow.org/api/v3/gas/price?utm_source=gas-price-oracle',
instantPropertyName: 'rapid', instantPropertyName: 'rapid',
fastPropertyName: 'fast', fastPropertyName: 'fast',
standardPropertyName: 'standard', standardPropertyName: 'standard',
lowPropertyName: 'slow', lowPropertyName: 'slow',
denominator: 1e9, denominator: 1,
additionalDataProperty: 'data', 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 anyblock: OffChainOracle = { const anyblock: OffChainOracle = {
name: 'anyblock', name: 'anyblock',
url: 'https://api.anyblock.tools/ethereum/latest-minimum-gasprice', url: 'https://api.anyblock.tools/ethereum/latest-minimum-gasprice',
@ -65,8 +54,7 @@ const chainlink: OnChainOracle = {
export const offChainOracles: OffChainOracles = { export const offChainOracles: OffChainOracles = {
ethgasstation, ethgasstation,
anyblock, anyblock,
gasNow, blockscout,
poa,
etherchain, etherchain,
}; };

@ -1,5 +1,8 @@
import axios from 'axios'; import axios from 'axios';
import { ChainId, networks } from './config'; import BigNumber from 'bignumber.js';
import { ChainId, NETWORKS } from './config';
import { import {
Config, Config,
Options, Options,
@ -10,7 +13,6 @@ import {
OnChainOracles, OnChainOracles,
OffChainOracles, OffChainOracles,
} from './types'; } from './types';
import BigNumber from 'bignumber.js';
const defaultFastGas = 22; const defaultFastGas = 22;
export class GasPriceOracle { export class GasPriceOracle {
@ -34,7 +36,7 @@ export class GasPriceOracle {
Object.assign(this.configuration, options); Object.assign(this.configuration, options);
} }
const network = networks[this.configuration.chainId]; const network = NETWORKS[this.configuration.chainId];
if (network) { if (network) {
const { offChainOracles, onChainOracles } = network; const { offChainOracles, onChainOracles } = network;
@ -215,6 +217,8 @@ export class GasPriceOracle {
async gasPrices(fallbackGasPrices?: GasPrice, median = true): Promise<GasPrice> { async gasPrices(fallbackGasPrices?: GasPrice, median = true): Promise<GasPrice> {
this.lastGasPrice = this.lastGasPrice || fallbackGasPrices || this.configuration.defaultFallbackGasPrices; this.lastGasPrice = this.lastGasPrice || fallbackGasPrices || this.configuration.defaultFallbackGasPrices;
if (Object.keys(this.offChainOracles).length > 0) {
try { try {
this.lastGasPrice = median this.lastGasPrice = median
? await this.fetchMedianGasPriceOffChain() ? await this.fetchMedianGasPriceOffChain()
@ -223,7 +227,9 @@ export class GasPriceOracle {
} catch (e) { } 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. Trying onchain ones...');
} }
}
if (Object.keys(this.onChainOracles).length > 0) {
try { try {
const fastGas = await this.fetchGasPricesOnChain(); const fastGas = await this.fetchGasPricesOnChain();
this.lastGasPrice = { this.lastGasPrice = {
@ -236,6 +242,7 @@ export class GasPriceOracle {
} catch (e) { } 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. Trying from default RPC...');
} }
}
try { try {
const fastGas = await this.fetchGasPriceFromRpc(); const fastGas = await this.fetchGasPriceFromRpc();

@ -1,12 +1,16 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */ /* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/no-var-requires */ /* eslint-disable @typescript-eslint/no-var-requires */
import { GasPrice, OffChainOracle } from '../src/types';
import mockery from 'mockery';
import chai from 'chai'; import chai from 'chai';
import mockery from 'mockery';
import { ChainId, NETWORKS } from '../src/config';
import { GasPriceOracle } from '../src/index'; import { GasPriceOracle } from '../src/index';
import { GasPrice, OffChainOracle } from '../src/types';
chai.use(require('chai-as-promised')); chai.use(require('chai-as-promised'));
chai.should(); chai.should();
let oracle = new GasPriceOracle(); let oracle = new GasPriceOracle();
let { onChainOracles, offChainOracles } = oracle; let { onChainOracles, offChainOracles } = oracle;
@ -237,8 +241,15 @@ describe('fetchMedianGasPriceOffChain', function () {
}); });
describe('askOracle', function () { describe('askOracle', function () {
it('all oracles should answer', async function () { const chains = Object.keys(NETWORKS).map(id => Number(id));
chains.forEach(chainId => {
describe(`all ${ChainId[chainId]} oracles should answer`, function () {
oracle = new GasPriceOracle({ chainId });
({ offChainOracles } = oracle);
for (const o of Object.values(offChainOracles) as Array<OffChainOracle>) { for (const o of Object.values(offChainOracles) as Array<OffChainOracle>) {
it(`check ${o.name}`, async function () {
try { try {
const gas: GasPrice = await oracle.askOracle(o); const gas: GasPrice = await oracle.askOracle(o);
@ -255,8 +266,10 @@ describe('askOracle', function () {
console.error(`Failed to get data from ${o.name} oracle`); console.error(`Failed to get data from ${o.name} oracle`);
throw new Error(e); throw new Error(e);
} }
});
} }
}); });
});
}); });
after('after', function () { after('after', function () {