add defaultFallbackGasPrices option

This commit is contained in:
Danil Kovtonyuk 2021-06-03 15:16:49 +03:00
parent 32c256bc4e
commit bb58318b6f
5 changed files with 26 additions and 29 deletions

@ -43,6 +43,12 @@ const options = {
chainId: 1, chainId: 1,
defaultRpc: 'https://api.mycryptoapi.com/eth', defaultRpc: 'https://api.mycryptoapi.com/eth',
timeout: 10000, timeout: 10000,
defaultFallbackGasPrices: {
instant: 28,
fast: 22,
standard: 17,
low: 11,
},
}; };
const oracle = new GasPriceOracle(options); const oracle = new GasPriceOracle(options);
// optional fallbackGasPrices // optional fallbackGasPrices

@ -1,6 +1,6 @@
{ {
"name": "gas-price-oracle", "name": "gas-price-oracle",
"version": "0.3.0", "version": "0.3.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",

@ -1,12 +1,6 @@
import { NetworkConfig } from '../types'; import { NetworkConfig } from '../types';
import { import mainnetOracles from './mainnet';
onChainOracles as mainnetOnchainOracles, import binanceOracles from './binance';
offChainOracles as mainnetOffChainOracles,
} from './mainnet';
import {
onChainOracles as binanceOnchainOracles,
offChainOracles as binanceOffchainOracles,
} from './binance';
export enum ChainId { export enum ChainId {
MAINNET = 1, MAINNET = 1,
@ -14,12 +8,6 @@ export enum ChainId {
} }
export const networks: NetworkConfig = { export const networks: NetworkConfig = {
[ChainId.MAINNET]: { [ChainId.MAINNET]: mainnetOracles,
onChainOracles: mainnetOnchainOracles, [ChainId.BINANCE]: binanceOracles,
offChainOracles: mainnetOffChainOracles,
},
[ChainId.BINANCE]: {
onChainOracles: binanceOnchainOracles,
offChainOracles: binanceOffchainOracles,
},
}; };

@ -12,6 +12,7 @@ import {
} from './types'; } from './types';
import BigNumber from 'bignumber.js'; import BigNumber from 'bignumber.js';
const defaultFastGas = 22;
export class GasPriceOracle { export class GasPriceOracle {
lastGasPrice: GasPrice; lastGasPrice: GasPrice;
offChainOracles: OffChainOracles; offChainOracles: OffChainOracles;
@ -20,6 +21,12 @@ export class GasPriceOracle {
chainId: ChainId.MAINNET, chainId: ChainId.MAINNET,
defaultRpc: 'https://api.mycryptoapi.com/eth', defaultRpc: 'https://api.mycryptoapi.com/eth',
timeout: 10000, timeout: 10000,
defaultFallbackGasPrices: {
instant: defaultFastGas * 1.3,
fast: defaultFastGas,
standard: defaultFastGas * 0.85,
low: defaultFastGas * 0.5,
},
}; };
constructor(options?: Options) { constructor(options?: Options) {
@ -175,14 +182,7 @@ export class GasPriceOracle {
} }
async gasPrices(fallbackGasPrices?: GasPrice, median = true): Promise<GasPrice> { async gasPrices(fallbackGasPrices?: GasPrice, median = true): Promise<GasPrice> {
const defaultFastGas = 22; this.lastGasPrice = this.lastGasPrice || fallbackGasPrices || this.configuration.defaultFallbackGasPrices;
const defaultFallbackGasPrices = {
instant: defaultFastGas * 1.3,
fast: defaultFastGas,
standard: defaultFastGas * 0.85,
low: defaultFastGas * 0.5,
};
this.lastGasPrice = this.lastGasPrice || fallbackGasPrices || defaultFallbackGasPrices;
try { try {
this.lastGasPrice = median this.lastGasPrice = median
? await this.fetchMedianGasPriceOffChain() ? await this.fetchMedianGasPriceOffChain()

@ -21,6 +21,11 @@ export type OnChainOracle = {
export type OnChainOracles = { [key: string]: OnChainOracle }; export type OnChainOracles = { [key: string]: OnChainOracle };
export type AllOracles = {
offChainOracles: OffChainOracles;
onChainOracles: OnChainOracles;
};
export type GasPrice = { export type GasPrice = {
[key in GasPriceKey]: number; [key in GasPriceKey]: number;
}; };
@ -31,13 +36,11 @@ export type Options = {
chainId?: number; chainId?: number;
defaultRpc?: string; defaultRpc?: string;
timeout?: number; timeout?: number;
defaultFallbackGasPrices?: GasPrice;
}; };
export type Config = Required<Options>; export type Config = Required<Options>;
export type NetworkConfig = { export type NetworkConfig = {
[key in number]: { [key in number]: AllOracles;
offChainOracles: OffChainOracles;
onChainOracles: OnChainOracles;
};
}; };