2019-05-06 14:12:53 +03:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const { expect } = require('chai')
|
|
|
|
const proxyquire = require('proxyquire').noPreserveCache()
|
2019-08-05 18:22:57 +03:00
|
|
|
const { DEFAULT_UPDATE_INTERVAL } = require('../src/utils/constants')
|
2019-05-06 14:12:53 +03:00
|
|
|
|
|
|
|
describe('gasPrice', () => {
|
|
|
|
describe('start', () => {
|
|
|
|
const utils = { setIntervalAndRun: sinon.spy() }
|
|
|
|
beforeEach(() => {
|
|
|
|
utils.setIntervalAndRun.resetHistory()
|
|
|
|
})
|
2019-09-13 10:11:38 +03:00
|
|
|
it('should call setIntervalAndRun with ORACLE_HOME_GAS_PRICE_UPDATE_INTERVAL interval value on Home', async () => {
|
2019-05-06 14:12:53 +03:00
|
|
|
// given
|
2019-09-13 10:11:38 +03:00
|
|
|
process.env.ORACLE_HOME_GAS_PRICE_UPDATE_INTERVAL = 15000
|
2019-05-06 14:12:53 +03:00
|
|
|
const gasPrice = proxyquire('../src/services/gasPrice', { '../utils/utils': utils })
|
|
|
|
|
|
|
|
// when
|
|
|
|
await gasPrice.start('home')
|
|
|
|
|
|
|
|
// then
|
2019-09-13 10:11:38 +03:00
|
|
|
expect(process.env.ORACLE_HOME_GAS_PRICE_UPDATE_INTERVAL).to.equal('15000')
|
|
|
|
expect(process.env.ORACLE_HOME_GAS_PRICE_UPDATE_INTERVAL).to.not.equal(DEFAULT_UPDATE_INTERVAL.toString())
|
|
|
|
expect(utils.setIntervalAndRun.args[0][1]).to.equal(process.env.ORACLE_HOME_GAS_PRICE_UPDATE_INTERVAL.toString())
|
2019-05-06 14:12:53 +03:00
|
|
|
})
|
2019-09-13 10:11:38 +03:00
|
|
|
it('should call setIntervalAndRun with ORACLE_FOREIGN_GAS_PRICE_UPDATE_INTERVAL interval value on Foreign', async () => {
|
2019-05-06 14:12:53 +03:00
|
|
|
// given
|
2019-09-13 10:11:38 +03:00
|
|
|
process.env.ORACLE_FOREIGN_GAS_PRICE_UPDATE_INTERVAL = 15000
|
2019-05-06 14:12:53 +03:00
|
|
|
const gasPrice = proxyquire('../src/services/gasPrice', { '../utils/utils': utils })
|
|
|
|
|
|
|
|
// when
|
|
|
|
await gasPrice.start('foreign')
|
|
|
|
|
|
|
|
// then
|
2019-09-13 10:11:38 +03:00
|
|
|
expect(process.env.ORACLE_FOREIGN_GAS_PRICE_UPDATE_INTERVAL).to.equal('15000')
|
|
|
|
expect(process.env.ORACLE_HOME_GAS_PRICE_UPDATE_INTERVAL).to.not.equal(DEFAULT_UPDATE_INTERVAL.toString())
|
|
|
|
expect(utils.setIntervalAndRun.args[0][1]).to.equal(
|
|
|
|
process.env.ORACLE_FOREIGN_GAS_PRICE_UPDATE_INTERVAL.toString()
|
|
|
|
)
|
2019-05-06 14:12:53 +03:00
|
|
|
})
|
|
|
|
it('should call setIntervalAndRun with default interval value on Home', async () => {
|
|
|
|
// given
|
2019-09-13 10:11:38 +03:00
|
|
|
delete process.env.ORACLE_HOME_GAS_PRICE_UPDATE_INTERVAL
|
2019-05-06 14:12:53 +03:00
|
|
|
const gasPrice = proxyquire('../src/services/gasPrice', { '../utils/utils': utils })
|
|
|
|
|
|
|
|
// when
|
|
|
|
await gasPrice.start('home')
|
|
|
|
|
|
|
|
// then
|
2019-09-13 10:11:38 +03:00
|
|
|
expect(process.env.ORACLE_HOME_GAS_PRICE_UPDATE_INTERVAL).to.equal(undefined)
|
2019-05-06 14:12:53 +03:00
|
|
|
expect(utils.setIntervalAndRun.args[0][1]).to.equal(DEFAULT_UPDATE_INTERVAL)
|
|
|
|
})
|
|
|
|
it('should call setIntervalAndRun with default interval value on Foreign', async () => {
|
|
|
|
// given
|
2019-09-13 10:11:38 +03:00
|
|
|
delete process.env.ORACLE_FOREIGN_GAS_PRICE_UPDATE_INTERVAL
|
2019-05-06 14:12:53 +03:00
|
|
|
const gasPrice = proxyquire('../src/services/gasPrice', { '../utils/utils': utils })
|
|
|
|
|
|
|
|
// when
|
|
|
|
await gasPrice.start('foreign')
|
|
|
|
|
|
|
|
// then
|
2019-09-13 10:11:38 +03:00
|
|
|
expect(process.env.ORACLE_FOREIGN_GAS_PRICE_UPDATE_INTERVAL).to.equal(undefined)
|
2019-05-06 14:12:53 +03:00
|
|
|
expect(utils.setIntervalAndRun.args[0][1]).to.equal(DEFAULT_UPDATE_INTERVAL)
|
|
|
|
})
|
|
|
|
})
|
2019-08-05 18:22:57 +03:00
|
|
|
|
|
|
|
describe('fetching gas price', () => {
|
|
|
|
const utils = { setIntervalAndRun: () => {} }
|
|
|
|
|
2019-09-13 15:54:51 +03:00
|
|
|
it('should fall back to default if contract and supplier are not working', async () => {
|
2019-05-06 14:12:53 +03:00
|
|
|
// given
|
2019-09-13 10:11:38 +03:00
|
|
|
process.env.COMMON_HOME_GAS_PRICE_FALLBACK = '101000000000'
|
2019-08-05 18:22:57 +03:00
|
|
|
const gasPrice = proxyquire('../src/services/gasPrice', { '../utils/utils': utils })
|
|
|
|
await gasPrice.start('home')
|
2019-05-06 14:12:53 +03:00
|
|
|
|
|
|
|
// when
|
2019-08-05 18:22:57 +03:00
|
|
|
await gasPrice.fetchGasPrice('standard', 1, null, null)
|
2019-05-06 14:12:53 +03:00
|
|
|
|
|
|
|
// then
|
2019-08-05 18:22:57 +03:00
|
|
|
expect(gasPrice.getPrice()).to.equal('101000000000')
|
2019-05-06 14:12:53 +03:00
|
|
|
})
|
|
|
|
|
2019-09-13 15:54:51 +03:00
|
|
|
it('should fetch gas from supplier', async () => {
|
2019-08-05 18:22:57 +03:00
|
|
|
// given
|
2019-09-13 10:11:38 +03:00
|
|
|
process.env.COMMON_HOME_GAS_PRICE_FALLBACK = '101000000000'
|
2019-08-05 18:22:57 +03:00
|
|
|
const gasPrice = proxyquire('../src/services/gasPrice', { '../utils/utils': utils })
|
|
|
|
await gasPrice.start('home')
|
2019-05-06 14:12:53 +03:00
|
|
|
|
2019-09-13 15:54:51 +03:00
|
|
|
const gasPriceSupplierFetchFn = () => ({
|
2019-08-05 18:22:57 +03:00
|
|
|
json: () => ({
|
|
|
|
standard: '103'
|
|
|
|
})
|
|
|
|
})
|
2019-05-06 14:12:53 +03:00
|
|
|
|
2019-08-05 18:22:57 +03:00
|
|
|
// when
|
2019-09-13 15:54:51 +03:00
|
|
|
await gasPrice.fetchGasPrice('standard', 1, null, gasPriceSupplierFetchFn)
|
2019-05-06 14:12:53 +03:00
|
|
|
|
2019-08-05 18:22:57 +03:00
|
|
|
// then
|
|
|
|
expect(gasPrice.getPrice().toString()).to.equal('103000000000')
|
2019-05-06 14:12:53 +03:00
|
|
|
})
|
2019-07-12 19:10:17 +03:00
|
|
|
|
2019-08-05 18:22:57 +03:00
|
|
|
it('should fetch gas from contract', async () => {
|
|
|
|
// given
|
2019-09-13 10:11:38 +03:00
|
|
|
process.env.COMMON_HOME_GAS_PRICE_FALLBACK = '101000000000'
|
2019-08-05 18:22:57 +03:00
|
|
|
const gasPrice = proxyquire('../src/services/gasPrice', { '../utils/utils': utils })
|
|
|
|
await gasPrice.start('home')
|
2019-07-12 19:10:17 +03:00
|
|
|
|
2019-08-05 18:22:57 +03:00
|
|
|
const bridgeContractMock = {
|
|
|
|
methods: {
|
|
|
|
gasPrice: sinon.stub().returns({
|
|
|
|
call: sinon.stub().returns(Promise.resolve('102000000000'))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-07-12 19:10:17 +03:00
|
|
|
|
2019-08-05 18:22:57 +03:00
|
|
|
// when
|
|
|
|
await gasPrice.fetchGasPrice('standard', 1, bridgeContractMock, null)
|
2019-07-12 19:10:17 +03:00
|
|
|
|
2019-08-05 18:22:57 +03:00
|
|
|
// then
|
|
|
|
expect(gasPrice.getPrice().toString()).to.equal('102000000000')
|
2019-07-12 19:10:17 +03:00
|
|
|
})
|
|
|
|
|
2019-08-05 18:22:57 +03:00
|
|
|
it('should fetch the gas price from the oracle first', async () => {
|
|
|
|
// given
|
2019-09-13 10:11:38 +03:00
|
|
|
process.env.COMMON_HOME_GAS_PRICE_FALLBACK = '101000000000'
|
2019-08-05 18:22:57 +03:00
|
|
|
const gasPrice = proxyquire('../src/services/gasPrice', { '../utils/utils': utils })
|
|
|
|
await gasPrice.start('home')
|
2019-07-12 19:10:17 +03:00
|
|
|
|
2019-08-05 18:22:57 +03:00
|
|
|
const bridgeContractMock = {
|
|
|
|
methods: {
|
|
|
|
gasPrice: sinon.stub().returns({
|
|
|
|
call: sinon.stub().returns(Promise.resolve('102000000000'))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-07-12 19:10:17 +03:00
|
|
|
|
2019-09-13 15:54:51 +03:00
|
|
|
const gasPriceSupplierFetchFn = () => ({
|
2019-08-05 18:22:57 +03:00
|
|
|
json: () => ({
|
|
|
|
standard: '103'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// when
|
2019-09-13 15:54:51 +03:00
|
|
|
await gasPrice.fetchGasPrice('standard', 1, bridgeContractMock, gasPriceSupplierFetchFn)
|
2019-07-12 19:10:17 +03:00
|
|
|
|
2019-08-05 18:22:57 +03:00
|
|
|
// then
|
|
|
|
expect(gasPrice.getPrice().toString()).to.equal('103000000000')
|
2019-07-12 19:10:17 +03:00
|
|
|
})
|
|
|
|
|
2019-08-05 18:22:57 +03: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')
|
|
|
|
|
|
|
|
// when
|
|
|
|
await gasPrice.fetchGasPrice('standard', 1, null, null)
|
2019-07-12 19:10:17 +03:00
|
|
|
|
2019-08-05 18:22:57 +03:00
|
|
|
// then
|
|
|
|
expect(fakeLogger.error.calledTwice).to.equal(true) // two errors
|
2019-07-12 19:10:17 +03:00
|
|
|
})
|
|
|
|
})
|
2019-05-06 14:12:53 +03:00
|
|
|
})
|