diff --git a/src/services/gasPrice.js b/src/services/gasPrice.js index 4a37352..9f472ce 100644 --- a/src/services/gasPrice.js +++ b/src/services/gasPrice.js @@ -7,7 +7,7 @@ const logger = require('../services/logger').child({ module: 'gasPrice' }) const { setIntervalAndRun } = require('../utils/utils') -const { DEFAULT_UPDATE_INTERVAL } = require('../utils/constants') +const { DEFAULT_UPDATE_INTERVAL, GAS_PRICE_BOUNDARIES } = require('../utils/constants') const HomeABI = bridgeConfig.homeBridgeAbi const ForeignABI = bridgeConfig.foreignBridgeAbi @@ -31,13 +31,24 @@ const foreignBridge = new web3Foreign.eth.Contract(ForeignABI, FOREIGN_BRIDGE_AD let cachedGasPrice = null +function gasPriceWithinLimits(gasPrice) { + if (gasPrice < GAS_PRICE_BOUNDARIES.MIN) { + return GAS_PRICE_BOUNDARIES.MIN + } else if (gasPrice > GAS_PRICE_BOUNDARIES.MAX) { + return GAS_PRICE_BOUNDARIES.MAX + } else { + return gasPrice + } +} + async function fetchGasPriceFromOracle(oracleUrl, speedType) { const response = await fetch(oracleUrl) const json = await response.json() - const gasPrice = json[speedType] - if (!gasPrice) { + const oracleGasPrice = json[speedType] + if (!oracleGasPrice) { throw new Error(`Response from Oracle didn't include gas price for ${speedType} type.`) } + const gasPrice = gasPriceWithinLimits(oracleGasPrice) return Web3Utils.toWei(gasPrice.toString(), 'gwei') } @@ -102,5 +113,6 @@ function getPrice() { module.exports = { start, fetchGasPrice, - getPrice + getPrice, + gasPriceWithinLimits } diff --git a/src/utils/constants.js b/src/utils/constants.js index 4579a21..5351781 100644 --- a/src/utils/constants.js +++ b/src/utils/constants.js @@ -12,5 +12,9 @@ module.exports = { GENERAL_ERROR: 1, INCOMPATIBILITY: 10, MAX_TIME_REACHED: 11 + }, + GAS_PRICE_BOUNDARIES: { + MIN: 1, + MAX: 250 } } diff --git a/test/gasPrice.test.js b/test/gasPrice.test.js index a8276c0..6707de9 100644 --- a/test/gasPrice.test.js +++ b/test/gasPrice.test.js @@ -1,8 +1,8 @@ const sinon = require('sinon') const { expect } = require('chai') const proxyquire = require('proxyquire').noPreserveCache() -const { fetchGasPrice } = require('../src/services/gasPrice') -const { DEFAULT_UPDATE_INTERVAL } = require('../src/utils/constants') +const { fetchGasPrice, gasPriceWithinLimits } = require('../src/services/gasPrice') +const { DEFAULT_UPDATE_INTERVAL, GAS_PRICE_BOUNDARIES } = require('../src/utils/constants') describe('gasPrice', () => { describe('fetchGasPrice', () => { @@ -138,4 +138,42 @@ describe('gasPrice', () => { expect(utils.setIntervalAndRun.args[0][1]).to.equal(DEFAULT_UPDATE_INTERVAL) }) }) + describe('gasPriceWithinLimits', () => { + it('should return gas price if gas price is between boundaries', () => { + // given + const minGasPrice = 1 + const middleGasPrice = 10 + const maxGasPrice = 250 + + // when + const minGasPriceWithinLimits = gasPriceWithinLimits(minGasPrice) + const middleGasPriceWithinLimits = gasPriceWithinLimits(middleGasPrice) + const maxGasPriceWithinLimits = gasPriceWithinLimits(maxGasPrice) + + // then + expect(minGasPriceWithinLimits).to.equal(minGasPrice) + expect(middleGasPriceWithinLimits).to.equal(middleGasPrice) + expect(maxGasPriceWithinLimits).to.equal(maxGasPrice) + }) + it('should return min limit if gas price is below min boundary', () => { + // Given + const initialGasPrice = 0.5 + + // When + const gasPrice = gasPriceWithinLimits(initialGasPrice) + + // Then + expect(gasPrice).to.equal(GAS_PRICE_BOUNDARIES.MIN) + }) + it('should return max limit if gas price is above max boundary', () => { + // Given + const initialGasPrice = 260 + + // When + const gasPrice = gasPriceWithinLimits(initialGasPrice) + + // Then + expect(gasPrice).to.equal(GAS_PRICE_BOUNDARIES.MAX) + }) + }) })