2019-05-06 13:12:53 +02:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const { expect } = require('chai')
|
|
|
|
const proxyquire = require('proxyquire').noPreserveCache()
|
2019-08-05 17:22:57 +02:00
|
|
|
const { DEFAULT_UPDATE_INTERVAL } = require('../src/utils/constants')
|
2019-05-06 13:12:53 +02:00
|
|
|
|
2021-07-05 13:50:18 +03:00
|
|
|
const utils = { setIntervalAndRun: sinon.spy() }
|
|
|
|
const fetchStub = () => ({
|
|
|
|
json: () => ({
|
|
|
|
standard: '103'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
const fakeLogger = { error: sinon.spy(), warn: sinon.spy(), child: () => fakeLogger }
|
|
|
|
fetchStub['@global'] = true
|
|
|
|
const gasPriceDefault = proxyquire('../src/services/gasPrice', {
|
|
|
|
'../utils/utils': utils,
|
|
|
|
'node-fetch': fetchStub,
|
|
|
|
'../services/logger': { child: () => fakeLogger }
|
|
|
|
})
|
|
|
|
process.env.ORACLE_HOME_GAS_PRICE_UPDATE_INTERVAL = 15000
|
|
|
|
process.env.ORACLE_FOREIGN_GAS_PRICE_UPDATE_INTERVAL = 30000
|
|
|
|
process.env.COMMON_HOME_GAS_PRICE_FALLBACK = '101000000000'
|
|
|
|
const gasPrice = proxyquire('../src/services/gasPrice', {
|
|
|
|
'../utils/utils': utils,
|
|
|
|
'node-fetch': fetchStub,
|
|
|
|
'../services/logger': { child: () => fakeLogger }
|
|
|
|
})
|
|
|
|
|
2019-05-06 13:12:53 +02:00
|
|
|
describe('gasPrice', () => {
|
2021-07-05 13:50:18 +03:00
|
|
|
beforeEach(() => {
|
|
|
|
utils.setIntervalAndRun.resetHistory()
|
|
|
|
fakeLogger.error.resetHistory()
|
|
|
|
fakeLogger.warn.resetHistory()
|
|
|
|
})
|
|
|
|
|
2019-05-06 13:12:53 +02:00
|
|
|
describe('start', () => {
|
2019-09-13 09:11:38 +02:00
|
|
|
it('should call setIntervalAndRun with ORACLE_HOME_GAS_PRICE_UPDATE_INTERVAL interval value on Home', async () => {
|
2019-05-06 13:12:53 +02:00
|
|
|
// when
|
|
|
|
await gasPrice.start('home')
|
|
|
|
|
|
|
|
// then
|
2019-09-13 09:11:38 +02:00
|
|
|
expect(utils.setIntervalAndRun.args[0][1]).to.equal(process.env.ORACLE_HOME_GAS_PRICE_UPDATE_INTERVAL.toString())
|
2019-05-06 13:12:53 +02:00
|
|
|
})
|
2019-09-13 09:11:38 +02:00
|
|
|
it('should call setIntervalAndRun with ORACLE_FOREIGN_GAS_PRICE_UPDATE_INTERVAL interval value on Foreign', async () => {
|
2019-05-06 13:12:53 +02:00
|
|
|
// when
|
|
|
|
await gasPrice.start('foreign')
|
|
|
|
|
|
|
|
// then
|
2019-09-13 09:11:38 +02:00
|
|
|
expect(utils.setIntervalAndRun.args[0][1]).to.equal(
|
|
|
|
process.env.ORACLE_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 () => {
|
|
|
|
// when
|
2021-07-05 13:50:18 +03:00
|
|
|
await gasPriceDefault.start('home')
|
2019-05-06 13:12:53 +02:00
|
|
|
|
|
|
|
// then
|
|
|
|
expect(utils.setIntervalAndRun.args[0][1]).to.equal(DEFAULT_UPDATE_INTERVAL)
|
|
|
|
})
|
|
|
|
it('should call setIntervalAndRun with default interval value on Foreign', async () => {
|
|
|
|
// when
|
2021-07-05 13:50:18 +03:00
|
|
|
await gasPriceDefault.start('foreign')
|
2019-05-06 13:12:53 +02:00
|
|
|
|
|
|
|
// then
|
|
|
|
expect(utils.setIntervalAndRun.args[0][1]).to.equal(DEFAULT_UPDATE_INTERVAL)
|
|
|
|
})
|
|
|
|
})
|
2019-08-05 17:22:57 +02:00
|
|
|
|
|
|
|
describe('fetching gas price', () => {
|
2019-09-13 14:54:51 +02:00
|
|
|
it('should fall back to default if contract and supplier are not working', async () => {
|
2019-05-06 13:12:53 +02:00
|
|
|
// given
|
2019-08-05 17:22:57 +02:00
|
|
|
await gasPrice.start('home')
|
2019-05-06 13:12:53 +02:00
|
|
|
|
|
|
|
// when
|
2022-01-03 21:58:36 +07:00
|
|
|
await gasPrice.fetchGasPrice('standard', 1, null, null, null)
|
2019-05-06 13:12:53 +02:00
|
|
|
|
|
|
|
// then
|
2022-01-03 21:58:36 +07:00
|
|
|
expect(gasPrice.gasPriceOptions()).to.eql({ gasPrice: '101000000000' })
|
2019-05-06 13:12:53 +02:00
|
|
|
})
|
|
|
|
|
2019-09-13 14:54:51 +02:00
|
|
|
it('should fetch gas from supplier', async () => {
|
2019-08-05 17:22:57 +02:00
|
|
|
// given
|
|
|
|
await gasPrice.start('home')
|
2019-05-06 13:12:53 +02:00
|
|
|
|
2019-08-05 17:22:57 +02:00
|
|
|
// when
|
2022-01-03 21:58:36 +07:00
|
|
|
await gasPrice.fetchGasPrice('standard', 1, null, null, 'url')
|
2019-05-06 13:12:53 +02:00
|
|
|
|
2019-08-05 17:22:57 +02:00
|
|
|
// then
|
2022-01-03 21:58:36 +07:00
|
|
|
expect(gasPrice.gasPriceOptions()).to.eql({ gasPrice: '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
|
|
|
|
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
|
2022-01-03 21:58:36 +07:00
|
|
|
await gasPrice.fetchGasPrice('standard', 1, null, bridgeContractMock, null)
|
2019-07-12 13:10:17 -03:00
|
|
|
|
2019-08-05 17:22:57 +02:00
|
|
|
// then
|
2022-01-03 21:58:36 +07:00
|
|
|
expect(gasPrice.gasPriceOptions()).to.eql({ gasPrice: '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
|
|
|
|
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
|
2022-01-03 21:58:36 +07:00
|
|
|
await gasPrice.fetchGasPrice('standard', 1, null, bridgeContractMock, 'url')
|
2019-07-12 13:10:17 -03:00
|
|
|
|
2019-08-05 17:22:57 +02:00
|
|
|
// then
|
2022-01-03 21:58:36 +07:00
|
|
|
expect(gasPrice.gasPriceOptions()).to.eql({ gasPrice: '103000000000' })
|
2019-07-12 13:10:17 -03:00
|
|
|
})
|
|
|
|
|
2021-07-05 13:50:18 +03:00
|
|
|
it('log error using the logger', async () => {
|
2019-08-05 17:22:57 +02:00
|
|
|
// given
|
|
|
|
await gasPrice.start('home')
|
|
|
|
|
|
|
|
// when
|
2022-01-03 21:58:36 +07:00
|
|
|
await gasPrice.fetchGasPrice('standard', 1, null, null, null)
|
2019-07-12 13:10:17 -03:00
|
|
|
|
2019-08-05 17:22:57 +02:00
|
|
|
// then
|
2021-07-05 13:50:18 +03:00
|
|
|
expect(fakeLogger.warn.calledOnce).to.equal(true) // one warning
|
|
|
|
expect(fakeLogger.error.calledOnce).to.equal(true) // one error
|
2019-07-12 13:10:17 -03:00
|
|
|
})
|
|
|
|
})
|
2019-05-06 13:12:53 +02:00
|
|
|
})
|