From de24fd57c6e8fdb00eda64c83c34eb42746f60b7 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 21 Feb 2018 19:52:06 -0500 Subject: [PATCH] extract confirmation dialog call to caller --- src/app/execution/txRunner.js | 72 +++++-------------------------- src/universal-dapp.js | 79 +++++++++++++++++++++++++++++++---- 2 files changed, 81 insertions(+), 70 deletions(-) diff --git a/src/app/execution/txRunner.js b/src/app/execution/txRunner.js index 4293d2e669..9263de3cd8 100644 --- a/src/app/execution/txRunner.js +++ b/src/app/execution/txRunner.js @@ -4,11 +4,7 @@ var EthJSBlock = require('ethereumjs-block') var ethJSUtil = require('ethereumjs-util') var BN = ethJSUtil.BN var executionContext = require('../../execution-context') -var modalDialog = require('../ui/modaldialog') var modal = require('../ui/modal-dialog-custom') -var typeConversion = require('./typeConversion') - -var confirmDialog = require('./confirmDialog') function TxRunner (vmaccounts, api) { this._api = api @@ -23,8 +19,8 @@ function TxRunner (vmaccounts, api) { this.queusTxs = [] } -TxRunner.prototype.rawRun = function (args, cb) { - run(this, args, Date.now(), cb) +TxRunner.prototype.rawRun = function (args, confirmationCb, cb) { + run(this, args, Date.now(), confirmationCb, cb) } function executeTx (tx, gasPrice, api, callback) { @@ -41,7 +37,7 @@ function executeTx (tx, gasPrice, api, callback) { } } -TxRunner.prototype.execute = function (args, callback) { +TxRunner.prototype.execute = function (args, confirmationCb, callback) { var self = this var data = args.data @@ -50,7 +46,7 @@ TxRunner.prototype.execute = function (args, callback) { } if (!executionContext.isVM()) { - self.runInNode(args.from, args.to, data, args.value, args.gasLimit, args.useCall, callback) + self.runInNode(args.from, args.to, data, args.value, args.gasLimit, args.useCall, confirmationCb, callback) } else { try { self.runInVm(args.from, args.to, data, args.value, args.gasLimit, args.useCall, callback) @@ -108,7 +104,7 @@ TxRunner.prototype.runInVm = function (from, to, data, value, gasLimit, useCall, }) } -TxRunner.prototype.runInNode = function (from, to, data, value, gasLimit, useCall, callback) { +TxRunner.prototype.runInNode = function (from, to, data, value, gasLimit, useCall, confirmCb, callback) { const self = this var tx = { from: from, to: to, data: data, value: value } @@ -142,62 +138,16 @@ TxRunner.prototype.runInNode = function (from, to, data, value, gasLimit, useCal if (self._api.config.getUnpersistedProperty('doNotShowTransactionConfirmationAgain')) { return executeTx(tx, null, self._api, callback) } + self._api.detectNetwork((err, network) => { if (err) { console.log(err) return } - if (network.name !== 'Main') { - return executeTx(tx, null, self._api, callback) - } - var amount = executionContext.web3().fromWei(typeConversion.toInt(tx.value), 'ether') - var content = confirmDialog(tx, amount, gasEstimation, self, - (gasPrice, cb) => { - let txFeeText, priceStatus - // TODO: this try catch feels like an anti pattern, can/should be - // removed, but for now keeping the original logic - try { - var fee = executionContext.web3().toBigNumber(tx.gas).mul(executionContext.web3().toBigNumber(executionContext.web3().toWei(gasPrice.toString(10), 'gwei'))) - txFeeText = ' ' + executionContext.web3().fromWei(fee.toString(10), 'ether') + ' Ether' - priceStatus = true - } catch (e) { - txFeeText = ' Please fix this issue before sending any transaction. ' + e.message - priceStatus = false - } - cb(priceStatus, txFeeText) - }, - (cb) => { - executionContext.web3().eth.getGasPrice((error, gasPrice) => { - var warnMessage = ' Please fix this issue before sending any transaction. ' - if (error) { - return cb('Unable to retrieve the current network gas price.' + warnMessage + error) - } - try { - var gasPriceValue = executionContext.web3().fromWei(gasPrice.toString(10), 'gwei') - cb(null, gasPriceValue) - } catch (e) { - cb(warnMessage + e.message, null, false) - } - }) - } - ) - - modalDialog('Confirm transaction', content, - { label: 'Confirm', - fn: () => { - self._api.config.setUnpersistedProperty('doNotShowTransactionConfirmationAgain', content.querySelector('input#confirmsetting').checked) - if (!content.gasPriceStatus) { - return callback('Given gas grice is not correct') - } - var gasPrice = executionContext.web3().toWei(content.querySelector('#gasprice').value, 'gwei') - executeTx(tx, gasPrice, self._api, callback) - }}, { - label: 'Cancel', - fn: () => { - return callback('Transaction canceled by user.') - } - }) + confirmCb(network, tx, gasEstimation, callback, (gasPrice) => { + return executeTx(tx, gasPrice, self._api, callback) + }) }) }) } @@ -231,12 +181,12 @@ function sendTransaction (sendTx, tx, pass, callback) { } } -function run (self, tx, stamp, callback) { +function run (self, tx, stamp, confirmationCb, callback) { if (!self.runAsync && Object.keys(self.pendingTxs).length) { self.queusTxs.push({ tx, stamp, callback }) } else { self.pendingTxs[stamp] = tx - self.execute(tx, (error, result) => { + self.execute(tx, confirmationCb, (error, result) => { delete self.pendingTxs[stamp] callback(error, result) if (self.queusTxs.length) { diff --git a/src/universal-dapp.js b/src/universal-dapp.js index 24bfd10921..3d75113e0c 100644 --- a/src/universal-dapp.js +++ b/src/universal-dapp.js @@ -14,6 +14,10 @@ var txHelper = remixLib.execution.txHelper var executionContext = require('./execution-context') var modalCustom = require('./app/ui/modal-dialog-custom') +var modalDialog = require('./app/ui/modaldialog') +var typeConversion = require('./app/execution/typeConversion') +var confirmDialog = require('./app/execution/confirmDialog') + /* trigger debugRequested */ @@ -248,18 +252,75 @@ UniversalDApp.prototype.runTx = function (args, cb) { var timestamp = Date.now() self.event.trigger('initiatingTransaction', [timestamp, tx, payLoad]) - self.txRunner.rawRun(tx, function (error, result) { - let eventName = (tx.useCall ? 'callExecuted' : 'transactionExecuted') - self.event.trigger(eventName, [error, tx.from, tx.to, tx.data, tx.useCall, result, timestamp, payLoad]) + self.txRunner.rawRun(tx, - if (error && (typeof (error) !== 'string')) { - if (error.message) error = error.message - else { - try { error = 'error: ' + JSON.stringify(error) } catch (e) {} + (network, tx, continueTxExecution, gasEstimation, cancelCb) => { + if (network.name !== 'Main') { + return continueTxExecution(null) } + var amount = executionContext.web3().fromWei(typeConversion.toInt(tx.value), 'ether') + var content = confirmDialog(tx, amount, gasEstimation, self, + (gasPrice, cb) => { + let txFeeText, priceStatus + // TODO: this try catch feels like an anti pattern, can/should be + // removed, but for now keeping the original logic + try { + var fee = executionContext.web3().toBigNumber(tx.gas).mul(executionContext.web3().toBigNumber(executionContext.web3().toWei(gasPrice.toString(10), 'gwei'))) + txFeeText = ' ' + executionContext.web3().fromWei(fee.toString(10), 'ether') + ' Ether' + priceStatus = true + } catch (e) { + txFeeText = ' Please fix this issue before sending any transaction. ' + e.message + priceStatus = false + } + cb(txFeeText, priceStatus) + }, + (cb) => { + executionContext.web3().eth.getGasPrice((error, gasPrice) => { + var warnMessage = ' Please fix this issue before sending any transaction. ' + if (error) { + return cb('Unable to retrieve the current network gas price.' + warnMessage + error) + } + try { + var gasPriceValue = executionContext.web3().fromWei(gasPrice.toString(10), 'gwei') + cb(null, gasPriceValue) + } catch (e) { + cb(warnMessage + e.message, null, false) + } + }) + } + ) + modalDialog('Confirm transaction', content, + { label: 'Confirm', + fn: () => { + self._api.config.setUnpersistedProperty('doNotShowTransactionConfirmationAgain', content.querySelector('input#confirmsetting').checked) + // TODO: check if this is check is still valid given the refactor + if (!content.gasPriceStatus) { + cancelCb('Given gas price is not correct') + } else { + var gasPrice = executionContext.web3().toWei(content.querySelector('#gasprice').value, 'gwei') + continueTxExecution(gasPrice) + } + }}, { + label: 'Cancel', + fn: () => { + return cancelCb('Transaction canceled by user.') + } + }) + }, + + function (error, result) { + let eventName = (tx.useCall ? 'callExecuted' : 'transactionExecuted') + self.event.trigger(eventName, [error, tx.from, tx.to, tx.data, tx.useCall, result, timestamp, payLoad]) + + if (error && (typeof (error) !== 'string')) { + if (error.message) error = error.message + else { + try { error = 'error: ' + JSON.stringify(error) } catch (e) {} + } + } + next(error, result) } - next(error, result) - }) + ) } ], cb) }