From 1f246a9528b5f26b1234ce5544da5b4ed022f018 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 21 Feb 2018 19:20:04 -0500 Subject: [PATCH 1/6] move typeConversion into execution dir --- src/app/execution/txLogger.js | 2 +- src/app/execution/txRunner.js | 2 +- src/{lib => app/execution}/typeConversion.js | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename src/{lib => app/execution}/typeConversion.js (100%) diff --git a/src/app/execution/txLogger.js b/src/app/execution/txLogger.js index 6e782a7acc..f39bfb39a4 100644 --- a/src/app/execution/txLogger.js +++ b/src/app/execution/txLogger.js @@ -12,7 +12,7 @@ var EventManager = remixLib.EventManager var helper = require('../../lib/helper') var executionContext = require('../../execution-context') var modalDialog = require('../ui/modal-dialog-custom') -var typeConversion = require('../../lib/typeConversion') +var typeConversion = require('./typeConversion') var css = csjs` .log { diff --git a/src/app/execution/txRunner.js b/src/app/execution/txRunner.js index e109881860..4293d2e669 100644 --- a/src/app/execution/txRunner.js +++ b/src/app/execution/txRunner.js @@ -6,7 +6,7 @@ var BN = ethJSUtil.BN var executionContext = require('../../execution-context') var modalDialog = require('../ui/modaldialog') var modal = require('../ui/modal-dialog-custom') -var typeConversion = require('../../lib/typeConversion') +var typeConversion = require('./typeConversion') var confirmDialog = require('./confirmDialog') diff --git a/src/lib/typeConversion.js b/src/app/execution/typeConversion.js similarity index 100% rename from src/lib/typeConversion.js rename to src/app/execution/typeConversion.js From f708c1232f959abc854e5b6b72554df088da8b17 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 21 Feb 2018 19:52:06 -0500 Subject: [PATCH 2/6] 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) } From c079bae125cdf0de63a232193fd0406192d59807 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 21 Feb 2018 19:52:06 -0500 Subject: [PATCH 3/6] 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) } From 829189fc56f488a89ebd12f91f59a12884dd55c4 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 22 Feb 2018 16:58:04 -0500 Subject: [PATCH 4/6] fix confirmCb params --- src/universal-dapp.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/universal-dapp.js b/src/universal-dapp.js index 3d75113e0c..ac3006f0c4 100644 --- a/src/universal-dapp.js +++ b/src/universal-dapp.js @@ -254,7 +254,7 @@ UniversalDApp.prototype.runTx = function (args, cb) { self.event.trigger('initiatingTransaction', [timestamp, tx, payLoad]) self.txRunner.rawRun(tx, - (network, tx, continueTxExecution, gasEstimation, cancelCb) => { + (network, tx, gasEstimation, continueTxExecution, cancelCb) => { if (network.name !== 'Main') { return continueTxExecution(null) } From 25da239ab5bc4681073efa0c62d49a7e3db1ac79 Mon Sep 17 00:00:00 2001 From: yann300 Date: Mon, 26 Feb 2018 10:20:06 +0100 Subject: [PATCH 5/6] fix confirmCbCallback --- src/app/execution/txRunner.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/app/execution/txRunner.js b/src/app/execution/txRunner.js index 9263de3cd8..0975dfc8c4 100644 --- a/src/app/execution/txRunner.js +++ b/src/app/execution/txRunner.js @@ -145,8 +145,10 @@ TxRunner.prototype.runInNode = function (from, to, data, value, gasLimit, useCal return } - confirmCb(network, tx, gasEstimation, callback, (gasPrice) => { + confirmCb(network, tx, gasEstimation, (gasPrice) => { return executeTx(tx, gasPrice, self._api, callback) + }, (error) => { + callback(error) }) }) }) From 49e1ea12e2c35074c05893bb508b655b188c61f7 Mon Sep 17 00:00:00 2001 From: yann300 Date: Mon, 26 Feb 2018 10:26:57 +0100 Subject: [PATCH 6/6] fix callback param --- src/app/execution/confirmDialog.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/execution/confirmDialog.js b/src/app/execution/confirmDialog.js index 60654b8092..1481018a7d 100644 --- a/src/app/execution/confirmDialog.js +++ b/src/app/execution/confirmDialog.js @@ -20,7 +20,7 @@ var css = csjs` function confirmDialog (tx, amount, gasEstimation, self, newGasPriceCb, initialParamsCb) { var onGasPriceChange = function () { var gasPrice = el.querySelector('#gasprice').value - newGasPriceCb(gasPrice, (priceStatus, txFeeText) => { + newGasPriceCb(gasPrice, (txFeeText, priceStatus) => { el.querySelector('#txfee').innerHTML = txFeeText el.gasPriceStatus = priceStatus })