1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

tx-utils - clean and comment

This commit is contained in:
kumavis 2017-03-07 22:18:14 -08:00 committed by GitHub
parent de5a30c5ff
commit 17b805c76e

View File

@ -55,26 +55,22 @@ module.exports = class txProviderUtils {
// try adding an additional gas buffer to our estimation for safety
const estimatedGasBn = new BN(ethUtil.stripHexPrefix(txData.estimatedGas), 16)
const blockGasLimitBn = new BN(ethUtil.stripHexPrefix(blockGasLimitHex), 16)
const estimationWithBuffer = new BN(this.addGasBuffer(estimatedGasBn, blockGasLimitHex), 16)
// added gas buffer is too high
if (estimationWithBuffer.gt(blockGasLimitBn)) {
txParams.gas = txData.estimatedGas
// added gas buffer is safe
} else {
const gasWithBufferHex = ethUtil.intToHex(estimationWithBuffer)
txParams.gas = gasWithBufferHex
}
const finalRecommendedGasBn = new BN(this.addGasBuffer(estimatedGasBn, blockGasLimitHex), 16)
txParams.gas = ethUtil.intToHex(finalRecommendedGasBn)
cb()
return
}
addGasBuffer (gas, blockGasLimitHex) {
addGasBuffer (initialGasLimitHex, blockGasLimitHex) {
const blockGasLimitBn = new BN(ethUtil.stripHexPrefix(blockGasLimitHex), 16)
const bnGas = new BN(ethUtil.stripHexPrefix(gas), 16)
const bufferedGas = bnGas.muln(1.5)
if (bnGas.gt(blockGasLimitBn)) return gas
if (bufferedGas.lt(blockGasLimitBn)) return ethUtil.addHexPrefix(bufferedGas.toString(16))
const initialGasLimitBn = new BN(ethUtil.stripHexPrefix(initialGasLimitHex), 16)
const bufferedGasLimitBn = initialGasLimitBn.muln(1.5)
// if initialGasLimit is above blockGasLimit, dont modify it
if (initialGasLimitBn.gt(blockGasLimitBn)) return initialGasLimitHex
// if bufferedGasLimit is below blockGasLimit, use bufferedGasLimit
if (bufferedGasLimitBn.lt(blockGasLimitBn)) return ethUtil.addHexPrefix(bufferedGasLimitBn.toString(16))
// otherwise use blockGasLimit
return ethUtil.addHexPrefix(blockGasLimitBn.toString(16))
}