diff --git a/package.json b/package.json index 495fe1be54..7ea6057e13 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "test": "node test/index.js" }, "devDependencies": { + "async": "^2.1.2", "babel-cli": "^6.16.0", "babel-plugin-transform-es2015-block-scoping": "^6.15.0", "babel-plugin-transform-es2015-template-literals": "^6.8.0", diff --git a/src/app/renderer.js b/src/app/renderer.js index fefd292665..2e81d1cfa2 100644 --- a/src/app/renderer.js +++ b/src/app/renderer.js @@ -113,14 +113,22 @@ Renderer.prototype.contracts = function (data, source) { return source.sources[currentFile] } - var getAddress = function () { return $('#txorigin').val() } + var getAddress = function (cb) { + cb(null, $('#txorigin').val()) + } - var getValue = function () { - var comp = $('#value').val().split(' ') - return self.executionContext.web3().toWei(comp[0], comp.slice(1).join(' ')) + var getValue = function (cb) { + try { + var comp = $('#value').val().split(' ') + cb(null, self.executionContext.web3().toWei(comp[0], comp.slice(1).join(' '))) + } catch (e) { + cb(e) + } } - var getGasLimit = function () { return $('#gasLimit').val() } + var getGasLimit = function (cb) { + cb(null, $('#gasLimit').val()) + } this.udapp.reset(udappContracts, getAddress, getValue, getGasLimit, renderOutputModifier) diff --git a/src/universal-dapp.js b/src/universal-dapp.js index cc52c9c038..2a08cff7e9 100644 --- a/src/universal-dapp.js +++ b/src/universal-dapp.js @@ -9,6 +9,7 @@ var EthJSBlock = require('ethereumjs-block') var BN = ethJSUtil.BN var EventManager = require('./lib/eventManager') var crypto = require('crypto') +var async = require('async') /* trigger debugRequested @@ -679,34 +680,94 @@ function tryTillResponse (web3, txhash, done) { UniversalDApp.prototype.runTx = function (args, cb) { var self = this - var to = args.to - var data = args.data - if (data.slice(0, 2) !== '0x') { - data = '0x' + data + var tx = { + to: args.to, + data: args.data } - var gasLimit = 3000000 - if (self.getGasLimit) { - try { - gasLimit = self.getGasLimit() - } catch (e) { - return cb(e) - } - } + async.waterfall([ + // query gas limit + function (callback) { + tx.gasLimit = 3000000 - var value = 0 - if (self.getValue) { - try { - value = self.getValue() - } catch (e) { - return cb(e) + if (self.getGasLimit) { + self.getGasLimit(function (err, ret) { + if (err) { + return callback(err) + } + + tx.gasLimit = ret + callback() + }) + } else { + callback() + } + }, + // query value + function (callback) { + tx.value = 0 + + if (self.getValue) { + self.getValue(function (err, ret) { + if (err) { + return callback(err) + } + + tx.value = ret + callback() + }) + } else { + callback() + } + }, + // query address + function (callback) { + if (self.getAddress) { + self.getAddress(function (err, ret) { + if (err) { + return callback(err) + } + + tx.from = ret + callback() + }) + } else { + self.getAccounts(function (err, ret) { + if (err) { + return callback(err) + } + + if (ret.length === 0) { + return callback('No accounts available') + } + + tx.from = ret[0] + callback() + }) + } + }, + // run transaction + function (callback) { + self.rawRunTx(tx, callback) } + ], cb) +} + +UniversalDApp.prototype.rawRunTx = function (args, cb) { + var self = this + var from = args.from + var to = args.to + var data = args.data + if (data.slice(0, 2) !== '0x') { + data = '0x' + data } + var value = args.value + var gasLimit = args.gasLimit var tx if (!self.executionContext.isVM()) { tx = { - from: self.getAddress ? self.getAddress() : self.web3.eth.accounts[0], + from: from, to: to, data: data, value: value @@ -739,8 +800,10 @@ UniversalDApp.prototype.runTx = function (args, cb) { } } else { try { - var address = self.getAddress ? self.getAddress() : Object.keys(self.accounts)[0] - var account = self.accounts[address] + var account = self.accounts[from] + if (!account) { + return cb('Invalid account selected') + } tx = new EthJSTX({ nonce: new BN(account.nonce++), gasPrice: new BN(1),