fix 337; support contract references in array arguments

This commit is contained in:
Iuri Matias 2018-03-04 18:46:12 -05:00
parent da7e5747f9
commit ac79cef97b
9 changed files with 103 additions and 3 deletions

View File

@ -146,6 +146,8 @@ class ContractsManager {
self.logger.trace(self.contracts);
callback();
},
/*eslint complexity: ["error", 16]*/
/*eslint max-depth: ["error", 16]*/
function determineDependencies(callback) {
let className, contract;
for (className in self.contracts) {
@ -167,6 +169,14 @@ class ContractsManager {
self.contractDependencies[className] = self.contractDependencies[className] || [];
self.contractDependencies[className].push(arg.substr(1));
}
if (Array.isArray(arg)) {
for (let sub_arg of arg) {
if (sub_arg[0] === "$") {
self.contractDependencies[className] = self.contractDependencies[className] || [];
self.contractDependencies[className].push(sub_arg.substr(1));
}
}
}
}
// look in onDeploy for dependencies

View File

@ -33,6 +33,18 @@ class Deploy {
contractName = arg.substr(1);
referedContract = this.contractsManager.getContract(contractName);
realArgs.push(referedContract.deployedAddress);
} else if (Array.isArray(arg)) {
let subRealArgs = [];
for (let sub_arg of arg) {
if (sub_arg[0] === "$") {
contractName = sub_arg.substr(1);
referedContract = this.contractsManager.getContract(contractName);
subRealArgs.push(referedContract.deployedAddress);
} else {
subRealArgs.push(sub_arg);
}
}
realArgs.push(subRealArgs);
} else {
realArgs.push(arg);
}

View File

@ -5,9 +5,9 @@ contract SomeContract {
function() public payable { }
function SomeContract(address _addresses, uint initialValue) public {
//addr_1 = _addresses[0];
//addr_2 = _addresses[1];
function SomeContract(address[] _addresses, uint initialValue) public {
addr_1 = _addresses[0];
addr_2 = _addresses[1];
value = initialValue;
}

View File

@ -54,6 +54,12 @@
},
"MyToken3": {
"instanceOf": "Tokn"
},
"SomeContract": {
"args": [
["$MyToken2", "$SimpleStorage"],
100
]
}
},
"afterDeploy": [

View File

@ -19,6 +19,12 @@ contract("AnotherStorage", function() {
"MyToken2": {
instanceOf: "Token",
args: [2000]
},
"SomeContract": {
"args": [
["$MyToken2", "$SimpleStorage"],
100
]
}
};
EmbarkSpec.deployAll(contractsConfig, () => { done() });

View File

@ -0,0 +1,48 @@
contract("SomeContract", function() {
this.timeout(0);
before(function(done) {
this.timeout(0);
var contractsConfig = {
"SimpleStorage": {
args: [100]
},
"AnotherStorage": {
args: ["$SimpleStorage"]
},
"Token": {
deploy: false,
args: [1000]
},
"MyToken": {
instanceOf: "Token"
},
"MyToken2": {
instanceOf: "Token",
args: [2000]
},
"SomeContract": {
"args": [
["$MyToken2", "$SimpleStorage"],
100
]
}
};
EmbarkSpec.deployAll(contractsConfig, () => { done() });
});
it("set MyToken2 address", function(done) {
SomeContract.methods.addr_1().call().then(function(address) {
assert.equal(address, MyToken2.options.address);
done();
});
});
it("set SimpleStorage address", function(done) {
SomeContract.methods.addr_2().call().then(function(address) {
assert.equal(address, SimpleStorage.options.address);
done();
});
});
});

View File

@ -24,6 +24,12 @@ contract("Test", function() {
"MyToken2": {
instanceOf: "Token",
args: [2000]
},
"SomeContract": {
"args": [
["$MyToken2", "$SimpleStorage"],
100
]
}
};

View File

@ -26,6 +26,12 @@ contract("SimpleStorage", function() {
"MyToken2": {
instanceOf: "Token",
args: [2000]
},
"SomeContract": {
"args": [
["$MyToken2", "$SimpleStorage"],
100
]
}
};
EmbarkSpec.deployAll(contractsConfig, () => { done() });

View File

@ -35,6 +35,12 @@ describe("Token", function() {
onDeploy: [
"Test.methods.changeAddress('$MyToken').send()"
]
},
"SomeContract": {
"args": [
["$MyToken2", "$SimpleStorage"],
100
]
}
};
EmbarkSpec.deployAll(contractsConfig, () => { done() });