Skip to content

Commit

Permalink
Merge pull request #133 from poanetwork/#132-boundaries-oracle-gas-price
Browse files Browse the repository at this point in the history
Add boundaries to oracle's gas price
  • Loading branch information
akolotov authored Mar 12, 2019
2 parents 24122ef + 7654da6 commit 212c0e3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
20 changes: 16 additions & 4 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,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')
}

Expand Down Expand Up @@ -102,5 +113,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
}
}
42 changes: 40 additions & 2 deletions test/gasPrice.test.js
Original file line number Diff line number Diff line change
@@ -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', () => {
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 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)
})
})
})

0 comments on commit 212c0e3

Please sign in to comment.