Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add boundaries to oracle's gas price #133

Merged
merged 2 commits into from
Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/services/gasPrice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.`
akolotov marked this conversation as resolved.
Show resolved Hide resolved
)
}
return Web3Utils.toWei(gasPrice.toString(), 'gwei')
}
Expand Down Expand Up @@ -102,5 +110,6 @@ function getPrice() {
module.exports = {
start,
fetchGasPrice,
getPrice
getPrice,
gasPriceWithinLimits
}
4 changes: 4 additions & 0 deletions src/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ module.exports = {
GENERAL_ERROR: 1,
INCOMPATIBILITY: 10,
MAX_TIME_REACHED: 11
},
GAS_PRICE_BOUNDARIES: {
MIN: 1,
MAX: 250
}
}
40 changes: 39 additions & 1 deletion test/gasPrice.test.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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)
})
})
})