fix: normalize result values
This commit is contained in:
parent
76f8cb036d
commit
4373641a30
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "gas-price-oracle",
|
"name": "gas-price-oracle",
|
||||||
"version": "0.4.3",
|
"version": "0.4.4",
|
||||||
"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",
|
||||||
|
31
src/index.ts
31
src/index.ts
@ -36,6 +36,8 @@ export class GasPriceOracle {
|
|||||||
Object.assign(this.configuration, options);
|
Object.assign(this.configuration, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.configuration.defaultFallbackGasPrices = this.normalize(this.configuration.defaultFallbackGasPrices);
|
||||||
|
|
||||||
const network = NETWORKS[this.configuration.chainId];
|
const network = NETWORKS[this.configuration.chainId];
|
||||||
|
|
||||||
if (network) {
|
if (network) {
|
||||||
@ -225,38 +227,37 @@ export class GasPriceOracle {
|
|||||||
: await this.fetchGasPricesOffChain();
|
: await this.fetchGasPricesOffChain();
|
||||||
return this.lastGasPrice;
|
return this.lastGasPrice;
|
||||||
} catch (e) {
|
} 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) {
|
if (Object.keys(this.onChainOracles).length > 0) {
|
||||||
try {
|
try {
|
||||||
const fastGas = await this.fetchGasPricesOnChain();
|
const fastGas = await this.fetchGasPricesOnChain();
|
||||||
this.lastGasPrice = {
|
this.lastGasPrice = this.categorize(fastGas);
|
||||||
instant: fastGas * 1.3,
|
|
||||||
fast: fastGas,
|
|
||||||
standard: fastGas * 0.85,
|
|
||||||
low: fastGas * 0.5,
|
|
||||||
};
|
|
||||||
return this.lastGasPrice;
|
return this.lastGasPrice;
|
||||||
} catch (e) {
|
} 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 {
|
try {
|
||||||
const fastGas = await this.fetchGasPriceFromRpc();
|
const fastGas = await this.fetchGasPriceFromRpc();
|
||||||
this.lastGasPrice = {
|
this.lastGasPrice = this.categorize(fastGas);
|
||||||
instant: fastGas * 1.3,
|
|
||||||
fast: fastGas,
|
|
||||||
standard: fastGas * 0.85,
|
|
||||||
low: fastGas * 0.5,
|
|
||||||
};
|
|
||||||
return this.lastGasPrice;
|
return this.lastGasPrice;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('Failed to fetch gas prices from default RPC. Last known gas will be returned');
|
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 {
|
addOffChainOracle(oracle: OffChainOracle): void {
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-var-requires */
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
||||||
import chai from 'chai';
|
import chai from 'chai';
|
||||||
import mockery from 'mockery';
|
import mockery from 'mockery';
|
||||||
|
import BigNumber from 'bignumber.js';
|
||||||
|
|
||||||
import { ChainId, NETWORKS } from '../src/config';
|
import { ChainId, NETWORKS } from '../src/config';
|
||||||
import { GasPriceOracle } from '../src/index';
|
import { GasPriceOracle } from '../src/index';
|
||||||
@ -240,6 +241,63 @@ 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: ChainId.ARBITRUM, defaultRpc: 'https://arb1.arbitrum.io/rpc' });
|
||||||
|
|
||||||
|
const { onChainOracles, offChainOracles } = oracle;
|
||||||
|
|
||||||
|
Object.keys(onChainOracles).forEach(chainOracle => oracle.removeOnChainOracle(chainOracle));
|
||||||
|
Object.keys(offChainOracles).forEach(chainOracle => oracle.removeOffChainOracle(chainOracle));
|
||||||
|
|
||||||
|
const gas = await oracle.gasPrices();
|
||||||
|
|
||||||
|
checkDecimals(gas);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('askOracle', function () {
|
describe('askOracle', function () {
|
||||||
const chains = Object.keys(NETWORKS).map(id => Number(id));
|
const chains = Object.keys(NETWORKS).map(id => Number(id));
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user