2020-10-19 15:02:47 +03:00
|
|
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
|
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
2020-10-20 17:39:50 +03:00
|
|
|
import { GasPrice, OffChainOracle } from '../src/types';
|
2020-06-01 17:29:58 +03:00
|
|
|
import mockery from 'mockery';
|
|
|
|
import chai from 'chai';
|
2020-05-27 18:52:57 +03:00
|
|
|
|
2020-10-19 15:02:47 +03:00
|
|
|
import { GasPriceOracle } from '../src/index';
|
2020-06-01 17:29:58 +03:00
|
|
|
chai.use(require('chai-as-promised'));
|
|
|
|
chai.should();
|
2020-06-02 15:46:03 +03:00
|
|
|
let oracle = new GasPriceOracle();
|
2021-06-02 19:48:50 +03:00
|
|
|
let { onChainOracles, offChainOracles } = oracle;
|
2020-06-02 15:46:03 +03:00
|
|
|
|
|
|
|
before('before', function () {
|
2020-10-19 15:02:47 +03:00
|
|
|
const axiosMock = {
|
2020-06-04 18:45:33 +03:00
|
|
|
get: () => {
|
2020-10-16 14:10:25 +03:00
|
|
|
throw new Error('axios GET method is mocked for tests');
|
2020-06-04 18:45:33 +03:00
|
|
|
},
|
|
|
|
post: () => {
|
2020-10-16 14:10:25 +03:00
|
|
|
throw new Error('axios POST method is mocked for tests');
|
|
|
|
},
|
2020-06-02 15:46:03 +03:00
|
|
|
};
|
2020-06-04 18:45:33 +03:00
|
|
|
mockery.registerMock('axios', axiosMock);
|
2020-06-02 15:46:03 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach('beforeEach', function () {
|
|
|
|
oracle = new GasPriceOracle();
|
2021-06-02 19:48:50 +03:00
|
|
|
({ onChainOracles, offChainOracles } = oracle);
|
2020-06-02 15:46:03 +03:00
|
|
|
});
|
2020-06-01 17:29:58 +03:00
|
|
|
|
2020-10-17 02:07:39 +03:00
|
|
|
describe('constructor', function () {
|
|
|
|
it('should set default values', async function () {
|
|
|
|
oracle.configuration.defaultRpc.should.be.equal('https://api.mycryptoapi.com/eth');
|
|
|
|
oracle.configuration.timeout.should.be.equal(10000);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set passed values', async function () {
|
|
|
|
const newOracle = new GasPriceOracle({ timeout: 1337 });
|
|
|
|
newOracle.configuration.defaultRpc.should.be.equal('https://api.mycryptoapi.com/eth');
|
|
|
|
newOracle.configuration.timeout.should.be.equal(1337);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-01 17:29:58 +03:00
|
|
|
describe('fetchGasPricesOffChain', function () {
|
2020-05-27 18:52:57 +03:00
|
|
|
it('should work', async function () {
|
2020-06-01 17:29:58 +03:00
|
|
|
const gas: GasPrice = await oracle.fetchGasPricesOffChain();
|
|
|
|
|
|
|
|
gas.instant.should.be.a('number');
|
|
|
|
gas.fast.should.be.a('number');
|
|
|
|
gas.standard.should.be.a('number');
|
|
|
|
gas.low.should.be.a('number');
|
|
|
|
|
2020-08-12 12:53:43 +03:00
|
|
|
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);
|
2020-06-01 17:29:58 +03:00
|
|
|
gas.low.should.not.be.equal(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw if all offchain oracles are down', async function () {
|
2020-06-02 15:46:03 +03:00
|
|
|
mockery.enable({ useCleanCache: true, warnOnUnregistered: false });
|
2020-06-01 17:29:58 +03:00
|
|
|
const { GasPriceOracle } = require('../src/index');
|
2020-06-02 15:46:03 +03:00
|
|
|
oracle = new GasPriceOracle();
|
2020-10-16 14:10:25 +03:00
|
|
|
await oracle
|
|
|
|
.fetchGasPricesOffChain()
|
|
|
|
.should.be.rejectedWith('All oracles are down. Probably a network error.');
|
2020-06-02 15:46:03 +03:00
|
|
|
mockery.disable();
|
2020-06-01 17:29:58 +03:00
|
|
|
});
|
2020-06-02 15:46:03 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('fetchGasPricesOnChain', function () {
|
|
|
|
it('should work', async function () {
|
2020-06-04 02:08:09 +03:00
|
|
|
const gas: number = await oracle.fetchGasPricesOnChain();
|
|
|
|
gas.should.be.a('number');
|
2020-06-04 18:49:16 +03:00
|
|
|
gas.should.be.above(1);
|
2020-06-04 02:08:09 +03:00
|
|
|
gas.should.not.be.equal(0);
|
2020-06-02 15:46:03 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with custom rpc', async function () {
|
|
|
|
const rpc = 'https://ethereum-rpc.trustwalletapp.com';
|
2020-06-04 02:08:09 +03:00
|
|
|
const oracle = new GasPriceOracle({ defaultRpc: rpc });
|
2020-10-17 02:07:39 +03:00
|
|
|
oracle.configuration.defaultRpc.should.be.equal(rpc);
|
2020-06-04 02:08:09 +03:00
|
|
|
const gas: number = await oracle.fetchGasPricesOnChain();
|
2020-06-02 15:46:03 +03:00
|
|
|
|
2020-06-04 02:08:09 +03:00
|
|
|
gas.should.be.a('number');
|
2020-06-02 15:46:03 +03:00
|
|
|
|
2020-06-04 02:08:09 +03:00
|
|
|
gas.should.be.above(1);
|
|
|
|
gas.should.not.be.equal(0);
|
2020-06-02 15:46:03 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove oracle', async function () {
|
|
|
|
await oracle.fetchGasPricesOnChain();
|
|
|
|
oracle.removeOnChainOracle('chainlink');
|
2020-10-16 14:10:25 +03:00
|
|
|
await oracle
|
|
|
|
.fetchGasPricesOnChain()
|
|
|
|
.should.be.rejectedWith('All oracles are down. Probably a network error.');
|
2020-06-01 17:29:58 +03:00
|
|
|
});
|
|
|
|
|
2020-06-02 15:46:03 +03:00
|
|
|
it('should add oracle', async function () {
|
|
|
|
const { chainlink } = onChainOracles;
|
|
|
|
await oracle.fetchGasPricesOnChain();
|
|
|
|
oracle.removeOnChainOracle('chainlink');
|
2020-10-16 14:10:25 +03:00
|
|
|
await oracle
|
|
|
|
.fetchGasPricesOnChain()
|
|
|
|
.should.be.rejectedWith('All oracles are down. Probably a network error.');
|
2020-06-02 15:46:03 +03:00
|
|
|
oracle.addOnChainOracle(chainlink);
|
2020-06-04 02:08:09 +03:00
|
|
|
const gas: number = await oracle.fetchGasPricesOnChain();
|
2020-06-02 15:46:03 +03:00
|
|
|
|
2020-06-04 02:08:09 +03:00
|
|
|
gas.should.be.a('number');
|
|
|
|
gas.should.not.be.equal(0);
|
2020-06-01 17:29:58 +03:00
|
|
|
});
|
|
|
|
|
2020-06-02 15:46:03 +03:00
|
|
|
it('should throw if all onchain oracles are down', async function () {
|
|
|
|
mockery.enable({ useCleanCache: true, warnOnUnregistered: false });
|
2020-06-01 17:29:58 +03:00
|
|
|
const { GasPriceOracle } = require('../src/index');
|
2020-06-02 15:46:03 +03:00
|
|
|
oracle = new GasPriceOracle();
|
2020-10-16 14:10:25 +03:00
|
|
|
await oracle
|
|
|
|
.fetchGasPricesOnChain()
|
|
|
|
.should.be.rejectedWith('All oracles are down. Probably a network error.');
|
2020-06-02 15:46:03 +03:00
|
|
|
mockery.disable();
|
2020-06-01 17:29:58 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-02 15:46:03 +03:00
|
|
|
describe('gasPrice', function () {
|
2020-06-01 17:29:58 +03:00
|
|
|
it('should work', async function () {
|
2020-06-02 15:46:03 +03:00
|
|
|
const gas: GasPrice = await oracle.gasPrices();
|
2020-06-01 17:29:58 +03:00
|
|
|
|
|
|
|
gas.instant.should.be.a('number');
|
|
|
|
gas.fast.should.be.a('number');
|
|
|
|
gas.standard.should.be.a('number');
|
|
|
|
gas.low.should.be.a('number');
|
|
|
|
|
2020-08-12 12:53:43 +03:00
|
|
|
gas.instant.should.be.at.least(gas.fast);
|
|
|
|
gas.fast.should.be.at.least(gas.standard);
|
|
|
|
gas.standard.should.be.at.least(gas.low);
|
2020-06-01 17:29:58 +03:00
|
|
|
gas.low.should.not.be.equal(0);
|
2020-05-27 18:52:57 +03:00
|
|
|
});
|
2020-06-04 02:08:09 +03:00
|
|
|
it('should fallback', async function () {
|
|
|
|
mockery.enable({ useCleanCache: true, warnOnUnregistered: false });
|
|
|
|
const { GasPriceOracle } = require('../src/index');
|
|
|
|
oracle = new GasPriceOracle();
|
|
|
|
const gas: GasPrice = await oracle.gasPrices();
|
|
|
|
|
|
|
|
gas.instant.should.be.equal(28.6);
|
|
|
|
gas.fast.should.be.equal(22);
|
|
|
|
gas.standard.should.be.equal(18.7);
|
|
|
|
gas.low.should.be.equal(11);
|
|
|
|
mockery.disable();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fallback to set values', async function () {
|
|
|
|
mockery.enable({ useCleanCache: true, warnOnUnregistered: false });
|
|
|
|
const { GasPriceOracle } = require('../src/index');
|
|
|
|
oracle = new GasPriceOracle();
|
|
|
|
const gas: GasPrice = await oracle.gasPrices({ instant: 50, fast: 21, standard: 10, low: 3 });
|
|
|
|
|
|
|
|
gas.instant.should.be.equal(50);
|
|
|
|
gas.fast.should.be.equal(21);
|
|
|
|
gas.standard.should.be.equal(10);
|
|
|
|
gas.low.should.be.equal(3);
|
|
|
|
mockery.disable();
|
|
|
|
});
|
2020-05-27 18:52:57 +03:00
|
|
|
});
|
2020-06-02 15:46:03 +03:00
|
|
|
|
2020-10-16 19:34:59 +03:00
|
|
|
describe('median', function () {
|
|
|
|
it('should work', async function () {
|
|
|
|
const gas1 = { instant: 100, fast: 100, standard: 100, low: 100 };
|
|
|
|
const gas2 = { instant: 90, fast: 90, standard: 90, low: 90 };
|
|
|
|
const gas3 = { instant: 70, fast: 70, standard: 70, low: 70 };
|
|
|
|
const gas4 = { instant: 110.1, fast: 110.1, standard: 110.1, low: 110.1 };
|
|
|
|
let gas: GasPrice = await oracle.median([gas1, gas2, gas3]);
|
|
|
|
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.eq(90);
|
|
|
|
gas.fast.should.be.eq(90);
|
|
|
|
gas.standard.should.be.eq(90);
|
|
|
|
gas.low.should.be.eq(90);
|
|
|
|
|
|
|
|
gas = await oracle.median([gas1, gas2, gas3, gas4]);
|
|
|
|
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.eq(95);
|
|
|
|
gas.fast.should.be.eq(95);
|
|
|
|
gas.standard.should.be.eq(95);
|
|
|
|
gas.low.should.be.eq(95);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('fetchMedianGasPriceOffChain', function () {
|
|
|
|
it('should work', async function () {
|
2020-10-19 15:02:47 +03:00
|
|
|
const gas: GasPrice = await oracle.fetchMedianGasPriceOffChain();
|
2020-10-16 19:34:59 +03:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-10-20 17:39:50 +03:00
|
|
|
describe('askOracle', function () {
|
|
|
|
it('all oracles should answer', async function () {
|
2021-06-03 12:51:14 +03:00
|
|
|
// TODO: remove after fix POA
|
|
|
|
delete offChainOracles['poa'];
|
|
|
|
|
2020-10-20 17:39:50 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-02 15:46:03 +03:00
|
|
|
after('after', function () {
|
|
|
|
after(function () {
|
|
|
|
mockery.disable();
|
|
|
|
mockery.deregisterMock('node-fetch');
|
|
|
|
});
|
|
|
|
});
|