Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b13a4252ab | ||
|
|
5036c65643 | ||
|
|
89a69519b2 | ||
|
|
3fce50efbb |
@@ -9,7 +9,6 @@ A library that has a collection of onchain and offchain gas price oracle URLs
|
|||||||
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
|
||||||
@@ -24,6 +23,12 @@ Current offchain list:
|
|||||||
|
|
||||||
- https://bscgas.info/
|
- https://bscgas.info/
|
||||||
|
|
||||||
|
### xDAI Chain
|
||||||
|
|
||||||
|
Current offchain list:
|
||||||
|
|
||||||
|
- https://www.xdaichain.com/for-developers/developer-resources/gas-price-oracle
|
||||||
|
|
||||||
### Polygon (Matic) Network
|
### Polygon (Matic) Network
|
||||||
|
|
||||||
Current offchain list:
|
Current offchain list:
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "gas-price-oracle",
|
"name": "gas-price-oracle",
|
||||||
"version": "0.3.3",
|
"version": "0.4.0",
|
||||||
"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,16 +1,20 @@
|
|||||||
import { NetworkConfig } from '../types';
|
import { NetworkConfig } from '../types';
|
||||||
|
|
||||||
import mainnetOracles from './mainnet';
|
import mainnetOracles from './mainnet';
|
||||||
import binanceOracles from './binance';
|
import binanceOracles from './binance';
|
||||||
|
import xdaiOracles from './xdai';
|
||||||
import polygonOracles from './polygon';
|
import polygonOracles from './polygon';
|
||||||
|
|
||||||
export enum ChainId {
|
export enum ChainId {
|
||||||
MAINNET = 1,
|
MAINNET = 1,
|
||||||
BINANCE = 56,
|
BINANCE = 56,
|
||||||
|
XDAI = 100,
|
||||||
POLYGON = 137,
|
POLYGON = 137,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const networks: NetworkConfig = {
|
export const networks: NetworkConfig = {
|
||||||
[ChainId.MAINNET]: mainnetOracles,
|
[ChainId.MAINNET]: mainnetOracles,
|
||||||
[ChainId.BINANCE]: binanceOracles,
|
[ChainId.BINANCE]: binanceOracles,
|
||||||
|
[ChainId.XDAI]: xdaiOracles,
|
||||||
[ChainId.POLYGON]: polygonOracles,
|
[ChainId.POLYGON]: polygonOracles,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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/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,
|
||||||
|
};
|
||||||
53
src/index.ts
53
src/index.ts
@@ -34,9 +34,13 @@ 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];
|
||||||
this.offChainOracles = { ...offChainOracles };
|
|
||||||
this.onChainOracles = { ...onChainOracles };
|
if (network) {
|
||||||
|
const { offChainOracles, onChainOracles } = network;
|
||||||
|
this.offChainOracles = { ...offChainOracles };
|
||||||
|
this.onChainOracles = { ...onChainOracles };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async askOracle(oracle: OffChainOracle): Promise<GasPrice> {
|
async askOracle(oracle: OffChainOracle): Promise<GasPrice> {
|
||||||
@@ -181,6 +185,34 @@ export class GasPriceOracle {
|
|||||||
throw new Error('All oracles are down. Probably a network error.');
|
throw new Error('All oracles are down. Probably a network error.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fetchGasPriceFromRpc(): Promise<number> {
|
||||||
|
const rpcUrl = this.configuration.defaultRpc;
|
||||||
|
const body = {
|
||||||
|
jsonrpc: '2.0',
|
||||||
|
id: 1337,
|
||||||
|
method: 'eth_gasPrice',
|
||||||
|
params: [],
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||||
|
const response = await axios.post(rpcUrl!, body, { timeout: this.configuration.timeout });
|
||||||
|
if (response.status === 200) {
|
||||||
|
const { result } = response.data;
|
||||||
|
let fastGasPrice = new BigNumber(result);
|
||||||
|
if (fastGasPrice.isZero()) {
|
||||||
|
throw new Error(`Default RPC provides corrupted values`);
|
||||||
|
}
|
||||||
|
fastGasPrice = fastGasPrice.div(1e9);
|
||||||
|
return fastGasPrice.toNumber();
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Fetch gasPrice from default RPC failed..`);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e.message);
|
||||||
|
throw new Error('Default RPC is down. Probably a network error.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async gasPrices(fallbackGasPrices?: GasPrice, median = true): Promise<GasPrice> {
|
async gasPrices(fallbackGasPrices?: GasPrice, median = true): Promise<GasPrice> {
|
||||||
this.lastGasPrice = this.lastGasPrice || fallbackGasPrices || this.configuration.defaultFallbackGasPrices;
|
this.lastGasPrice = this.lastGasPrice || fallbackGasPrices || this.configuration.defaultFallbackGasPrices;
|
||||||
try {
|
try {
|
||||||
@@ -202,7 +234,20 @@ export class GasPriceOracle {
|
|||||||
};
|
};
|
||||||
return this.lastGasPrice;
|
return this.lastGasPrice;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('Failed to fetch gas prices from onchain oracles. Last known gas will be returned');
|
console.log('Failed to fetch gas prices from onchain oracles. Trying from default RPC...');
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const fastGas = await this.fetchGasPriceFromRpc();
|
||||||
|
this.lastGasPrice = {
|
||||||
|
instant: fastGas * 1.3,
|
||||||
|
fast: fastGas,
|
||||||
|
standard: fastGas * 0.85,
|
||||||
|
low: fastGas * 0.5,
|
||||||
|
};
|
||||||
|
return this.lastGasPrice;
|
||||||
|
} catch (e) {
|
||||||
|
console.log('Failed to fetch gas prices from default RPC. Last known gas will be returned');
|
||||||
}
|
}
|
||||||
return this.lastGasPrice;
|
return this.lastGasPrice;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -119,6 +119,37 @@ describe('fetchGasPricesOnChain', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('fetchGasPriceFromRpc', function () {
|
||||||
|
it('should work', async function () {
|
||||||
|
const gas: number = await oracle.fetchGasPriceFromRpc();
|
||||||
|
gas.should.be.a('number');
|
||||||
|
gas.should.be.above(1);
|
||||||
|
gas.should.not.be.equal(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should work with custom rpc', async function () {
|
||||||
|
const rpc = 'https://ethereum-rpc.trustwalletapp.com';
|
||||||
|
const oracle = new GasPriceOracle({ defaultRpc: rpc });
|
||||||
|
oracle.configuration.defaultRpc.should.be.equal(rpc);
|
||||||
|
const gas: number = await oracle.fetchGasPriceFromRpc();
|
||||||
|
|
||||||
|
gas.should.be.a('number');
|
||||||
|
|
||||||
|
gas.should.be.above(1);
|
||||||
|
gas.should.not.be.equal(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw if default rpc is down', async function () {
|
||||||
|
mockery.enable({ useCleanCache: true, warnOnUnregistered: false });
|
||||||
|
const { GasPriceOracle } = require('../src/index');
|
||||||
|
oracle = new GasPriceOracle();
|
||||||
|
await oracle
|
||||||
|
.fetchGasPriceFromRpc()
|
||||||
|
.should.be.rejectedWith('Default RPC is down. Probably a network error.');
|
||||||
|
mockery.disable();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('gasPrice', function () {
|
describe('gasPrice', function () {
|
||||||
it('should work', async function () {
|
it('should work', async function () {
|
||||||
const gas: GasPrice = await oracle.gasPrices();
|
const gas: GasPrice = await oracle.gasPrices();
|
||||||
|
|||||||
Reference in New Issue
Block a user