Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5036c65643 | ||
|
|
89a69519b2 | ||
|
|
3fce50efbb | ||
|
|
9b0c229ace | ||
|
|
a508e41652 | ||
|
|
bb58318b6f | ||
|
|
32c256bc4e |
30
README.md
30
README.md
@@ -2,10 +2,13 @@
|
|||||||
|
|
||||||
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
|
||||||
- https://gas-oracle.zoltu.io/
|
|
||||||
- https://www.etherchain.org/api/gasPriceOracle
|
- https://www.etherchain.org/api/gasPriceOracle
|
||||||
- https://gasprice.poa.network/
|
- https://gasprice.poa.network/
|
||||||
- https://www.gasnow.org/api/v3/gas/price
|
- https://www.gasnow.org/api/v3/gas/price
|
||||||
@@ -14,6 +17,24 @@ 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/
|
||||||
|
|
||||||
|
### xDAI Chain
|
||||||
|
|
||||||
|
Current offchain list:
|
||||||
|
|
||||||
|
- https://www.xdaichain.com/for-developers/developer-resources/gas-price-oracle
|
||||||
|
|
||||||
|
### Polygon (Matic) Network
|
||||||
|
|
||||||
|
Current offchain list:
|
||||||
|
|
||||||
|
- https://gasstation-mainnet.matic.network/
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
`npm i gas-price-oracle`
|
`npm i gas-price-oracle`
|
||||||
@@ -30,8 +51,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
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "gas-price-oracle",
|
"name": "gas-price-oracle",
|
||||||
"version": "0.3.0",
|
"version": "0.3.5",
|
||||||
"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",
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { OffChainOracle, OffChainOracles, OnChainOracles } from '../types';
|
|||||||
const bscgas: OffChainOracle = {
|
const bscgas: OffChainOracle = {
|
||||||
name: 'bscgas',
|
name: 'bscgas',
|
||||||
url: 'https://bscgas.info/gas',
|
url: 'https://bscgas.info/gas',
|
||||||
instantPropertyName: 'imediate',
|
instantPropertyName: 'instant',
|
||||||
fastPropertyName: 'fast',
|
fastPropertyName: 'fast',
|
||||||
standardPropertyName: 'standard',
|
standardPropertyName: 'standard',
|
||||||
lowPropertyName: 'slow',
|
lowPropertyName: 'slow',
|
||||||
|
|||||||
@@ -1,25 +1,20 @@
|
|||||||
import { NetworkConfig } from '../types';
|
import { NetworkConfig } from '../types';
|
||||||
import {
|
|
||||||
onChainOracles as mainnetOnchainOracles,
|
import mainnetOracles from './mainnet';
|
||||||
offChainOracles as mainnetOffChainOracles,
|
import binanceOracles from './binance';
|
||||||
} from './mainnet';
|
import xdaiOracles from './xdai';
|
||||||
import {
|
import polygonOracles from './polygon';
|
||||||
onChainOracles as binanceOnchainOracles,
|
|
||||||
offChainOracles as binanceOffchainOracles,
|
|
||||||
} from './binance';
|
|
||||||
|
|
||||||
export enum ChainId {
|
export enum ChainId {
|
||||||
MAINNET = 1,
|
MAINNET = 1,
|
||||||
BINANCE = 56,
|
BINANCE = 56,
|
||||||
|
XDAI = 100,
|
||||||
|
POLYGON = 137,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const networks: NetworkConfig = {
|
export const networks: NetworkConfig = {
|
||||||
[ChainId.MAINNET]: {
|
[ChainId.MAINNET]: mainnetOracles,
|
||||||
onChainOracles: mainnetOnchainOracles,
|
[ChainId.BINANCE]: binanceOracles,
|
||||||
offChainOracles: mainnetOffChainOracles,
|
[ChainId.XDAI]: xdaiOracles,
|
||||||
},
|
[ChainId.POLYGON]: polygonOracles,
|
||||||
[ChainId.BINANCE]: {
|
|
||||||
onChainOracles: binanceOnchainOracles,
|
|
||||||
offChainOracles: binanceOffchainOracles,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -11,17 +11,6 @@ const ethgasstation: OffChainOracle = {
|
|||||||
additionalDataProperty: null,
|
additionalDataProperty: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
const zoltu: OffChainOracle = {
|
|
||||||
name: 'zoltu',
|
|
||||||
url: 'https://gas-oracle.zoltu.io/',
|
|
||||||
instantPropertyName: 'percentile_99',
|
|
||||||
fastPropertyName: 'percentile_90',
|
|
||||||
standardPropertyName: 'percentile_60',
|
|
||||||
lowPropertyName: 'percentile_30',
|
|
||||||
denominator: 1,
|
|
||||||
additionalDataProperty: null,
|
|
||||||
};
|
|
||||||
|
|
||||||
const etherchain: OffChainOracle = {
|
const etherchain: OffChainOracle = {
|
||||||
name: 'etherchain',
|
name: 'etherchain',
|
||||||
url: 'https://www.etherchain.org/api/gasPriceOracle',
|
url: 'https://www.etherchain.org/api/gasPriceOracle',
|
||||||
@@ -79,7 +68,6 @@ export const offChainOracles: OffChainOracles = {
|
|||||||
gasNow,
|
gasNow,
|
||||||
poa,
|
poa,
|
||||||
etherchain,
|
etherchain,
|
||||||
zoltu,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const onChainOracles: OnChainOracles = {
|
export const onChainOracles: OnChainOracles = {
|
||||||
|
|||||||
23
src/config/polygon.ts
Normal file
23
src/config/polygon.ts
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import { OffChainOracle, OffChainOracles, OnChainOracles } from '../types';
|
||||||
|
|
||||||
|
const maticGasStation: OffChainOracle = {
|
||||||
|
name: 'maticGasStation',
|
||||||
|
url: 'https://gasstation-mainnet.matic.network',
|
||||||
|
instantPropertyName: 'fastest',
|
||||||
|
fastPropertyName: 'fast',
|
||||||
|
standardPropertyName: 'standard',
|
||||||
|
lowPropertyName: 'safeLow',
|
||||||
|
denominator: 1,
|
||||||
|
additionalDataProperty: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const offChainOracles: OffChainOracles = {
|
||||||
|
maticGasStation,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const onChainOracles: OnChainOracles = {};
|
||||||
|
|
||||||
|
export default {
|
||||||
|
offChainOracles,
|
||||||
|
onChainOracles,
|
||||||
|
};
|
||||||
23
src/config/xdai.ts
Normal file
23
src/config/xdai.ts
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import { OffChainOracle, OffChainOracles, OnChainOracles } from '../types';
|
||||||
|
|
||||||
|
const blockscout: OffChainOracle = {
|
||||||
|
name: 'blockscout',
|
||||||
|
url: 'https://blockscout.com/xdai/mainnet/api/v1/gas-price-oracle',
|
||||||
|
instantPropertyName: 'fast',
|
||||||
|
fastPropertyName: 'average',
|
||||||
|
standardPropertyName: 'slow',
|
||||||
|
lowPropertyName: 'slow',
|
||||||
|
denominator: 1,
|
||||||
|
additionalDataProperty: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const offChainOracles: OffChainOracles = {
|
||||||
|
blockscout,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const onChainOracles: OnChainOracles = {};
|
||||||
|
|
||||||
|
export default {
|
||||||
|
offChainOracles,
|
||||||
|
onChainOracles,
|
||||||
|
};
|
||||||
22
src/index.ts
22
src/index.ts
@@ -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) {
|
||||||
@@ -27,10 +34,14 @@ export class GasPriceOracle {
|
|||||||
Object.assign(this.configuration, options);
|
Object.assign(this.configuration, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
const { offChainOracles, onChainOracles } = networks[this.configuration.chainId];
|
const network = networks[this.configuration.chainId];
|
||||||
|
|
||||||
|
if (network) {
|
||||||
|
const { offChainOracles, onChainOracles } = network;
|
||||||
this.offChainOracles = { ...offChainOracles };
|
this.offChainOracles = { ...offChainOracles };
|
||||||
this.onChainOracles = { ...onChainOracles };
|
this.onChainOracles = { ...onChainOracles };
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async askOracle(oracle: OffChainOracle): Promise<GasPrice> {
|
async askOracle(oracle: OffChainOracle): Promise<GasPrice> {
|
||||||
const {
|
const {
|
||||||
@@ -175,14 +186,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()
|
||||||
|
|||||||
11
src/types.ts
11
src/types.ts
@@ -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;
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -207,9 +207,6 @@ describe('fetchMedianGasPriceOffChain', function () {
|
|||||||
|
|
||||||
describe('askOracle', function () {
|
describe('askOracle', function () {
|
||||||
it('all oracles should answer', async function () {
|
it('all oracles should answer', async function () {
|
||||||
// TODO: remove after fix POA
|
|
||||||
delete offChainOracles['poa'];
|
|
||||||
|
|
||||||
for (const o of Object.values(offChainOracles) as Array<OffChainOracle>) {
|
for (const o of Object.values(offChainOracles) as Array<OffChainOracle>) {
|
||||||
try {
|
try {
|
||||||
const gas: GasPrice = await oracle.askOracle(o);
|
const gas: GasPrice = await oracle.askOracle(o);
|
||||||
|
|||||||
Reference in New Issue
Block a user