From 17b805c76ea3ea160bcb2560d2043581b54c4834 Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 7 Mar 2017 22:18:14 -0800 Subject: tx-utils - clean and comment --- app/scripts/lib/tx-utils.js | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) (limited to 'app') diff --git a/app/scripts/lib/tx-utils.js b/app/scripts/lib/tx-utils.js index f5051eb8f..32a8a012a 100644 --- a/app/scripts/lib/tx-utils.js +++ b/app/scripts/lib/tx-utils.js @@ -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)) } -- cgit v1.2.3 From c063fab9932c6b90414fd7db7849012d6594afb7 Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 7 Mar 2017 22:47:35 -0800 Subject: tx-utils - stricter naming type-based convention --- app/scripts/lib/tx-utils.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'app') diff --git a/app/scripts/lib/tx-utils.js b/app/scripts/lib/tx-utils.js index 32a8a012a..f1171851c 100644 --- a/app/scripts/lib/tx-utils.js +++ b/app/scripts/lib/tx-utils.js @@ -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) +} \ No newline at end of file -- cgit v1.2.3 From 92b8443824bffca219b146028d5685e6aa1ffabf Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 7 Mar 2017 22:51:39 -0800 Subject: tx-utils - add encoding utils --- app/scripts/lib/tx-utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app') diff --git a/app/scripts/lib/tx-utils.js b/app/scripts/lib/tx-utils.js index f1171851c..c6814c05f 100644 --- a/app/scripts/lib/tx-utils.js +++ b/app/scripts/lib/tx-utils.js @@ -91,7 +91,7 @@ module.exports = class txProviderUtils { // builds ethTx from txParams object buildEthTxFromParams (txParams) { // 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 txParams.gasPrice = ethUtil.intToHex(gasPrice.toNumber()) // normalize values -- cgit v1.2.3