support specifying contract args as an object

This commit is contained in:
Iuri Matias 2018-03-04 20:07:39 -05:00
parent a03be15c4a
commit 05aadc5c10
4 changed files with 50 additions and 6 deletions

View File

@ -146,6 +146,7 @@ class ContractsManager {
self.logger.trace(self.contracts); self.logger.trace(self.contracts);
callback(); callback();
}, },
// TODO: needs refactoring, has gotten too complex
/*eslint complexity: ["error", 16]*/ /*eslint complexity: ["error", 16]*/
/*eslint max-depth: ["error", 16]*/ /*eslint max-depth: ["error", 16]*/
function determineDependencies(callback) { function determineDependencies(callback) {
@ -162,7 +163,15 @@ class ContractsManager {
// look in arguments for dependencies // look in arguments for dependencies
if (contract.args === []) continue; 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++) { for (let j = 0; j < ref.length; j++) {
let arg = ref[j]; let arg = ref[j];
if (arg[0] === "$") { if (arg[0] === "$") {

View File

@ -24,11 +24,26 @@ class Deploy {
}, cb); }, cb);
} }
determineArguments(suppliedArgs) { determineArguments(suppliedArgs, contract) {
let realArgs = [], l, arg, contractName, referedContract; let realArgs = [], l, arg, contractName, referedContract;
for (l = 0; l < suppliedArgs.length; l++) { let args = suppliedArgs;
arg = suppliedArgs[l];
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] === "$") { if (arg[0] === "$") {
contractName = arg.substr(1); contractName = arg.substr(1);
referedContract = this.contractsManager.getContract(contractName); referedContract = this.contractsManager.getContract(contractName);
@ -63,7 +78,7 @@ class Deploy {
return callback(); return callback();
} }
realArgs = self.determineArguments(params || contract.args); realArgs = self.determineArguments(params || contract.args, contract);
if (contract.address !== undefined) { if (contract.address !== undefined) {
try { try {
@ -119,7 +134,7 @@ class Deploy {
contractToDeploy(contract, params, callback) { contractToDeploy(contract, params, callback) {
const self = this; 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) { this.deployContract(contract, realArgs, function (err, address) {
if (err) { if (err) {

View File

@ -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;
}
}

View File

@ -55,6 +55,12 @@
"MyToken3": { "MyToken3": {
"instanceOf": "Tokn" "instanceOf": "Tokn"
}, },
"ContractArgs": {
"args": {
"initialValue": 123,
"_addresses": ["$MyToken2", "$SimpleStorage"]
}
},
"SomeContract": { "SomeContract": {
"args": [ "args": [
["$MyToken2", "$SimpleStorage"], ["$MyToken2", "$SimpleStorage"],