mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-26 12:29:06 +01:00
tx-utils - stricter naming type-based convention
This commit is contained in:
parent
68b99dfb08
commit
c063fab993
@ -53,25 +53,23 @@ module.exports = class txProviderUtils {
|
||||
}
|
||||
// if gasLimit not originally specified,
|
||||
// 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 finalRecommendedGasBn = new BN(this.addGasBuffer(estimatedGasBn, blockGasLimitHex), 16)
|
||||
txParams.gas = ethUtil.intToHex(finalRecommendedGasBn)
|
||||
const recommendedGasHex = this.addGasBuffer(txData.estimatedGas, blockGasLimitHex)
|
||||
txParams.gas = recommendedGasHex
|
||||
cb()
|
||||
return
|
||||
}
|
||||
|
||||
addGasBuffer (initialGasLimitHex, blockGasLimitHex) {
|
||||
const blockGasLimitBn = new BN(ethUtil.stripHexPrefix(blockGasLimitHex), 16)
|
||||
const initialGasLimitBn = new BN(ethUtil.stripHexPrefix(initialGasLimitHex), 16)
|
||||
const initialGasLimitBn = hexToBn(initialGasLimitHex)
|
||||
const blockGasLimitBn = hexToBn(blockGasLimitHex)
|
||||
const bufferedGasLimitBn = initialGasLimitBn.muln(1.5)
|
||||
|
||||
// if initialGasLimit is above blockGasLimit, dont modify it
|
||||
if (initialGasLimitBn.gt(blockGasLimitBn)) return initialGasLimitHex
|
||||
if (initialGasLimitBn.gt(blockGasLimitBn)) return bnToHex(initialGasLimitBn)
|
||||
// if bufferedGasLimit is below blockGasLimit, use bufferedGasLimit
|
||||
if (bufferedGasLimitBn.lt(blockGasLimitBn)) return ethUtil.addHexPrefix(bufferedGasLimitBn.toString(16))
|
||||
if (bufferedGasLimitBn.lt(blockGasLimitBn)) return bnToHex(bufferedGasLimitBn)
|
||||
// otherwise use blockGasLimit
|
||||
return ethUtil.addHexPrefix(blockGasLimitBn.toString(16))
|
||||
return bnToHex(blockGasLimitBn)
|
||||
}
|
||||
|
||||
fillInTxParams (txParams, cb) {
|
||||
@ -129,3 +127,11 @@ module.exports = class txProviderUtils {
|
||||
function isUndef(value) {
|
||||
return value === undefined
|
||||
}
|
||||
|
||||
function bnToHex(inputBn) {
|
||||
return ethUtil.addHexPrefix(inputBn.toString(16))
|
||||
}
|
||||
|
||||
function hexToBn(inputHex) {
|
||||
return new BN(ethUtil.stripHexPrefix(inputHex), 16)
|
||||
}
|
@ -15,11 +15,11 @@ describe('txUtils', function() {
|
||||
describe('addGasBuffer', function() {
|
||||
it('multiplies by 1.5, when within block gas limit', function() {
|
||||
// naive estimatedGas: 0x123fad (~1.2 mil)
|
||||
const input = '0x123fad'
|
||||
const inputHex = '0x123fad'
|
||||
// dummy gas limit: 0x3d4c52 (4 mil)
|
||||
const blockGasLimit = '0x3d4c52'
|
||||
const output = txUtils.addGasBuffer(input, blockGasLimit)
|
||||
const inputBn = new BN(ethUtil.stripHexPrefix(input), 'hex')
|
||||
const blockGasLimitHex = '0x3d4c52'
|
||||
const output = txUtils.addGasBuffer(inputHex, blockGasLimitHex)
|
||||
const inputBn = new BN(ethUtil.stripHexPrefix(inputHex), 'hex')
|
||||
const outputBn = new BN(ethUtil.stripHexPrefix(output), 'hex')
|
||||
const expectedBn = inputBn.muln(1.5)
|
||||
assert(outputBn.eq(expectedBn), 'returns 1.5 the input value')
|
||||
@ -27,25 +27,25 @@ describe('txUtils', function() {
|
||||
|
||||
it('uses original estimatedGas, when above block gas limit', function() {
|
||||
// naive estimatedGas: 0x123fad (~1.2 mil)
|
||||
const input = '0x123fad'
|
||||
const inputHex = '0x123fad'
|
||||
// dummy gas limit: 0x0f4240 (1 mil)
|
||||
const blockGasLimit = '0x0f4240'
|
||||
const output = txUtils.addGasBuffer(input, blockGasLimit)
|
||||
const inputBn = new BN(ethUtil.stripHexPrefix(input), 'hex')
|
||||
const blockGasLimitHex = '0x0f4240'
|
||||
const output = txUtils.addGasBuffer(inputHex, blockGasLimitHex)
|
||||
const inputBn = new BN(ethUtil.stripHexPrefix(inputHex), 'hex')
|
||||
const outputBn = new BN(ethUtil.stripHexPrefix(output), 'hex')
|
||||
const expectedBn = new BN(ethUtil.stripHexPrefix(input), 'hex')
|
||||
const expectedBn = new BN(ethUtil.stripHexPrefix(inputHex), 'hex')
|
||||
assert(outputBn.eq(expectedBn), 'returns the original estimatedGas value')
|
||||
})
|
||||
|
||||
it('buffers up to block gas limit', function() {
|
||||
// naive estimatedGas: 0x123fad (~1.2 mil)
|
||||
const input = '0x1e8480'
|
||||
const inputHex = '0x1e8480'
|
||||
// dummy gas limit: 0x1e8480 (2 mil)
|
||||
const blockGasLimit = '0x1e8480'
|
||||
const output = txUtils.addGasBuffer(input, blockGasLimit)
|
||||
const inputBn = new BN(ethUtil.stripHexPrefix(input), 'hex')
|
||||
const blockGasLimitHex = '0x1e8480'
|
||||
const output = txUtils.addGasBuffer(inputHex, blockGasLimitHex)
|
||||
const inputBn = new BN(ethUtil.stripHexPrefix(inputHex), 'hex')
|
||||
const outputBn = new BN(ethUtil.stripHexPrefix(output), 'hex')
|
||||
const expectedBn = new BN(ethUtil.stripHexPrefix(blockGasLimit), 'hex')
|
||||
const expectedBn = new BN(ethUtil.stripHexPrefix(blockGasLimitHex), 'hex')
|
||||
assert(outputBn.eq(expectedBn), 'returns the block gas limit value')
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user