2 Commits

Author SHA1 Message Date
Alexey
ca24732a9b fix bug with extra precision 2020-10-20 18:11:07 +03:00
Alexey
a05a1e62d4 anyblock new oracle 2020-10-20 17:39:50 +03:00
4 changed files with 58 additions and 5 deletions

View File

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

View File

@@ -55,6 +55,17 @@ const gasNow: OffChainOracle = {
additionalDataProperty: 'data',
};
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',
callData: '0x50d25bcd',
@@ -64,6 +75,7 @@ const chainlink: OnChainOracle = {
export const offChainOracles: { [key: string]: OffChainOracle } = {
ethgasstation,
anyblock,
gasNow,
poa,
etherchain,

View File

@@ -41,7 +41,7 @@ export class GasPriceOracle {
standard: parseFloat(gas[standardPropertyName]) / denominator,
low: parseFloat(gas[lowPropertyName]) / denominator,
};
return gasPrices;
return this.normalize(gasPrices);
} else {
throw new Error(`Fetch gasPrice from ${name} oracle failed. Trying another one...`);
}
@@ -108,7 +108,25 @@ export class GasPriceOracle {
const middle = Math.floor(allPrices.length / 2);
medianGasPrice[type] = isEven ? (allPrices[middle - 1] + allPrices[middle]) / 2.0 : allPrices[middle];
}
return medianGasPrice;
return this.normalize(medianGasPrice);
}
/**
* Normalizes GasPrice values to Gwei. No more than 9 decimals basically
* @param GasPrice _gas
*/
normalize(_gas: GasPrice): GasPrice {
const format = {
decimalSeparator: '.',
groupSeparator: '',
};
const decimals = 9;
const gas: GasPrice = { ..._gas };
for (const type of Object.keys(gas) as Array<keyof GasPrice>) {
gas[type] = Number(new BigNumber(gas[type]).toFormat(decimals, format));
}
return gas;
}
async fetchGasPricesOnChain(): Promise<number> {

View File

@@ -1,9 +1,9 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/no-var-requires */
import { GasPrice } from '../src/types';
import { GasPrice, OffChainOracle } from '../src/types';
import mockery from 'mockery';
import chai from 'chai';
import { onChainOracles } from '../src/config';
import { onChainOracles, offChainOracles } from '../src/config';
import { GasPriceOracle } from '../src/index';
chai.use(require('chai-as-promised'));
@@ -204,6 +204,29 @@ describe('fetchMedianGasPriceOffChain', function () {
});
});
describe('askOracle', function () {
it('all oracles should answer', async function () {
for (const o of Object.values(offChainOracles) as Array<OffChainOracle>) {
try {
const gas: GasPrice = await oracle.askOracle(o);
gas.instant.should.be.a('number');
gas.fast.should.be.a('number');
gas.standard.should.be.a('number');
gas.low.should.be.a('number');
gas.instant.should.be.at.least(gas.fast); // greater than or equal to the given number.
gas.fast.should.be.at.least(gas.standard);
gas.standard.should.be.at.least(gas.low);
gas.low.should.not.be.equal(0);
} catch (e) {
console.error(`Failed to get data from ${o.name} oracle`);
throw new Error(e);
}
}
});
});
after('after', function () {
after(function () {
mockery.disable();