aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/scripts/lib/tx-utils.js13
-rw-r--r--test/unit/tx-utils-test.js26
2 files changed, 34 insertions, 5 deletions
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')
+ })
+ })
+})