From 1854bea93da873f93486fac1eeb6da1d7df6e743 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 8 Feb 2018 13:43:03 -0500 Subject: [PATCH] move execution txHelper to Remix-Lib --- remix-lib/index.js | 4 +- remix-lib/src/execution/txHelper.js | 122 ++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 remix-lib/src/execution/txHelper.js diff --git a/remix-lib/index.js b/remix-lib/index.js index ee025ab25b..86c3c6790f 100644 --- a/remix-lib/index.js +++ b/remix-lib/index.js @@ -19,6 +19,7 @@ var Storage = require('./src/storage') var EventsDecoder = require('./src/execution/eventsDecoder') var txExecution = require('./src/execution/txExecution') +var txHelper = require('./src/execution/txHelper') if (typeof (module) !== 'undefined' && typeof (module.exports) !== 'undefined') { module.exports = modules() @@ -55,7 +56,8 @@ function modules () { }, execution: { EventsDecoder: EventsDecoder, - txExecution: txExecution + txExecution: txExecution, + txHelper: txHelper } } } diff --git a/remix-lib/src/execution/txHelper.js b/remix-lib/src/execution/txHelper.js new file mode 100644 index 0000000000..ecc107f74a --- /dev/null +++ b/remix-lib/src/execution/txHelper.js @@ -0,0 +1,122 @@ +'use strict' +var ethJSABI = require('ethereumjs-abi') + +module.exports = { + encodeParams: function (funABI, args) { + var types = [] + if (funABI.inputs && funABI.inputs.length) { + for (var i = 0; i < funABI.inputs.length; i++) { + var type = funABI.inputs[i].type + types.push(type) + if (args.length < types.length) { + args.push('') + } + } + } + + // NOTE: the caller will concatenate the bytecode and this + // it could be done here too for consistency + return ethJSABI.rawEncode(types, args) + }, + + encodeFunctionId: function (funABI) { + var types = [] + if (funABI.inputs && funABI.inputs.length) { + for (var i = 0; i < funABI.inputs.length; i++) { + types.push(funABI.inputs[i].type) + } + } + + return ethJSABI.methodID(funABI.name, types) + }, + + sortAbiFunction: function (contractabi) { + var abi = contractabi.sort(function (a, b) { + if (a.name > b.name) { + return -1 + } else { + return 1 + } + }).sort(function (a, b) { + if (a.constant === true) { + return -1 + } else { + return 1 + } + }) + return abi + }, + + getConstructorInterface: function (abi) { + var funABI = { 'name': '', 'inputs': [], 'type': 'constructor', 'outputs': [] } + if (typeof abi === 'string') { + try { + abi = JSON.parse(abi) + } catch (e) { + console.log('exception retrieving ctor abi ' + abi) + return funABI + } + } + + for (var i = 0; i < abi.length; i++) { + if (abi[i].type === 'constructor') { + funABI.inputs = abi[i].inputs || [] + break + } + } + + return funABI + }, + + getFunction: function (abi, fnName) { + for (var i = 0; i < abi.length; i++) { + if (abi[i].name === fnName) { + return abi[i] + } + } + return null + }, + + getFallbackInterface: function (abi) { + for (var i = 0; i < abi.length; i++) { + if (abi[i].type === 'fallback') { + return abi[i] + } + } + }, + + /** + * return the contract obj of the given @arg name. Uses last compilation result. + * return null if not found + * @param {String} name - contract name + * @returns contract obj and associated file: { contract, file } or null + */ + getContract: (contractName, contracts) => { + for (var file in contracts) { + if (contracts[file][contractName]) { + return { object: contracts[file][contractName], file: file } + } + } + return null + }, + + /** + * call the given @arg cb (function) for all the contracts. Uses last compilation result + * stop visiting when cb return true + * @param {Function} cb - callback + */ + visitContracts: (contracts, cb) => { + for (var file in contracts) { + for (var name in contracts[file]) { + if (cb({ name: name, object: contracts[file][name], file: file })) return + } + } + }, + + inputParametersDeclarationToString: function (abiinputs) { + var inputs = (abiinputs || []).map((inp) => inp.type + ' ' + inp.name) + return inputs.join(', ') + } + +} +