From a9ce1343ab6263b634111a6ddb5a031896465aee Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 28 Aug 2020 10:31:01 -0400 Subject: [PATCH] simplify txRunner --- libs/remix-lib/src/execution/txRunner.js | 44 +++++++++++------------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/libs/remix-lib/src/execution/txRunner.js b/libs/remix-lib/src/execution/txRunner.js index 2c14cdb308..2ad6fb737e 100644 --- a/libs/remix-lib/src/execution/txRunner.js +++ b/libs/remix-lib/src/execution/txRunner.js @@ -77,20 +77,19 @@ class TxRunner { } } - execute (args, confirmationCb, gasEstimationForceSend, promptCb, callback) { + execute(args, confirmationCb, gasEstimationForceSend, promptCb, callback) { let data = args.data if (data.slice(0, 2) !== '0x') { data = '0x' + data } if (!this.executionContext.isVM()) { - this.runInNode(args.from, args.to, data, args.value, args.gasLimit, args.useCall, confirmationCb, gasEstimationForceSend, promptCb, callback) - } else { - try { - this.runInVm(args.from, args.to, data, args.value, args.gasLimit, args.useCall, args.timestamp, callback) - } catch (e) { - callback(e, null) - } + return this.runInNode(args.from, args.to, data, args.value, args.gasLimit, args.useCall, confirmationCb, gasEstimationForceSend, promptCb, callback) + } + try { + this.runInVm(args.from, args.to, data, args.value, args.gasLimit, args.useCall, args.timestamp, callback) + } catch (e) { + callback(e, null) } } @@ -228,9 +227,8 @@ async function tryTillReceiptAvailable (txhash, executionContext) { // Try again with a bit of delay if error or if result still null await pause() return resolve(await tryTillReceiptAvailable(txhash, executionContext)) - } else { - return resolve(receipt) } + return resolve(receipt) }) }) } @@ -242,29 +240,27 @@ async function tryTillTxAvailable (txhash, executionContext) { // Try again with a bit of delay if error or if result still null await pause() return resolve(await tryTillTxAvailable(txhash, executionContext)) - } else { - return resolve(tx) } + return resolve(tx) }) }) } async function pause () { return new Promise((resolve, reject) => { setTimeout(resolve, 500) }) } -function run (self, tx, stamp, confirmationCb, gasEstimationForceSend, promptCb, callback) { +function run(self, tx, stamp, confirmationCb, gasEstimationForceSend, promptCb, callback) { if (!self.runAsync && Object.keys(self.pendingTxs).length) { - self.queusTxs.push({ tx, stamp, callback }) - } else { - self.pendingTxs[stamp] = tx - self.execute(tx, confirmationCb, gasEstimationForceSend, promptCb, function(error, result) { - delete self.pendingTxs[stamp] - if (callback && typeof callback === 'function') callback(error, result) - if (self.queusTxs.length) { - const next = self.queusTxs.pop() - run(self, next.tx, next.stamp, next.callback) - } - }) + return self.queusTxs.push({ tx, stamp, callback }) } + self.pendingTxs[stamp] = tx + self.execute(tx, confirmationCb, gasEstimationForceSend, promptCb, function (error, result) { + delete self.pendingTxs[stamp] + if (callback && typeof callback === 'function') callback(error, result) + if (self.queusTxs.length) { + const next = self.queusTxs.pop() + run(self, next.tx, next.stamp, next.callback) + } + }) } module.exports = TxRunner