4 Commits

Author SHA1 Message Date
Danil Kovtonyuk
85ca7000bc remove Avalanche 2022-05-23 20:26:11 +10:00
Danil Kovtonyuk
d65b3dfe6c remove Arbitrum 2022-04-13 21:30:55 +10:00
Danil Kovtonyuk
be905f2b71 fix: mainnet oracles 2022-03-18 05:19:29 +10:00
Danil Kovtonyuk
4373641a30 fix: normalize result values 2021-11-16 03:46:43 +10:00
8 changed files with 93 additions and 103 deletions

View File

@@ -34,18 +34,6 @@ Current offchain list:
- https://gasstation-mainnet.matic.network/
### Arbitrum One
Current offchain list:
- https://ztake.org/
### Avalanche Mainnet
Current offchain list:
- https://ztake.org/
## Installation
`npm i gas-price-oracle`

View File

@@ -1,6 +1,6 @@
{
"name": "gas-price-oracle",
"version": "0.4.3",
"version": "0.4.7",
"description": "Gas Price Oracle library for Ethereum dApps.",
"main": "lib/index.js",
"homepage": "https://github.com/peppersec/gas-price-oracle",

View File

@@ -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,
};

View File

@@ -1,23 +0,0 @@
import { OffChainOracle, OffChainOracles, OnChainOracles } from '../types';
const ztake: OffChainOracle = {
name: 'ztake',
url: 'https://blockchains.ztake.org/api/h6WnmwNqw9CAJHzej5W4gD6LZ9n7v8EK/gasprice/avax/',
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,
};

View File

@@ -4,16 +4,12 @@ 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 {
MAINNET = 1,
BSC = 56,
XDAI = 100,
POLYGON = 137,
ARBITRUM = 42161,
AVALANCHE = 43114,
}
export const NETWORKS: NetworkConfig = {
@@ -21,6 +17,4 @@ export const NETWORKS: NetworkConfig = {
[ChainId.BSC]: bscOracles,
[ChainId.XDAI]: xdaiOracles,
[ChainId.POLYGON]: polygonOracles,
[ChainId.ARBITRUM]: arbitrumOracles,
[ChainId.AVALANCHE]: avalancheOracles,
};

View File

@@ -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,
};

View File

@@ -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 {

View File

@@ -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));