Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d65b3dfe6c | ||
|
|
be905f2b71 | ||
|
|
4373641a30 |
@@ -34,12 +34,6 @@ Current offchain list:
|
||||
|
||||
- https://gasstation-mainnet.matic.network/
|
||||
|
||||
### Arbitrum One
|
||||
|
||||
Current offchain list:
|
||||
|
||||
- https://ztake.org/
|
||||
|
||||
### Avalanche Mainnet
|
||||
|
||||
Current offchain list:
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "gas-price-oracle",
|
||||
"version": "0.4.3",
|
||||
"version": "0.4.6",
|
||||
"description": "Gas Price Oracle library for Ethereum dApps.",
|
||||
"main": "lib/index.js",
|
||||
"homepage": "https://github.com/peppersec/gas-price-oracle",
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
import { OffChainOracle, OffChainOracles, OnChainOracles } from '../types';
|
||||
|
||||
const ztake: OffChainOracle = {
|
||||
name: 'ztake',
|
||||
url: 'https://blockchains.ztake.org/api/h6WnmwNqw9CAJHzej5W4gD6LZ9n7v8EK/gasprice/arb/',
|
||||
instantPropertyName: 'percentile_90',
|
||||
fastPropertyName: 'percentile_80',
|
||||
standardPropertyName: 'percentile_60',
|
||||
lowPropertyName: 'percentile_30',
|
||||
denominator: 1,
|
||||
additionalDataProperty: null,
|
||||
};
|
||||
|
||||
export const offChainOracles: OffChainOracles = {
|
||||
ztake,
|
||||
};
|
||||
|
||||
export const onChainOracles: OnChainOracles = {};
|
||||
|
||||
export default {
|
||||
offChainOracles,
|
||||
onChainOracles,
|
||||
};
|
||||
@@ -4,7 +4,6 @@ import mainnetOracles from './mainnet';
|
||||
import bscOracles from './bsc';
|
||||
import xdaiOracles from './xdai';
|
||||
import polygonOracles from './polygon';
|
||||
import arbitrumOracles from './arbitrum';
|
||||
import avalancheOracles from './avalanche';
|
||||
|
||||
export enum ChainId {
|
||||
@@ -12,7 +11,6 @@ export enum ChainId {
|
||||
BSC = 56,
|
||||
XDAI = 100,
|
||||
POLYGON = 137,
|
||||
ARBITRUM = 42161,
|
||||
AVALANCHE = 43114,
|
||||
}
|
||||
|
||||
@@ -21,6 +19,5 @@ export const NETWORKS: NetworkConfig = {
|
||||
[ChainId.BSC]: bscOracles,
|
||||
[ChainId.XDAI]: xdaiOracles,
|
||||
[ChainId.POLYGON]: polygonOracles,
|
||||
[ChainId.ARBITRUM]: arbitrumOracles,
|
||||
[ChainId.AVALANCHE]: avalancheOracles,
|
||||
};
|
||||
|
||||
@@ -18,31 +18,31 @@ const etherchain: OffChainOracle = {
|
||||
fastPropertyName: 'fast',
|
||||
standardPropertyName: 'standard',
|
||||
lowPropertyName: 'slow',
|
||||
denominator: 1,
|
||||
denominator: 1e9,
|
||||
additionalDataProperty: 'data',
|
||||
};
|
||||
|
||||
const blockscout: OffChainOracle = {
|
||||
name: 'blockscout',
|
||||
url: 'https://blockscout.com/eth/mainnet/api/v1/gas-price-oracle',
|
||||
instantPropertyName: 'fast',
|
||||
fastPropertyName: 'average',
|
||||
standardPropertyName: 'slow',
|
||||
lowPropertyName: 'slow',
|
||||
denominator: 1,
|
||||
additionalDataProperty: null,
|
||||
};
|
||||
// const blockscout: OffChainOracle = {
|
||||
// name: 'blockscout',
|
||||
// url: 'https://blockscout.com/eth/mainnet/api/v1/gas-price-oracle',
|
||||
// instantPropertyName: 'fast',
|
||||
// fastPropertyName: 'average',
|
||||
// standardPropertyName: 'slow',
|
||||
// lowPropertyName: 'slow',
|
||||
// denominator: 1,
|
||||
// additionalDataProperty: null,
|
||||
// };
|
||||
|
||||
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 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',
|
||||
@@ -53,8 +53,8 @@ const chainlink: OnChainOracle = {
|
||||
|
||||
export const offChainOracles: OffChainOracles = {
|
||||
ethgasstation,
|
||||
anyblock,
|
||||
blockscout,
|
||||
// anyblock,
|
||||
// blockscout,
|
||||
etherchain,
|
||||
};
|
||||
|
||||
|
||||
31
src/index.ts
31
src/index.ts
@@ -36,6 +36,8 @@ export class GasPriceOracle {
|
||||
Object.assign(this.configuration, options);
|
||||
}
|
||||
|
||||
this.configuration.defaultFallbackGasPrices = this.normalize(this.configuration.defaultFallbackGasPrices);
|
||||
|
||||
const network = NETWORKS[this.configuration.chainId];
|
||||
|
||||
if (network) {
|
||||
@@ -225,38 +227,37 @@ export class GasPriceOracle {
|
||||
: await this.fetchGasPricesOffChain();
|
||||
return this.lastGasPrice;
|
||||
} catch (e) {
|
||||
console.log('Failed to fetch gas prices from offchain oracles. Trying onchain ones...');
|
||||
console.log('Failed to fetch gas prices from offchain oracles...');
|
||||
}
|
||||
}
|
||||
|
||||
if (Object.keys(this.onChainOracles).length > 0) {
|
||||
try {
|
||||
const fastGas = await this.fetchGasPricesOnChain();
|
||||
this.lastGasPrice = {
|
||||
instant: fastGas * 1.3,
|
||||
fast: fastGas,
|
||||
standard: fastGas * 0.85,
|
||||
low: fastGas * 0.5,
|
||||
};
|
||||
this.lastGasPrice = this.categorize(fastGas);
|
||||
return this.lastGasPrice;
|
||||
} catch (e) {
|
||||
console.log('Failed to fetch gas prices from onchain oracles. Trying from default RPC...');
|
||||
console.log('Failed to fetch gas prices from onchain oracles...');
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const fastGas = await this.fetchGasPriceFromRpc();
|
||||
this.lastGasPrice = {
|
||||
instant: fastGas * 1.3,
|
||||
fast: fastGas,
|
||||
standard: fastGas * 0.85,
|
||||
low: fastGas * 0.5,
|
||||
};
|
||||
this.lastGasPrice = this.categorize(fastGas);
|
||||
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.normalize(this.lastGasPrice);
|
||||
}
|
||||
|
||||
categorize(gasPrice: number): GasPrice {
|
||||
return this.normalize({
|
||||
instant: gasPrice * 1.3,
|
||||
fast: gasPrice,
|
||||
standard: gasPrice * 0.85,
|
||||
low: gasPrice * 0.5,
|
||||
});
|
||||
}
|
||||
|
||||
addOffChainOracle(oracle: OffChainOracle): void {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
/* eslint-disable @typescript-eslint/no-var-requires */
|
||||
import chai from 'chai';
|
||||
import mockery from 'mockery';
|
||||
import BigNumber from 'bignumber.js';
|
||||
|
||||
import { ChainId, NETWORKS } from '../src/config';
|
||||
import { GasPriceOracle } from '../src/index';
|
||||
@@ -240,6 +241,58 @@ describe('fetchMedianGasPriceOffChain', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('normalize result values', function () {
|
||||
const wrongDecimalsGas = {
|
||||
instant: 1.1,
|
||||
fast: 2.12345678901,
|
||||
standard: 3.12345678901,
|
||||
low: 3.1234567890123456789,
|
||||
};
|
||||
|
||||
const checkDecimals = (gas: GasPrice) => {
|
||||
const gasPrices: number[] = Object.values(gas);
|
||||
|
||||
for (const gas of gasPrices) {
|
||||
new BigNumber(gas).dp().should.be.at.most(9);
|
||||
}
|
||||
};
|
||||
|
||||
it('default fallback should be normalized', function () {
|
||||
mockery.enable({ useCleanCache: true, warnOnUnregistered: false });
|
||||
|
||||
const { GasPriceOracle } = require('../src/index');
|
||||
oracle = new GasPriceOracle({
|
||||
defaultFallbackGasPrices: wrongDecimalsGas,
|
||||
});
|
||||
const { configuration } = oracle;
|
||||
|
||||
checkDecimals(configuration.defaultFallbackGasPrices);
|
||||
|
||||
mockery.disable();
|
||||
});
|
||||
|
||||
it('fallback should be normalized', async function () {
|
||||
mockery.enable({ useCleanCache: true, warnOnUnregistered: false });
|
||||
|
||||
const { GasPriceOracle } = require('../src/index');
|
||||
oracle = new GasPriceOracle();
|
||||
|
||||
const gas = await oracle.gasPrices(wrongDecimalsGas);
|
||||
|
||||
checkDecimals(gas);
|
||||
mockery.disable();
|
||||
});
|
||||
|
||||
it('rpc fallback should be normalized', async function () {
|
||||
const { GasPriceOracle } = require('../src/index');
|
||||
oracle = new GasPriceOracle({ chainId: 42161, defaultRpc: 'https://arb1.arbitrum.io/rpc' });
|
||||
|
||||
const gas = await oracle.gasPrices();
|
||||
|
||||
checkDecimals(gas);
|
||||
});
|
||||
});
|
||||
|
||||
describe('askOracle', function () {
|
||||
const chains = Object.keys(NETWORKS).map(id => Number(id));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user