2019-05-06 13:12:53 +02:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const { expect } = require('chai')
|
|
|
|
const proxyquire = require('proxyquire').noPreserveCache()
|
2019-05-24 15:42:26 -03:00
|
|
|
const Web3Utils = require('web3-utils')
|
2019-08-22 12:10:49 -03:00
|
|
|
const { processGasPriceOptions } = require('../src/services/gasPrice')
|
2019-08-05 17:22:57 +02:00
|
|
|
const { DEFAULT_UPDATE_INTERVAL } = require('../src/utils/constants')
|
2019-07-25 12:36:10 -03:00
|
|
|
const { GAS_PRICE_OPTIONS, ORACLE_GAS_PRICE_SPEEDS } = require('../../commons')
|
2019-05-06 13:12:53 +02:00
|
|
|
|
|
|
|
describe('gasPrice', () => {
|
|
|
|
describe('start', () => {
|
|
|
|
const utils = { setIntervalAndRun: sinon.spy() }
|
|
|
|
beforeEach(() => {
|
|
|
|
utils.setIntervalAndRun.resetHistory()
|
|
|
|
})
|
|
|
|
it('should call setIntervalAndRun with HOME_GAS_PRICE_UPDATE_INTERVAL interval value on Home', async () => {
|
|
|
|
// given
|
|
|
|
process.env.HOME_GAS_PRICE_UPDATE_INTERVAL = 15000
|
|
|
|
const gasPrice = proxyquire('../src/services/gasPrice', { '../utils/utils': utils })
|
|
|
|
|
|
|
|
// when
|
|
|
|
await gasPrice.start('home')
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(process.env.HOME_GAS_PRICE_UPDATE_INTERVAL).to.equal('15000')
|
2019-08-01 15:10:22 +02:00
|
|
|
expect(process.env.HOME_GAS_PRICE_UPDATE_INTERVAL).to.not.equal(DEFAULT_UPDATE_INTERVAL.toString())
|
|
|
|
expect(utils.setIntervalAndRun.args[0][1]).to.equal(process.env.HOME_GAS_PRICE_UPDATE_INTERVAL.toString())
|
2019-05-06 13:12:53 +02:00
|
|
|
})
|
|
|
|
it('should call setIntervalAndRun with FOREIGN_GAS_PRICE_UPDATE_INTERVAL interval value on Foreign', async () => {
|
|
|
|
// given
|
|
|
|
process.env.FOREIGN_GAS_PRICE_UPDATE_INTERVAL = 15000
|
|
|
|
const gasPrice = proxyquire('../src/services/gasPrice', { '../utils/utils': utils })
|
|
|
|
|
|
|
|
// when
|
|
|
|
await gasPrice.start('foreign')
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(process.env.FOREIGN_GAS_PRICE_UPDATE_INTERVAL).to.equal('15000')
|
2019-08-01 15:10:22 +02:00
|
|
|
expect(process.env.HOME_GAS_PRICE_UPDATE_INTERVAL).to.not.equal(DEFAULT_UPDATE_INTERVAL.toString())
|
|
|
|
expect(utils.setIntervalAndRun.args[0][1]).to.equal(process.env.FOREIGN_GAS_PRICE_UPDATE_INTERVAL.toString())
|
2019-05-06 13:12:53 +02:00
|
|
|
})
|
|
|
|
it('should call setIntervalAndRun with default interval value on Home', async () => {
|
|
|
|
// given
|
|
|
|
delete process.env.HOME_GAS_PRICE_UPDATE_INTERVAL
|
|
|
|
const gasPrice = proxyquire('../src/services/gasPrice', { '../utils/utils': utils })
|
|
|
|
|
|
|
|
// when
|
|
|
|
await gasPrice.start('home')
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(process.env.HOME_GAS_PRICE_UPDATE_INTERVAL).to.equal(undefined)
|
|
|
|
expect(utils.setIntervalAndRun.args[0][1]).to.equal(DEFAULT_UPDATE_INTERVAL)
|
|
|
|
})
|
|
|
|
it('should call setIntervalAndRun with default interval value on Foreign', async () => {
|
|
|
|
// given
|
|
|
|
delete process.env.FOREIGN_GAS_PRICE_UPDATE_INTERVAL
|
|
|
|
const gasPrice = proxyquire('../src/services/gasPrice', { '../utils/utils': utils })
|
|
|
|
|
|
|
|
// when
|
|
|
|
await gasPrice.start('foreign')
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(process.env.FOREIGN_GAS_PRICE_UPDATE_INTERVAL).to.equal(undefined)
|
|
|
|
expect(utils.setIntervalAndRun.args[0][1]).to.equal(DEFAULT_UPDATE_INTERVAL)
|
|
|
|
})
|
|
|
|
})
|
2019-08-05 17:22:57 +02:00
|
|
|
|
|
|
|
describe('fetching gas price', () => {
|
|
|
|
const utils = { setIntervalAndRun: () => {} }
|
|
|
|
|
|
|
|
it('should fall back to default if contract and oracle/supplier are not working', async () => {
|
2019-05-06 13:12:53 +02:00
|
|
|
// given
|
2019-08-05 17:22:57 +02:00
|
|
|
process.env.HOME_GAS_PRICE_FALLBACK = '101000000000'
|
|
|
|
const gasPrice = proxyquire('../src/services/gasPrice', { '../utils/utils': utils })
|
|
|
|
await gasPrice.start('home')
|
2019-05-06 13:12:53 +02:00
|
|
|
|
|
|
|
// when
|
2019-08-05 17:22:57 +02:00
|
|
|
await gasPrice.fetchGasPrice('standard', 1, null, null)
|
2019-05-06 13:12:53 +02:00
|
|
|
|
|
|
|
// then
|
2019-08-05 17:22:57 +02:00
|
|
|
expect(gasPrice.getPrice()).to.equal('101000000000')
|
2019-05-06 13:12:53 +02:00
|
|
|
})
|
|
|
|
|
2019-08-05 17:22:57 +02:00
|
|
|
it('should fetch gas from oracle/supplier', async () => {
|
|
|
|
// given
|
|
|
|
process.env.HOME_GAS_PRICE_FALLBACK = '101000000000'
|
|
|
|
const gasPrice = proxyquire('../src/services/gasPrice', { '../utils/utils': utils })
|
|
|
|
await gasPrice.start('home')
|
2019-05-06 13:12:53 +02:00
|
|
|
|
2019-08-05 17:22:57 +02:00
|
|
|
const oracleFetchFn = () => ({
|
|
|
|
json: () => ({
|
|
|
|
standard: '103'
|
|
|
|
})
|
|
|
|
})
|
2019-05-06 13:12:53 +02:00
|
|
|
|
2019-08-05 17:22:57 +02:00
|
|
|
// when
|
|
|
|
await gasPrice.fetchGasPrice('standard', 1, null, oracleFetchFn)
|
2019-05-06 13:12:53 +02:00
|
|
|
|
2019-08-05 17:22:57 +02:00
|
|
|
// then
|
|
|
|
expect(gasPrice.getPrice().toString()).to.equal('103000000000')
|
2019-05-06 13:12:53 +02:00
|
|
|
})
|
2019-07-12 13:10:17 -03:00
|
|
|
|
2019-08-05 17:22:57 +02:00
|
|
|
it('should fetch gas from contract', async () => {
|
|
|
|
// given
|
|
|
|
process.env.HOME_GAS_PRICE_FALLBACK = '101000000000'
|
|
|
|
const gasPrice = proxyquire('../src/services/gasPrice', { '../utils/utils': utils })
|
|
|
|
await gasPrice.start('home')
|
2019-07-12 13:10:17 -03:00
|
|
|
|
2019-08-05 17:22:57 +02:00
|
|
|
const bridgeContractMock = {
|
|
|
|
methods: {
|
|
|
|
gasPrice: sinon.stub().returns({
|
|
|
|
call: sinon.stub().returns(Promise.resolve('102000000000'))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-07-12 13:10:17 -03:00
|
|
|
|
2019-08-05 17:22:57 +02:00
|
|
|
// when
|
|
|
|
await gasPrice.fetchGasPrice('standard', 1, bridgeContractMock, null)
|
2019-07-12 13:10:17 -03:00
|
|
|
|
2019-08-05 17:22:57 +02:00
|
|
|
// then
|
|
|
|
expect(gasPrice.getPrice().toString()).to.equal('102000000000')
|
2019-07-12 13:10:17 -03:00
|
|
|
})
|
|
|
|
|
2019-08-05 17:22:57 +02:00
|
|
|
it('should fetch the gas price from the oracle first', async () => {
|
|
|
|
// given
|
|
|
|
process.env.HOME_GAS_PRICE_FALLBACK = '101000000000'
|
|
|
|
const gasPrice = proxyquire('../src/services/gasPrice', { '../utils/utils': utils })
|
|
|
|
await gasPrice.start('home')
|
2019-07-12 13:10:17 -03:00
|
|
|
|
2019-08-05 17:22:57 +02:00
|
|
|
const bridgeContractMock = {
|
|
|
|
methods: {
|
|
|
|
gasPrice: sinon.stub().returns({
|
|
|
|
call: sinon.stub().returns(Promise.resolve('102000000000'))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-07-12 13:10:17 -03:00
|
|
|
|
2019-08-05 17:22:57 +02:00
|
|
|
const oracleFetchFn = () => ({
|
|
|
|
json: () => ({
|
|
|
|
standard: '103'
|
|
|
|
})
|
|
|
|
})
|
2019-07-12 13:10:17 -03:00
|
|
|
|
2019-08-05 17:22:57 +02:00
|
|
|
// when
|
|
|
|
await gasPrice.fetchGasPrice('standard', 1, bridgeContractMock, oracleFetchFn)
|
2019-07-12 13:10:17 -03:00
|
|
|
|
2019-08-05 17:22:57 +02:00
|
|
|
// then
|
|
|
|
expect(gasPrice.getPrice().toString()).to.equal('103000000000')
|
2019-07-12 13:10:17 -03:00
|
|
|
})
|
|
|
|
|
2019-08-05 17:22:57 +02:00
|
|
|
it('log errors using the logger', async () => {
|
|
|
|
// given
|
|
|
|
const fakeLogger = { error: sinon.spy() }
|
|
|
|
const gasPrice = proxyquire('../src/services/gasPrice', {
|
|
|
|
'../utils/utils': utils,
|
|
|
|
'../services/logger': { child: () => fakeLogger }
|
|
|
|
})
|
|
|
|
await gasPrice.start('home')
|
2019-07-12 13:10:17 -03:00
|
|
|
|
2019-08-05 17:22:57 +02:00
|
|
|
// when
|
|
|
|
await gasPrice.fetchGasPrice('standard', 1, null, null)
|
2019-07-12 13:10:17 -03:00
|
|
|
|
2019-08-05 17:22:57 +02:00
|
|
|
// then
|
|
|
|
expect(fakeLogger.error.calledTwice).to.equal(true) // two errors
|
2019-07-12 13:10:17 -03:00
|
|
|
})
|
|
|
|
})
|
2019-05-24 15:42:26 -03:00
|
|
|
describe('processGasPriceOptions', () => {
|
|
|
|
const oracleMockResponse = {
|
|
|
|
fast: 17.64,
|
|
|
|
block_time: 13.548,
|
|
|
|
health: true,
|
|
|
|
standard: 10.64,
|
|
|
|
block_number: 6704240,
|
|
|
|
instant: 51.9,
|
|
|
|
slow: 4.4
|
|
|
|
}
|
|
|
|
it('should return cached gas price if no options provided', async () => {
|
|
|
|
// given
|
|
|
|
const options = {}
|
|
|
|
const cachedGasPrice = '1000000000'
|
|
|
|
|
|
|
|
// when
|
|
|
|
const gasPrice = await processGasPriceOptions({
|
|
|
|
options,
|
|
|
|
cachedGasPrice,
|
|
|
|
cachedGasPriceOracleSpeeds: oracleMockResponse
|
|
|
|
})
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(gasPrice).to.equal(cachedGasPrice)
|
|
|
|
})
|
|
|
|
it('should return gas price provided by options', async () => {
|
|
|
|
// given
|
|
|
|
const options = {
|
|
|
|
type: GAS_PRICE_OPTIONS.GAS_PRICE,
|
|
|
|
value: '3000000000'
|
|
|
|
}
|
|
|
|
const cachedGasPrice = '1000000000'
|
|
|
|
|
|
|
|
// when
|
|
|
|
const gasPrice = await processGasPriceOptions({
|
|
|
|
options,
|
|
|
|
cachedGasPrice,
|
|
|
|
cachedGasPriceOracleSpeeds: oracleMockResponse
|
|
|
|
})
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(gasPrice).to.equal(options.value)
|
|
|
|
})
|
|
|
|
it('should return gas price provided by oracle speed option', async () => {
|
|
|
|
// given
|
|
|
|
const options = {
|
|
|
|
type: GAS_PRICE_OPTIONS.SPEED,
|
|
|
|
value: ORACLE_GAS_PRICE_SPEEDS.STANDARD
|
|
|
|
}
|
|
|
|
const cachedGasPrice = '1000000000'
|
|
|
|
const oracleGasPriceGwei = oracleMockResponse[ORACLE_GAS_PRICE_SPEEDS.STANDARD]
|
|
|
|
const oracleGasPrice = Web3Utils.toWei(oracleGasPriceGwei.toString(), 'gwei')
|
|
|
|
|
|
|
|
// when
|
|
|
|
const gasPrice = await processGasPriceOptions({
|
|
|
|
options,
|
|
|
|
cachedGasPrice,
|
|
|
|
cachedGasPriceOracleSpeeds: oracleMockResponse
|
|
|
|
})
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(gasPrice).to.equal(oracleGasPrice)
|
|
|
|
})
|
|
|
|
it('should return cached gas price if invalid speed option', async () => {
|
|
|
|
// given
|
|
|
|
const options = {
|
|
|
|
type: GAS_PRICE_OPTIONS.SPEED,
|
|
|
|
value: 'unknown'
|
|
|
|
}
|
|
|
|
const cachedGasPrice = '1000000000'
|
|
|
|
|
|
|
|
// when
|
|
|
|
const gasPrice = await processGasPriceOptions({
|
|
|
|
options,
|
|
|
|
cachedGasPrice,
|
|
|
|
cachedGasPriceOracleSpeeds: oracleMockResponse
|
|
|
|
})
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(gasPrice).to.equal(cachedGasPrice)
|
|
|
|
})
|
|
|
|
})
|
2019-05-06 13:12:53 +02:00
|
|
|
})
|