From c1418da4ce82fc454742ed23703849faa1618205 Mon Sep 17 00:00:00 2001 From: Gerardo Nardelli Date: Thu, 21 Feb 2019 11:19:00 -0300 Subject: [PATCH 1/2] Add boundaries to oracle gas price --- src/services/gasPrice.js | 13 +++++++++++-- src/utils/constants.js | 4 ++++ test/gasPrice.test.js | 40 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/services/gasPrice.js b/src/services/gasPrice.js index 4a37352..b500f87 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,12 +31,20 @@ const foreignBridge = new web3Foreign.eth.Contract(ForeignABI, FOREIGN_BRIDGE_AD let cachedGasPrice = null +function gasPriceWithinLimits(gasPrice) { + return GAS_PRICE_BOUNDARIES.MIN <= gasPrice && gasPrice <= GAS_PRICE_BOUNDARIES.MAX +} + async function fetchGasPriceFromOracle(oracleUrl, speedType) { const response = await fetch(oracleUrl) const json = await response.json() const gasPrice = json[speedType] if (!gasPrice) { throw new Error(`Response from Oracle didn't include gas price for ${speedType} type.`) + } else if (!gasPriceWithinLimits(gasPrice)) { + throw new Error( + `Response from Oracle included a gas price out of boundaries ${gasPrice} for ${speedType} type.` + ) } return Web3Utils.toWei(gasPrice.toString(), 'gwei') } @@ -102,5 +110,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..cd59eda 100644 --- a/test/gasPrice.test.js +++ b/test/gasPrice.test.js @@ -1,7 +1,7 @@ const sinon = require('sinon') const { expect } = require('chai') const proxyquire = require('proxyquire').noPreserveCache() -const { fetchGasPrice } = require('../src/services/gasPrice') +const { fetchGasPrice, gasPriceWithinLimits } = require('../src/services/gasPrice') const { DEFAULT_UPDATE_INTERVAL } = require('../src/utils/constants') describe('gasPrice', () => { @@ -138,4 +138,42 @@ describe('gasPrice', () => { expect(utils.setIntervalAndRun.args[0][1]).to.equal(DEFAULT_UPDATE_INTERVAL) }) }) + describe('gasPriceWithinLimits', () => { + it('should return true 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(true) + expect(middleGasPriceWithinLimits).to.equal(true) + expect(maxGasPriceWithinLimits).to.equal(true) + }) + it('should return false if gas price is below min boundary', () => { + // Given + const gasPrice = 0.5 + + // When + const isGasPriceWithinLimits = gasPriceWithinLimits(gasPrice) + + // Then + expect(isGasPriceWithinLimits).to.equal(false) + }) + it('should return false if gas price is above max boundary', () => { + // Given + const gasPrice = 260 + + // When + const isGasPriceWithinLimits = gasPriceWithinLimits(gasPrice) + + // Then + expect(isGasPriceWithinLimits).to.equal(false) + }) + }) }) From 7654da6df98826ec28f0dbca74e8c13e220cc9b4 Mon Sep 17 00:00:00 2001 From: Gerardo Nardelli Date: Mon, 11 Mar 2019 10:59:59 -0300 Subject: [PATCH 2/2] Use limit value if oracle gas price out of boundaries --- src/services/gasPrice.js | 17 ++++++++++------- test/gasPrice.test.js | 26 +++++++++++++------------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/services/gasPrice.js b/src/services/gasPrice.js index b500f87..9f472ce 100644 --- a/src/services/gasPrice.js +++ b/src/services/gasPrice.js @@ -32,20 +32,23 @@ const foreignBridge = new web3Foreign.eth.Contract(ForeignABI, FOREIGN_BRIDGE_AD let cachedGasPrice = null function gasPriceWithinLimits(gasPrice) { - return GAS_PRICE_BOUNDARIES.MIN <= gasPrice && gasPrice <= GAS_PRICE_BOUNDARIES.MAX + 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.`) - } else if (!gasPriceWithinLimits(gasPrice)) { - throw new Error( - `Response from Oracle included a gas price out of boundaries ${gasPrice} for ${speedType} type.` - ) } + const gasPrice = gasPriceWithinLimits(oracleGasPrice) return Web3Utils.toWei(gasPrice.toString(), 'gwei') } diff --git a/test/gasPrice.test.js b/test/gasPrice.test.js index cd59eda..6707de9 100644 --- a/test/gasPrice.test.js +++ b/test/gasPrice.test.js @@ -2,7 +2,7 @@ const sinon = require('sinon') const { expect } = require('chai') const proxyquire = require('proxyquire').noPreserveCache() const { fetchGasPrice, gasPriceWithinLimits } = require('../src/services/gasPrice') -const { DEFAULT_UPDATE_INTERVAL } = require('../src/utils/constants') +const { DEFAULT_UPDATE_INTERVAL, GAS_PRICE_BOUNDARIES } = require('../src/utils/constants') describe('gasPrice', () => { describe('fetchGasPrice', () => { @@ -139,7 +139,7 @@ describe('gasPrice', () => { }) }) describe('gasPriceWithinLimits', () => { - it('should return true if gas price is between boundaries', () => { + it('should return gas price if gas price is between boundaries', () => { // given const minGasPrice = 1 const middleGasPrice = 10 @@ -151,29 +151,29 @@ describe('gasPrice', () => { const maxGasPriceWithinLimits = gasPriceWithinLimits(maxGasPrice) // then - expect(minGasPriceWithinLimits).to.equal(true) - expect(middleGasPriceWithinLimits).to.equal(true) - expect(maxGasPriceWithinLimits).to.equal(true) + expect(minGasPriceWithinLimits).to.equal(minGasPrice) + expect(middleGasPriceWithinLimits).to.equal(middleGasPrice) + expect(maxGasPriceWithinLimits).to.equal(maxGasPrice) }) - it('should return false if gas price is below min boundary', () => { + it('should return min limit if gas price is below min boundary', () => { // Given - const gasPrice = 0.5 + const initialGasPrice = 0.5 // When - const isGasPriceWithinLimits = gasPriceWithinLimits(gasPrice) + const gasPrice = gasPriceWithinLimits(initialGasPrice) // Then - expect(isGasPriceWithinLimits).to.equal(false) + expect(gasPrice).to.equal(GAS_PRICE_BOUNDARIES.MIN) }) - it('should return false if gas price is above max boundary', () => { + it('should return max limit if gas price is above max boundary', () => { // Given - const gasPrice = 260 + const initialGasPrice = 260 // When - const isGasPriceWithinLimits = gasPriceWithinLimits(gasPrice) + const gasPrice = gasPriceWithinLimits(initialGasPrice) // Then - expect(isGasPriceWithinLimits).to.equal(false) + expect(gasPrice).to.equal(GAS_PRICE_BOUNDARIES.MAX) }) }) })