Add a buffer to new ui gas estimates

feature/default_network_editable
Dan 7 years ago
parent 1b9ed23752
commit b9b6cbf561
  1. 43
      ui/app/components/send_/send.utils.js
  2. 11
      ui/app/conversion-util.js

@ -4,6 +4,7 @@ const {
conversionGTE,
multiplyCurrencies,
conversionGreaterThan,
conversionLessThan,
} = require('../../conversion-util')
const {
calcTokenAmount,
@ -201,16 +202,54 @@ async function estimateGas ({ selectedAddress, selectedToken, blockGasLimit, to,
err.message.includes('gas required exceeds allowance or always failing transaction')
)
if (simulationFailed) {
return resolve(ethUtil.addHexPrefix(paramsForGasEstimate.gas))
const estimateWithBuffer = addGasBuffer(
paramsForGasEstimate.gas,
blockGasLimit,
selectedToken ? 2 : 1.5
)
return resolve(ethUtil.addHexPrefix(estimateWithBuffer))
} else {
return reject(err)
}
}
return resolve(ethUtil.addHexPrefix(estimatedGas.toString(16)))
const estimateWithBuffer = addGasBuffer(
estimatedGas.toString(16),
blockGasLimit,
selectedToken ? 2 : 1.5
)
return resolve(ethUtil.addHexPrefix(estimateWithBuffer))
})
})
}
function addGasBuffer (initialGasLimitHex, blockGasLimitHex, bufferMultiplier = 1.5) {
const upperGasLimit = multiplyCurrencies(blockGasLimitHex, 0.9, {
toNumericBase: 'hex',
multiplicandBase: 16,
multiplierBase: 10,
numberOfDecimals: '0',
})
const bufferedGasLimit = multiplyCurrencies(initialGasLimitHex, bufferMultiplier, {
toNumericBase: 'hex',
multiplicandBase: 16,
multiplierBase: 10,
numberOfDecimals: '0',
})
// if initialGasLimit is above blockGasLimit, dont modify it
if (conversionGreaterThan(
{ value: initialGasLimitHex, fromNumericBase: 'hex' },
{ value: upperGasLimit, fromNumericBase: 'hex' },
)) return initialGasLimitHex
// if bufferedGasLimit is below blockGasLimit, use bufferedGasLimit
if (conversionLessThan(
{ value: bufferedGasLimit, fromNumericBase: 'hex' },
{ value: upperGasLimit, fromNumericBase: 'hex' },
)) return bufferedGasLimit
// otherwise use blockGasLimit
return upperGasLimit
}
function generateTokenTransferData ({ toAddress = '0x0', amount = '0x0', selectedToken }) {
if (!selectedToken) return
return TOKEN_TRANSFER_FUNCTION_SIGNATURE + Array.prototype.map.call(

@ -190,6 +190,16 @@ const conversionGreaterThan = (
return firstValue.gt(secondValue)
}
const conversionLessThan = (
{ ...firstProps },
{ ...secondProps },
) => {
const firstValue = converter({ ...firstProps })
const secondValue = converter({ ...secondProps })
return firstValue.lt(secondValue)
}
const conversionMax = (
{ ...firstProps },
{ ...secondProps },
@ -229,6 +239,7 @@ module.exports = {
addCurrencies,
multiplyCurrencies,
conversionGreaterThan,
conversionLessThan,
conversionGTE,
conversionLTE,
conversionMax,

Loading…
Cancel
Save