From 05aadc5c10866b46517ac1046ce0b3b32934e1a9 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 4 Mar 2018 20:07:39 -0500 Subject: [PATCH] support specifying contract args as an object --- lib/contracts/contracts.js | 11 ++++++++++- lib/contracts/deploy.js | 25 +++++++++++++++++++----- test_app/app/contracts/contract_args.sol | 14 +++++++++++++ test_app/config/contracts.json | 6 ++++++ 4 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 test_app/app/contracts/contract_args.sol diff --git a/lib/contracts/contracts.js b/lib/contracts/contracts.js index 13efc8948..14fc8f35c 100644 --- a/lib/contracts/contracts.js +++ b/lib/contracts/contracts.js @@ -146,6 +146,7 @@ class ContractsManager { self.logger.trace(self.contracts); callback(); }, + // TODO: needs refactoring, has gotten too complex /*eslint complexity: ["error", 16]*/ /*eslint max-depth: ["error", 16]*/ function determineDependencies(callback) { @@ -162,7 +163,15 @@ class ContractsManager { // look in arguments for dependencies if (contract.args === []) continue; - let ref = contract.args; + + let ref; + if (Array.isArray(contract.args)) { + ref = contract.args; + } else { + let keys = Object.keys(contract.args); + ref = keys.map((k) => contract.args[k]).filter((x) => !x); + } + for (let j = 0; j < ref.length; j++) { let arg = ref[j]; if (arg[0] === "$") { diff --git a/lib/contracts/deploy.js b/lib/contracts/deploy.js index ad8a0215b..d6f6636e2 100644 --- a/lib/contracts/deploy.js +++ b/lib/contracts/deploy.js @@ -24,11 +24,26 @@ class Deploy { }, cb); } - determineArguments(suppliedArgs) { + determineArguments(suppliedArgs, contract) { let realArgs = [], l, arg, contractName, referedContract; - for (l = 0; l < suppliedArgs.length; l++) { - arg = suppliedArgs[l]; + let args = suppliedArgs; + + if (!Array.isArray(args)) { + args = []; + let abi = contract.abiDefinition.find((abi) => abi.type === 'constructor'); + + for (let input of abi.inputs) { + let inputValue = suppliedArgs[input.name]; + if (!inputValue) { + this.logger.error(input.name + " has not been defined for " + contract.className + " constructor"); + } + args.push(inputValue || ""); + } + } + + for (l = 0; l < args.length; l++) { + arg = args[l]; if (arg[0] === "$") { contractName = arg.substr(1); referedContract = this.contractsManager.getContract(contractName); @@ -63,7 +78,7 @@ class Deploy { return callback(); } - realArgs = self.determineArguments(params || contract.args); + realArgs = self.determineArguments(params || contract.args, contract); if (contract.address !== undefined) { try { @@ -119,7 +134,7 @@ class Deploy { contractToDeploy(contract, params, callback) { const self = this; - let realArgs = self.determineArguments(params || contract.args); + let realArgs = self.determineArguments(params || contract.args, contract); this.deployContract(contract, realArgs, function (err, address) { if (err) { diff --git a/test_app/app/contracts/contract_args.sol b/test_app/app/contracts/contract_args.sol new file mode 100644 index 000000000..1bf7a2d37 --- /dev/null +++ b/test_app/app/contracts/contract_args.sol @@ -0,0 +1,14 @@ +contract ContractArgs { + address public addr_1; + address public addr_2; + uint public value; + + function() public payable { } + + function ContractArgs(address[] _addresses, uint initialValue) public { + addr_1 = _addresses[0]; + addr_2 = _addresses[1]; + value = initialValue; + } + +} diff --git a/test_app/config/contracts.json b/test_app/config/contracts.json index 633a227ec..c15d84b00 100644 --- a/test_app/config/contracts.json +++ b/test_app/config/contracts.json @@ -55,6 +55,12 @@ "MyToken3": { "instanceOf": "Tokn" }, + "ContractArgs": { + "args": { + "initialValue": 123, + "_addresses": ["$MyToken2", "$SimpleStorage"] + } + }, "SomeContract": { "args": [ ["$MyToken2", "$SimpleStorage"],