gas-price-oracle/tests/index.test.ts

141 lines
4.5 KiB
TypeScript
Raw Normal View History

2020-05-27 18:52:57 +03:00
import { GasPrice } from '../src/types';
2020-06-01 17:29:58 +03:00
import mockery from 'mockery';
import chai from 'chai';
2020-06-02 15:46:03 +03:00
import { onChainOracles } from '../src/config';
2020-05-27 18:52:57 +03:00
2020-06-02 15:46:03 +03:00
const { GasPriceOracle } = require('../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();
before('before', function () {
let fetchMock = () => {
throw new Error('Mocked for tests');
};
mockery.registerMock('node-fetch', fetchMock);
});
beforeEach('beforeEach', function () {
oracle = new GasPriceOracle();
});
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');
gas.instant.should.be.above(gas.fast);
gas.fast.should.be.above(gas.standard);
gas.standard.should.be.above(gas.low);
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-06-01 17:29:58 +03:00
await oracle.fetchGasPricesOffChain().should.be.rejectedWith('All oracles are down. Probaly 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();
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-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-06-02 15:46:03 +03:00
oracle.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-06-01 17:29:58 +03:00
await oracle.fetchGasPricesOnChain().should.be.rejectedWith('All oracles are down. Probaly network error.');
});
2020-06-02 15:46:03 +03:00
it('should add oracle', async function () {
const { chainlink } = onChainOracles;
await oracle.fetchGasPricesOnChain();
oracle.removeOnChainOracle('chainlink');
await oracle.fetchGasPricesOnChain().should.be.rejectedWith('All oracles are down. Probaly network error.');
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();
await oracle.fetchGasPricesOnChain().should.be.rejectedWith('All oracles are down. Probaly network error.');
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');
gas.instant.should.be.above(gas.fast);
gas.fast.should.be.above(gas.standard);
gas.standard.should.be.above(gas.low);
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
after('after', function () {
after(function () {
mockery.disable();
mockery.deregisterMock('node-fetch');
});
});