diff options
author | kumavis <kumavis@users.noreply.github.com> | 2017-03-29 05:48:03 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-29 05:48:03 +0800 |
commit | 0f1ea5861f10193ab958fea6ba10a5e2aed4c839 (patch) | |
tree | 35755083efcb9bedad73bdcd340432c298866125 /app/scripts/lib/tx-utils.js | |
parent | 731f6654094fb6cac832d4092471c51b554a34d4 (diff) | |
parent | 5d38792910147dfcc00028a2f8b262a47cab295f (diff) | |
download | tangerine-wallet-browser-0f1ea5861f10193ab958fea6ba10a5e2aed4c839.tar tangerine-wallet-browser-0f1ea5861f10193ab958fea6ba10a5e2aed4c839.tar.gz tangerine-wallet-browser-0f1ea5861f10193ab958fea6ba10a5e2aed4c839.tar.bz2 tangerine-wallet-browser-0f1ea5861f10193ab958fea6ba10a5e2aed4c839.tar.lz tangerine-wallet-browser-0f1ea5861f10193ab958fea6ba10a5e2aed4c839.tar.xz tangerine-wallet-browser-0f1ea5861f10193ab958fea6ba10a5e2aed4c839.tar.zst tangerine-wallet-browser-0f1ea5861f10193ab958fea6ba10a5e2aed4c839.zip |
Merge pull request #1276 from MetaMask/ImproveGasEstimates
Improve UI gas calculation logic
Diffstat (limited to 'app/scripts/lib/tx-utils.js')
-rw-r--r-- | app/scripts/lib/tx-utils.js | 32 |
1 files changed, 15 insertions, 17 deletions
diff --git a/app/scripts/lib/tx-utils.js b/app/scripts/lib/tx-utils.js index 7988f83e9..72df53631 100644 --- a/app/scripts/lib/tx-utils.js +++ b/app/scripts/lib/tx-utils.js @@ -12,48 +12,49 @@ and used to do things like calculate gas of a tx. */ module.exports = class txProviderUtils { + constructor (provider) { this.provider = provider this.query = new EthQuery(provider) } - analyzeGasUsage (txData, cb) { + analyzeGasUsage (txMeta, cb) { var self = this this.query.getBlockByNumber('latest', true, (err, block) => { if (err) return cb(err) async.waterfall([ - self.estimateTxGas.bind(self, txData, block.gasLimit), - self.setTxGas.bind(self, txData, block.gasLimit), + self.estimateTxGas.bind(self, txMeta, block.gasLimit), + self.setTxGas.bind(self, txMeta, block.gasLimit), ], cb) }) } - estimateTxGas (txData, blockGasLimitHex, cb) { - const txParams = txData.txParams + estimateTxGas (txMeta, blockGasLimitHex, cb) { + const txParams = txMeta.txParams // check if gasLimit is already specified - txData.gasLimitSpecified = Boolean(txParams.gas) + txMeta.gasLimitSpecified = Boolean(txParams.gas) // if not, fallback to block gasLimit - if (!txData.gasLimitSpecified) { + if (!txMeta.gasLimitSpecified) { txParams.gas = blockGasLimitHex } // run tx, see if it will OOG this.query.estimateGas(txParams, cb) } - setTxGas (txData, blockGasLimitHex, estimatedGasHex, cb) { - txData.estimatedGas = estimatedGasHex - const txParams = txData.txParams + setTxGas (txMeta, blockGasLimitHex, estimatedGasHex, cb) { + txMeta.estimatedGas = estimatedGasHex + const txParams = txMeta.txParams // if gasLimit was specified and doesnt OOG, // use original specified amount - if (txData.gasLimitSpecified) { - txData.estimatedGas = txParams.gas + if (txMeta.gasLimitSpecified) { + txMeta.estimatedGas = txParams.gas cb() return } // if gasLimit not originally specified, // try adding an additional gas buffer to our estimation for safety - const recommendedGasHex = this.addGasBuffer(txData.estimatedGas, blockGasLimitHex) + const recommendedGasHex = this.addGasBuffer(txMeta.estimatedGas, blockGasLimitHex) txParams.gas = recommendedGasHex cb() return @@ -90,16 +91,13 @@ module.exports = class txProviderUtils { // builds ethTx from txParams object buildEthTxFromParams (txParams) { - // apply gas multiplyer - 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 txParams.to = normalize(txParams.to) txParams.from = normalize(txParams.from) txParams.value = normalize(txParams.value) txParams.data = normalize(txParams.data) txParams.gas = normalize(txParams.gas || txParams.gasLimit) + txParams.gasPrice = normalize(txParams.gasPrice) txParams.nonce = normalize(txParams.nonce) // build ethTx log.info(`Prepared tx for signing: ${JSON.stringify(txParams)}`) |