diff options
author | Dan Finlay <flyswatter@users.noreply.github.com> | 2017-03-08 13:41:54 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-08 13:41:54 +0800 |
commit | de5a30c5ff3acbcda7a46a3caba70a622308dbb2 (patch) | |
tree | 378135a51316e92cf58025b8823e7f109145a88b | |
parent | a8551c4fe2c86b0cab5afa23040cc0c9b0cf444f (diff) | |
parent | 0889309bf32fbb23f8bcea037338a9b240439e64 (diff) | |
download | tangerine-wallet-browser-de5a30c5ff3acbcda7a46a3caba70a622308dbb2.tar tangerine-wallet-browser-de5a30c5ff3acbcda7a46a3caba70a622308dbb2.tar.gz tangerine-wallet-browser-de5a30c5ff3acbcda7a46a3caba70a622308dbb2.tar.bz2 tangerine-wallet-browser-de5a30c5ff3acbcda7a46a3caba70a622308dbb2.tar.lz tangerine-wallet-browser-de5a30c5ff3acbcda7a46a3caba70a622308dbb2.tar.xz tangerine-wallet-browser-de5a30c5ff3acbcda7a46a3caba70a622308dbb2.tar.zst tangerine-wallet-browser-de5a30c5ff3acbcda7a46a3caba70a622308dbb2.zip |
Merge pull request #1187 from MetaMask/i1118-IncreaseGasBuffer
I1118 increase gas buffer
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | app/scripts/lib/tx-utils.js | 13 | ||||
-rw-r--r-- | test/unit/tx-utils-test.js | 26 |
3 files changed, 35 insertions, 5 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index d07154bdf..603a92df5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Add personal_sign method support. - Add ability to customize gas and gasPrice on the transaction approval screen. +- Increase default gas buffer to 1.5x estimated gas value. ## 3.3.0 2017-2-20 diff --git a/app/scripts/lib/tx-utils.js b/app/scripts/lib/tx-utils.js index 19a2d430e..f5051eb8f 100644 --- a/app/scripts/lib/tx-utils.js +++ b/app/scripts/lib/tx-utils.js @@ -55,7 +55,7 @@ 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), 16) + const estimationWithBuffer = new BN(this.addGasBuffer(estimatedGasBn, blockGasLimitHex), 16) // added gas buffer is too high if (estimationWithBuffer.gt(blockGasLimitBn)) { txParams.gas = txData.estimatedGas @@ -68,11 +68,14 @@ module.exports = class txProviderUtils { return } - addGasBuffer (gas) { - const gasBuffer = new BN('100000', 10) + addGasBuffer (gas, blockGasLimitHex) { + const blockGasLimitBn = new BN(ethUtil.stripHexPrefix(blockGasLimitHex), 16) const bnGas = new BN(ethUtil.stripHexPrefix(gas), 16) - const correct = bnGas.add(gasBuffer) - return ethUtil.addHexPrefix(correct.toString(16)) + const bufferedGas = bnGas.muln(1.5) + + if (bnGas.gt(blockGasLimitBn)) return gas + if (bufferedGas.lt(blockGasLimitBn)) return ethUtil.addHexPrefix(bufferedGas.toString(16)) + return ethUtil.addHexPrefix(blockGasLimitBn.toString(16)) } fillInTxParams (txParams, cb) { diff --git a/test/unit/tx-utils-test.js b/test/unit/tx-utils-test.js new file mode 100644 index 000000000..65233e1d9 --- /dev/null +++ b/test/unit/tx-utils-test.js @@ -0,0 +1,26 @@ +const assert = require('assert') +const ethUtil = require('ethereumjs-util') +const BN = ethUtil.BN + +const TxUtils = require('../../app/scripts/lib/tx-utils') + + +describe('txUtils', function() { + let txUtils + + before(function() { + txUtils = new TxUtils() + }) + + describe('addGasBuffer', function() { + it('multiplies by 1.5', function() { + const input = '0x123fad' + const output = txUtils.addGasBuffer(input, '0x3d4c52') //0x3d4c52 is 4mil for dummy gas limit + + const inputBn = new BN(ethUtil.stripHexPrefix(input), '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') + }) + }) +}) |