Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a508e41652 | ||
|
|
bb58318b6f | ||
|
|
32c256bc4e | ||
|
|
efd3cb3c7c | ||
|
|
88a757bb45 | ||
|
|
c30c7aed63 | ||
|
|
ca24732a9b | ||
|
|
a05a1e62d4 |
17
README.md
17
README.md
@@ -2,6 +2,10 @@
|
||||
|
||||
A library that has a collection of onchain and offchain gas price oracle URLs
|
||||
|
||||
## Supported networks
|
||||
|
||||
### Ethereum Mainnet
|
||||
|
||||
Current offchain list:
|
||||
|
||||
- https://ethgasstation.info/json/ethgasAPI.json
|
||||
@@ -14,6 +18,12 @@ Current onchain list:
|
||||
|
||||
- [chainlink](https://etherscan.io/address/0x169e633a2d1e6c10dd91238ba11c4a708dfef37c#readContract)
|
||||
|
||||
### Binance Smart Chain
|
||||
|
||||
Current offchain list:
|
||||
|
||||
- https://bscgas.info/
|
||||
|
||||
## Installation
|
||||
|
||||
`npm i gas-price-oracle`
|
||||
@@ -30,8 +40,15 @@ const { GasPriceOracle } = require('gas-price-oracle');
|
||||
|
||||
```js
|
||||
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
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "gas-price-oracle",
|
||||
"version": "0.2.0",
|
||||
"version": "0.3.2",
|
||||
"description": "Gas Price Oracle library for Ethereum dApps.",
|
||||
"main": "lib/index.js",
|
||||
"homepage": "https://github.com/peppersec/gas-price-oracle",
|
||||
|
||||
23
src/config/binance.ts
Normal file
23
src/config/binance.ts
Normal 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
13
src/config/index.ts
Normal 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,
|
||||
};
|
||||
@@ -1,4 +1,4 @@
|
||||
import { OffChainOracle, OnChainOracle } from './types';
|
||||
import { OffChainOracle, OnChainOracle, OffChainOracles, OnChainOracles } from '../types';
|
||||
|
||||
const ethgasstation: OffChainOracle = {
|
||||
name: 'ethgasstation',
|
||||
@@ -55,6 +55,17 @@ const gasNow: OffChainOracle = {
|
||||
additionalDataProperty: 'data',
|
||||
};
|
||||
|
||||
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',
|
||||
callData: '0x50d25bcd',
|
||||
@@ -62,15 +73,16 @@ const chainlink: OnChainOracle = {
|
||||
denominator: '1000000000',
|
||||
};
|
||||
|
||||
export const offChainOracles: { [key: string]: OffChainOracle } = {
|
||||
export const offChainOracles: OffChainOracles = {
|
||||
ethgasstation,
|
||||
anyblock,
|
||||
gasNow,
|
||||
poa,
|
||||
etherchain,
|
||||
zoltu,
|
||||
};
|
||||
|
||||
export const onChainOracles: { [key: string]: OnChainOracle } = {
|
||||
export const onChainOracles: OnChainOracles = {
|
||||
chainlink,
|
||||
};
|
||||
|
||||
60
src/index.ts
60
src/index.ts
@@ -1,21 +1,42 @@
|
||||
import axios from 'axios';
|
||||
import config from './config';
|
||||
import { GasPrice, OffChainOracle, OnChainOracle, Config, GasPriceKey, Options } from './types';
|
||||
import { ChainId, networks } from './config';
|
||||
import {
|
||||
Config,
|
||||
Options,
|
||||
GasPrice,
|
||||
GasPriceKey,
|
||||
OffChainOracle,
|
||||
OnChainOracle,
|
||||
OnChainOracles,
|
||||
OffChainOracles,
|
||||
} from './types';
|
||||
import BigNumber from 'bignumber.js';
|
||||
|
||||
const defaultFastGas = 22;
|
||||
export class GasPriceOracle {
|
||||
lastGasPrice: GasPrice;
|
||||
offChainOracles = { ...config.offChainOracles };
|
||||
onChainOracles = { ...config.onChainOracles };
|
||||
offChainOracles: OffChainOracles;
|
||||
onChainOracles: OnChainOracles;
|
||||
configuration: Config = {
|
||||
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) {
|
||||
if (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> {
|
||||
@@ -41,7 +62,7 @@ export class GasPriceOracle {
|
||||
standard: parseFloat(gas[standardPropertyName]) / denominator,
|
||||
low: parseFloat(gas[lowPropertyName]) / denominator,
|
||||
};
|
||||
return gasPrices;
|
||||
return this.normalize(gasPrices);
|
||||
} else {
|
||||
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);
|
||||
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> {
|
||||
@@ -143,14 +182,7 @@ export class GasPriceOracle {
|
||||
}
|
||||
|
||||
async gasPrices(fallbackGasPrices?: GasPrice, median = true): Promise<GasPrice> {
|
||||
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()
|
||||
|
||||
15
src/types.ts
15
src/types.ts
@@ -9,6 +9,8 @@ export type OffChainOracle = {
|
||||
additionalDataProperty: string | null;
|
||||
};
|
||||
|
||||
export type OffChainOracles = { [key: string]: OffChainOracle };
|
||||
|
||||
export type OnChainOracle = {
|
||||
name: string;
|
||||
rpc?: string;
|
||||
@@ -17,6 +19,13 @@ export type OnChainOracle = {
|
||||
denominator: string;
|
||||
};
|
||||
|
||||
export type OnChainOracles = { [key: string]: OnChainOracle };
|
||||
|
||||
export type AllOracles = {
|
||||
offChainOracles: OffChainOracles;
|
||||
onChainOracles: OnChainOracles;
|
||||
};
|
||||
|
||||
export type GasPrice = {
|
||||
[key in GasPriceKey]: number;
|
||||
};
|
||||
@@ -24,8 +33,14 @@ export type GasPrice = {
|
||||
export type GasPriceKey = 'instant' | 'fast' | 'standard' | 'low';
|
||||
|
||||
export type Options = {
|
||||
chainId?: number;
|
||||
defaultRpc?: string;
|
||||
timeout?: number;
|
||||
defaultFallbackGasPrices?: GasPrice;
|
||||
};
|
||||
|
||||
export type Config = Required<Options>;
|
||||
|
||||
export type NetworkConfig = {
|
||||
[key in number]: AllOracles;
|
||||
};
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
||||
/* eslint-disable @typescript-eslint/no-var-requires */
|
||||
import { GasPrice } from '../src/types';
|
||||
import { GasPrice, OffChainOracle } from '../src/types';
|
||||
import mockery from 'mockery';
|
||||
import chai from 'chai';
|
||||
import { onChainOracles } from '../src/config';
|
||||
|
||||
import { GasPriceOracle } from '../src/index';
|
||||
chai.use(require('chai-as-promised'));
|
||||
chai.should();
|
||||
let oracle = new GasPriceOracle();
|
||||
let { onChainOracles, offChainOracles } = oracle;
|
||||
|
||||
before('before', function () {
|
||||
const axiosMock = {
|
||||
@@ -24,6 +24,7 @@ before('before', function () {
|
||||
|
||||
beforeEach('beforeEach', function () {
|
||||
oracle = new GasPriceOracle();
|
||||
({ onChainOracles, offChainOracles } = oracle);
|
||||
});
|
||||
|
||||
describe('constructor', function () {
|
||||
@@ -204,6 +205,29 @@ describe('fetchMedianGasPriceOffChain', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('askOracle', function () {
|
||||
it('all oracles should answer', async function () {
|
||||
for (const o of Object.values(offChainOracles) as Array<OffChainOracle>) {
|
||||
try {
|
||||
const gas: GasPrice = await oracle.askOracle(o);
|
||||
|
||||
gas.instant.should.be.a('number');
|
||||
gas.fast.should.be.a('number');
|
||||
gas.standard.should.be.a('number');
|
||||
gas.low.should.be.a('number');
|
||||
|
||||
gas.instant.should.be.at.least(gas.fast); // greater than or equal to the given number.
|
||||
gas.fast.should.be.at.least(gas.standard);
|
||||
gas.standard.should.be.at.least(gas.low);
|
||||
gas.low.should.not.be.equal(0);
|
||||
} catch (e) {
|
||||
console.error(`Failed to get data from ${o.name} oracle`);
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
after('after', function () {
|
||||
after(function () {
|
||||
mockery.disable();
|
||||
|
||||
Reference in New Issue
Block a user