7 Commits

Author SHA1 Message Date
Danil Kovtonyuk
a508e41652 fix: bscgas property 2021-06-10 23:33:59 +03:00
Danil Kovtonyuk
bb58318b6f add defaultFallbackGasPrices option 2021-06-03 15:16:49 +03:00
Danil Kovtonyuk
32c256bc4e update readme 2021-06-03 13:01:25 +03:00
Danil Kovtonyuk
efd3cb3c7c fix test 2021-06-03 12:51:14 +03:00
Danil Kovtonyuk
88a757bb45 bump version 2021-06-03 12:46:36 +03:00
Danil Kovtonyuk
c30c7aed63 add binance 2021-06-03 12:46:36 +03:00
Alexey
ca24732a9b fix bug with extra precision 2020-10-20 18:11:07 +03:00
8 changed files with 120 additions and 19 deletions

View File

@@ -2,6 +2,10 @@
A library that has a collection of onchain and offchain gas price oracle URLs A library that has a collection of onchain and offchain gas price oracle URLs
## Supported networks
### Ethereum Mainnet
Current offchain list: Current offchain list:
- https://ethgasstation.info/json/ethgasAPI.json - https://ethgasstation.info/json/ethgasAPI.json
@@ -14,6 +18,12 @@ Current onchain list:
- [chainlink](https://etherscan.io/address/0x169e633a2d1e6c10dd91238ba11c4a708dfef37c#readContract) - [chainlink](https://etherscan.io/address/0x169e633a2d1e6c10dd91238ba11c4a708dfef37c#readContract)
### Binance Smart Chain
Current offchain list:
- https://bscgas.info/
## Installation ## Installation
`npm i gas-price-oracle` `npm i gas-price-oracle`
@@ -30,8 +40,15 @@ const { GasPriceOracle } = require('gas-price-oracle');
```js ```js
const options = { const options = {
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

View File

@@ -1,6 +1,6 @@
{ {
"name": "gas-price-oracle", "name": "gas-price-oracle",
"version": "0.2.1", "version": "0.3.2",
"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/binance.ts Normal file
View File

@@ -0,0 +1,23 @@
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,
};

13
src/config/index.ts Normal file
View File

@@ -0,0 +1,13 @@
import { NetworkConfig } from '../types';
import mainnetOracles from './mainnet';
import binanceOracles from './binance';
export enum ChainId {
MAINNET = 1,
BINANCE = 56,
}
export const networks: NetworkConfig = {
[ChainId.MAINNET]: mainnetOracles,
[ChainId.BINANCE]: binanceOracles,
};

View File

@@ -1,4 +1,4 @@
import { OffChainOracle, OnChainOracle } from './types'; import { OffChainOracle, OnChainOracle, OffChainOracles, OnChainOracles } from '../types';
const ethgasstation: OffChainOracle = { const ethgasstation: OffChainOracle = {
name: 'ethgasstation', name: 'ethgasstation',
@@ -73,7 +73,7 @@ const chainlink: OnChainOracle = {
denominator: '1000000000', denominator: '1000000000',
}; };
export const offChainOracles: { [key: string]: OffChainOracle } = { export const offChainOracles: OffChainOracles = {
ethgasstation, ethgasstation,
anyblock, anyblock,
gasNow, gasNow,
@@ -82,7 +82,7 @@ export const offChainOracles: { [key: string]: OffChainOracle } = {
zoltu, zoltu,
}; };
export const onChainOracles: { [key: string]: OnChainOracle } = { export const onChainOracles: OnChainOracles = {
chainlink, chainlink,
}; };

View File

@@ -1,21 +1,42 @@
import axios from 'axios'; import axios from 'axios';
import config from './config'; import { ChainId, networks } from './config';
import { GasPrice, OffChainOracle, OnChainOracle, Config, GasPriceKey, Options } from './types'; import {
Config,
Options,
GasPrice,
GasPriceKey,
OffChainOracle,
OnChainOracle,
OnChainOracles,
OffChainOracles,
} 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 = { ...config.offChainOracles }; offChainOracles: OffChainOracles;
onChainOracles = { ...config.onChainOracles }; onChainOracles: OnChainOracles;
configuration: Config = { configuration: Config = {
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) {
if (options) { if (options) {
Object.assign(this.configuration, options); Object.assign(this.configuration, options);
} }
const { offChainOracles, onChainOracles } = networks[this.configuration.chainId];
this.offChainOracles = { ...offChainOracles };
this.onChainOracles = { ...onChainOracles };
} }
async askOracle(oracle: OffChainOracle): Promise<GasPrice> { async askOracle(oracle: OffChainOracle): Promise<GasPrice> {
@@ -41,7 +62,7 @@ export class GasPriceOracle {
standard: parseFloat(gas[standardPropertyName]) / denominator, standard: parseFloat(gas[standardPropertyName]) / denominator,
low: parseFloat(gas[lowPropertyName]) / denominator, low: parseFloat(gas[lowPropertyName]) / denominator,
}; };
return gasPrices; return this.normalize(gasPrices);
} else { } else {
throw new Error(`Fetch gasPrice from ${name} oracle failed. Trying another one...`); throw new Error(`Fetch gasPrice from ${name} oracle failed. Trying another one...`);
} }
@@ -108,7 +129,25 @@ export class GasPriceOracle {
const middle = Math.floor(allPrices.length / 2); const middle = Math.floor(allPrices.length / 2);
medianGasPrice[type] = isEven ? (allPrices[middle - 1] + allPrices[middle]) / 2.0 : allPrices[middle]; medianGasPrice[type] = isEven ? (allPrices[middle - 1] + allPrices[middle]) / 2.0 : allPrices[middle];
} }
return medianGasPrice; return this.normalize(medianGasPrice);
}
/**
* Normalizes GasPrice values to Gwei. No more than 9 decimals basically
* @param GasPrice _gas
*/
normalize(_gas: GasPrice): GasPrice {
const format = {
decimalSeparator: '.',
groupSeparator: '',
};
const decimals = 9;
const gas: GasPrice = { ..._gas };
for (const type of Object.keys(gas) as Array<keyof GasPrice>) {
gas[type] = Number(new BigNumber(gas[type]).toFormat(decimals, format));
}
return gas;
} }
async fetchGasPricesOnChain(): Promise<number> { async fetchGasPricesOnChain(): Promise<number> {
@@ -143,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()

View File

@@ -9,6 +9,8 @@ export type OffChainOracle = {
additionalDataProperty: string | null; additionalDataProperty: string | null;
}; };
export type OffChainOracles = { [key: string]: OffChainOracle };
export type OnChainOracle = { export type OnChainOracle = {
name: string; name: string;
rpc?: string; rpc?: string;
@@ -17,6 +19,13 @@ export type OnChainOracle = {
denominator: string; denominator: string;
}; };
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;
}; };
@@ -24,8 +33,14 @@ export type GasPrice = {
export type GasPriceKey = 'instant' | 'fast' | 'standard' | 'low'; export type GasPriceKey = 'instant' | 'fast' | 'standard' | 'low';
export type Options = { export type Options = {
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 = {
[key in number]: AllOracles;
};

View File

@@ -3,12 +3,12 @@
import { GasPrice, OffChainOracle } from '../src/types'; import { GasPrice, OffChainOracle } from '../src/types';
import mockery from 'mockery'; import mockery from 'mockery';
import chai from 'chai'; import chai from 'chai';
import { onChainOracles, offChainOracles } from '../src/config';
import { GasPriceOracle } from '../src/index'; import { GasPriceOracle } from '../src/index';
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;
before('before', function () { before('before', function () {
const axiosMock = { const axiosMock = {
@@ -24,6 +24,7 @@ before('before', function () {
beforeEach('beforeEach', function () { beforeEach('beforeEach', function () {
oracle = new GasPriceOracle(); oracle = new GasPriceOracle();
({ onChainOracles, offChainOracles } = oracle);
}); });
describe('constructor', function () { describe('constructor', function () {