From bb58318b6f1b0e9e9e732d721b41ad6fb068d4cf Mon Sep 17 00:00:00 2001 From: Danil Kovtonyuk Date: Thu, 3 Jun 2021 15:16:49 +0300 Subject: [PATCH] add defaultFallbackGasPrices option --- README.md | 6 ++++++ package.json | 2 +- src/config/index.ts | 20 ++++---------------- src/index.ts | 16 ++++++++-------- src/types.ts | 11 +++++++---- 5 files changed, 26 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 2d049c0..d126eb4 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,12 @@ const options = { chainId: 1, defaultRpc: 'https://api.mycryptoapi.com/eth', timeout: 10000, + defaultFallbackGasPrices: { + instant: 28, + fast: 22, + standard: 17, + low: 11, + }, }; const oracle = new GasPriceOracle(options); // optional fallbackGasPrices diff --git a/package.json b/package.json index b1607a5..77d0f8b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gas-price-oracle", - "version": "0.3.0", + "version": "0.3.1", "description": "Gas Price Oracle library for Ethereum dApps.", "main": "lib/index.js", "homepage": "https://github.com/peppersec/gas-price-oracle", diff --git a/src/config/index.ts b/src/config/index.ts index 1163c87..8ec2bee 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -1,12 +1,6 @@ import { NetworkConfig } from '../types'; -import { - onChainOracles as mainnetOnchainOracles, - offChainOracles as mainnetOffChainOracles, -} from './mainnet'; -import { - onChainOracles as binanceOnchainOracles, - offChainOracles as binanceOffchainOracles, -} from './binance'; +import mainnetOracles from './mainnet'; +import binanceOracles from './binance'; export enum ChainId { MAINNET = 1, @@ -14,12 +8,6 @@ export enum ChainId { } export const networks: NetworkConfig = { - [ChainId.MAINNET]: { - onChainOracles: mainnetOnchainOracles, - offChainOracles: mainnetOffChainOracles, - }, - [ChainId.BINANCE]: { - onChainOracles: binanceOnchainOracles, - offChainOracles: binanceOffchainOracles, - }, + [ChainId.MAINNET]: mainnetOracles, + [ChainId.BINANCE]: binanceOracles, }; diff --git a/src/index.ts b/src/index.ts index 8c69bac..f8b352f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,6 +12,7 @@ import { } from './types'; import BigNumber from 'bignumber.js'; +const defaultFastGas = 22; export class GasPriceOracle { lastGasPrice: GasPrice; offChainOracles: OffChainOracles; @@ -20,6 +21,12 @@ export class GasPriceOracle { chainId: ChainId.MAINNET, defaultRpc: 'https://api.mycryptoapi.com/eth', timeout: 10000, + defaultFallbackGasPrices: { + instant: defaultFastGas * 1.3, + fast: defaultFastGas, + standard: defaultFastGas * 0.85, + low: defaultFastGas * 0.5, + }, }; constructor(options?: Options) { @@ -175,14 +182,7 @@ export class GasPriceOracle { } async gasPrices(fallbackGasPrices?: GasPrice, median = true): Promise { - const defaultFastGas = 22; - const defaultFallbackGasPrices = { - instant: defaultFastGas * 1.3, - fast: defaultFastGas, - standard: defaultFastGas * 0.85, - low: defaultFastGas * 0.5, - }; - this.lastGasPrice = this.lastGasPrice || fallbackGasPrices || defaultFallbackGasPrices; + this.lastGasPrice = this.lastGasPrice || fallbackGasPrices || this.configuration.defaultFallbackGasPrices; try { this.lastGasPrice = median ? await this.fetchMedianGasPriceOffChain() diff --git a/src/types.ts b/src/types.ts index 83ea57a..558428e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -21,6 +21,11 @@ export type OnChainOracle = { export type OnChainOracles = { [key: string]: OnChainOracle }; +export type AllOracles = { + offChainOracles: OffChainOracles; + onChainOracles: OnChainOracles; +}; + export type GasPrice = { [key in GasPriceKey]: number; }; @@ -31,13 +36,11 @@ export type Options = { chainId?: number; defaultRpc?: string; timeout?: number; + defaultFallbackGasPrices?: GasPrice; }; export type Config = Required; export type NetworkConfig = { - [key in number]: { - offChainOracles: OffChainOracles; - onChainOracles: OnChainOracles; - }; + [key in number]: AllOracles; };