mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Merge pull request #1189 from MetaMask/kumavis-patch-1
tx-utils - clean and comment
This commit is contained in:
commit
b0059bef44
@ -53,29 +53,23 @@ module.exports = class txProviderUtils {
|
|||||||
}
|
}
|
||||||
// if gasLimit not originally specified,
|
// if gasLimit not originally specified,
|
||||||
// try adding an additional gas buffer to our estimation for safety
|
// try adding an additional gas buffer to our estimation for safety
|
||||||
const estimatedGasBn = new BN(ethUtil.stripHexPrefix(txData.estimatedGas), 16)
|
const recommendedGasHex = this.addGasBuffer(txData.estimatedGas, blockGasLimitHex)
|
||||||
const blockGasLimitBn = new BN(ethUtil.stripHexPrefix(blockGasLimitHex), 16)
|
txParams.gas = recommendedGasHex
|
||||||
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
|
|
||||||
}
|
|
||||||
cb()
|
cb()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
addGasBuffer (gas, blockGasLimitHex) {
|
addGasBuffer (initialGasLimitHex, blockGasLimitHex) {
|
||||||
const blockGasLimitBn = new BN(ethUtil.stripHexPrefix(blockGasLimitHex), 16)
|
const initialGasLimitBn = hexToBn(initialGasLimitHex)
|
||||||
const bnGas = new BN(ethUtil.stripHexPrefix(gas), 16)
|
const blockGasLimitBn = hexToBn(blockGasLimitHex)
|
||||||
const bufferedGas = bnGas.muln(1.5)
|
const bufferedGasLimitBn = initialGasLimitBn.muln(1.5)
|
||||||
|
|
||||||
if (bnGas.gt(blockGasLimitBn)) return gas
|
// if initialGasLimit is above blockGasLimit, dont modify it
|
||||||
if (bufferedGas.lt(blockGasLimitBn)) return ethUtil.addHexPrefix(bufferedGas.toString(16))
|
if (initialGasLimitBn.gt(blockGasLimitBn)) return bnToHex(initialGasLimitBn)
|
||||||
return ethUtil.addHexPrefix(blockGasLimitBn.toString(16))
|
// if bufferedGasLimit is below blockGasLimit, use bufferedGasLimit
|
||||||
|
if (bufferedGasLimitBn.lt(blockGasLimitBn)) return bnToHex(bufferedGasLimitBn)
|
||||||
|
// otherwise use blockGasLimit
|
||||||
|
return bnToHex(blockGasLimitBn)
|
||||||
}
|
}
|
||||||
|
|
||||||
fillInTxParams (txParams, cb) {
|
fillInTxParams (txParams, cb) {
|
||||||
@ -97,7 +91,7 @@ module.exports = class txProviderUtils {
|
|||||||
// builds ethTx from txParams object
|
// builds ethTx from txParams object
|
||||||
buildEthTxFromParams (txParams) {
|
buildEthTxFromParams (txParams) {
|
||||||
// apply gas multiplyer
|
// apply gas multiplyer
|
||||||
let gasPrice = new BN(ethUtil.stripHexPrefix(txParams.gasPrice), 16)
|
let gasPrice = hexToBn(txParams.gasPrice)
|
||||||
// multiply and divide by 100 so as to add percision to integer mul
|
// multiply and divide by 100 so as to add percision to integer mul
|
||||||
txParams.gasPrice = ethUtil.intToHex(gasPrice.toNumber())
|
txParams.gasPrice = ethUtil.intToHex(gasPrice.toNumber())
|
||||||
// normalize values
|
// normalize values
|
||||||
@ -133,3 +127,11 @@ module.exports = class txProviderUtils {
|
|||||||
function isUndef(value) {
|
function isUndef(value) {
|
||||||
return value === undefined
|
return value === undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function bnToHex(inputBn) {
|
||||||
|
return ethUtil.addHexPrefix(inputBn.toString(16))
|
||||||
|
}
|
||||||
|
|
||||||
|
function hexToBn(inputHex) {
|
||||||
|
return new BN(ethUtil.stripHexPrefix(inputHex), 16)
|
||||||
|
}
|
@ -13,14 +13,46 @@ describe('txUtils', function() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('addGasBuffer', function() {
|
describe('addGasBuffer', function() {
|
||||||
it('multiplies by 1.5', function() {
|
it('multiplies by 1.5, when within block gas limit', function() {
|
||||||
const input = '0x123fad'
|
// naive estimatedGas: 0x123fad (~1.2 mil)
|
||||||
const output = txUtils.addGasBuffer(input, '0x3d4c52') //0x3d4c52 is 4mil for dummy gas limit
|
const inputHex = '0x123fad'
|
||||||
|
// dummy gas limit: 0x3d4c52 (4 mil)
|
||||||
const inputBn = new BN(ethUtil.stripHexPrefix(input), 'hex')
|
const blockGasLimitHex = '0x3d4c52'
|
||||||
const outputBn = new BN(ethUtil.stripHexPrefix(output), 'hex')
|
const output = txUtils.addGasBuffer(inputHex, blockGasLimitHex)
|
||||||
|
const inputBn = hexToBn(inputHex)
|
||||||
|
const outputBn = hexToBn(output)
|
||||||
const expectedBn = inputBn.muln(1.5)
|
const expectedBn = inputBn.muln(1.5)
|
||||||
assert(outputBn.eq(expectedBn), 'returns 1.5 the input value')
|
assert(outputBn.eq(expectedBn), 'returns 1.5 the input value')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('uses original estimatedGas, when above block gas limit', function() {
|
||||||
|
// naive estimatedGas: 0x123fad (~1.2 mil)
|
||||||
|
const inputHex = '0x123fad'
|
||||||
|
// dummy gas limit: 0x0f4240 (1 mil)
|
||||||
|
const blockGasLimitHex = '0x0f4240'
|
||||||
|
const output = txUtils.addGasBuffer(inputHex, blockGasLimitHex)
|
||||||
|
const inputBn = hexToBn(inputHex)
|
||||||
|
const outputBn = hexToBn(output)
|
||||||
|
const expectedBn = hexToBn(inputHex)
|
||||||
|
assert(outputBn.eq(expectedBn), 'returns the original estimatedGas value')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('buffers up to block gas limit', function() {
|
||||||
|
// naive estimatedGas: 0x123fad (~1.2 mil)
|
||||||
|
const inputHex = '0x1e8480'
|
||||||
|
// dummy gas limit: 0x1e8480 (2 mil)
|
||||||
|
const blockGasLimitHex = '0x1e8480'
|
||||||
|
const output = txUtils.addGasBuffer(inputHex, blockGasLimitHex)
|
||||||
|
const inputBn = hexToBn(inputHex)
|
||||||
|
const outputBn = hexToBn(output)
|
||||||
|
const expectedBn = hexToBn(blockGasLimitHex)
|
||||||
|
assert(outputBn.eq(expectedBn), 'returns the block gas limit value')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// util
|
||||||
|
|
||||||
|
function hexToBn(inputHex) {
|
||||||
|
return new BN(ethUtil.stripHexPrefix(inputHex), 16)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user