Merge pull request #1041 from ethereum/refactor/udapp_refactor_waterfall

refactor udapp runTx and related functions
pull/1/head
yann300 7 years ago committed by GitHub
commit 168d44ca35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 142
      src/universal-dapp.js

@ -1,6 +1,7 @@
/* global */ /* global */
'use strict' 'use strict'
var async = require('async')
var ethJSUtil = require('ethereumjs-util') var ethJSUtil = require('ethereumjs-util')
var BN = ethJSUtil.BN var BN = ethJSUtil.BN
var remixLib = require('remix-lib') var remixLib = require('remix-lib')
@ -201,99 +202,66 @@ UniversalDApp.prototype.getInputs = function (funABI) {
return txHelper.inputParametersDeclarationToString(funABI.inputs) return txHelper.inputParametersDeclarationToString(funABI.inputs)
} }
function execute (pipeline, env, callback) {
function next (err, env) {
if (err) return callback(err)
var step = pipeline.shift()
if (step) step(env, next)
else callback(null, env.result)
}
next(null, env)
}
UniversalDApp.prototype.runTx = function (args, cb) { UniversalDApp.prototype.runTx = function (args, cb) {
var self = this const self = this
var tx = { to: args.to, data: args.data.dataHex, useCall: args.useCall, from: args.from, value: args.value } async.waterfall([
var payLoad = { funAbi: args.data.funAbi, funArgs: args.data.funArgs, contractBytecode: args.data.contractBytecode, contractName: args.data.contractName } // contains decoded parameters function getGasLimit (next) {
var pipeline = [queryGasLimit] if (self.transactionContextAPI.getGasLimit) {
if (!args.value) { return self.transactionContextAPI.getGasLimit(next)
pipeline.push(queryValue) }
} next(null, 3000000)
if (!args.from) { },
pipeline.push(queryAddress) function queryValue (gasLimit, next) {
} if (args.value) {
pipeline.push(runTransaction) return next(null, args.value, gasLimit)
var env = { self, tx, payLoad } }
execute(pipeline, env, cb) if (args.useCall || !self.transactionContextAPI.getValue) {
} return next(null, 0, gasLimit)
}
function queryGasLimit (env, next) { self.transactionContextAPI.getValue(function (err, value) {
var { self, tx } = env next(err, value, gasLimit)
tx.gasLimit = 3000000 })
if (self.transactionContextAPI.getGasLimit) { },
self.transactionContextAPI.getGasLimit(function (err, ret) { function getAccount (value, gasLimit, next) {
if (err) return next(err) if (args.from) {
tx.gasLimit = ret return next(null, args.from, value, gasLimit)
next(null, env) }
}) if (self.transactionContextAPI.getAddress) {
} else next(null, env) return self.transactionContextAPI.getAddress(function (err, address) {
} next(err, address, value, gasLimit)
})
}
self.getAccounts(function (err, accounts) {
let address = accounts[0]
function queryValue (env, next) { if (err) return next(err)
var { self, tx } = env if (!address) return next('No accounts available')
tx.value = 0 if (executionContext.isVM() && !self.accounts[address]) {
if (tx.useCall) return next(null, env) return next('Invalid account selected')
if (self.transactionContextAPI.getValue) { }
self.transactionContextAPI.getValue(function (err, ret) { next(null, address, value, gasLimit)
if (err) return next(err) })
tx.value = ret },
next(null, env) function runTransaction (fromAddress, value, gasLimit, next) {
}) var tx = { to: args.to, data: args.data.dataHex, useCall: args.useCall, from: fromAddress, value: value, gasLimit: gasLimit }
} else next(null, env) var payLoad = { funAbi: args.data.funAbi, funArgs: args.data.funArgs, contractBytecode: args.data.contractBytecode, contractName: args.data.contractName }
} var timestamp = Date.now()
function queryAddress (env, next) { self.event.trigger('initiatingTransaction', [timestamp, tx, payLoad])
var { self, tx } = env self.txRunner.rawRun(tx, function (error, result) {
if (self.transactionContextAPI.getAddress) { let eventName = (tx.useCall ? 'callExecuted' : 'transactionExecuted')
self.transactionContextAPI.getAddress(function (err, ret) { self.event.trigger(eventName, [error, tx.from, tx.to, tx.data, tx.useCall, result, timestamp, payLoad])
if (err) return next(err)
tx.from = ret
next(null, env)
})
} else {
self.getAccounts(function (err, ret) {
if (err) return next(err)
if (ret.length === 0) return next('No accounts available')
if (executionContext.isVM() && !self.accounts[ret[0]]) {
return next('Invalid account selected')
}
tx.from = ret[0]
next(null, env)
})
}
}
function runTransaction (env, next) { if (error && (typeof (error) !== 'string')) {
var { self, tx, payLoad } = env if (error.message) error = error.message
var timestamp = Date.now() else {
self.event.trigger('initiatingTransaction', [timestamp, tx, payLoad]) try { error = 'error: ' + JSON.stringify(error) } catch (e) {}
self.txRunner.rawRun(tx, function (error, result) { }
if (!tx.useCall) {
self.event.trigger('transactionExecuted', [error, tx.from, tx.to, tx.data, false, result, timestamp, payLoad])
} else {
self.event.trigger('callExecuted', [error, tx.from, tx.to, tx.data, true, result, timestamp, payLoad])
}
if (error) {
if (typeof (error) !== 'string') {
if (error.message) error = error.message
else {
try { error = 'error: ' + JSON.stringify(error) } catch (e) {}
} }
} next(error, result)
})
} }
env.result = result ], cb)
next(error, env)
})
} }
module.exports = UniversalDApp module.exports = UniversalDApp

Loading…
Cancel
Save