diff --git a/lib/contracts/deploy.js b/lib/contracts/deploy.js index 4b07e2c2..beedfa82 100644 --- a/lib/contracts/deploy.js +++ b/lib/contracts/deploy.js @@ -80,7 +80,46 @@ class Deploy { let cmds = ""; cmds += codeGenerator.generateContractCode(contract); - cmds += contract.onDeploy.join(';\n'); + + let withErrors = false; + let regex = /\$\w+/g; + let onDeployCode = contract.onDeploy.map((cmd) => { + let realCmd = cmd.replace(regex, (match) => { + let referedContractName = match.slice(1); + let referedContract = self.contractsManager.getContract(referedContractName); + if (!referedContract) { + self.logger.error('error executing onDeploy for ' + contract.className); + self.logger.error(referedContractName + ' does not exist'); + self.logger.error("error running onDeploy: " + cmd); + withErrors = true; + return; + } + if (referedContract && referedContract.deploy === false) { + self.logger.error('error executing onDeploy for ' + contract.className); + self.logger.error(referedContractName + " exists but has been set to not deploy"); + self.logger.error("error running onDeploy: " + cmd); + withErrors = true; + return; + } + if (referedContract && !referedContract.deployedAddress) { + self.logger.error('error executing onDeploy for ' + contract.className); + self.logger.error("couldn't find a valid address for " + referedContractName + ". has it been deployed?"); + self.logger.error("error running onDeploy: " + cmd); + withErrors = true; + return; + } + return referedContract.deployedAddress; + }); + return realCmd; + }); + + if (withErrors) { + contract.error = "onDeployCmdError"; + return callback(new Error("error running onDeploy")); + } + + cmds += onDeployCode.join(';\n'); + RunCode.doEval(cmds, self.web3); } @@ -180,7 +219,7 @@ class Deploy { } self.logger.info("finished deploying contracts"); self.logger.trace(arguments); - done(); + done(err); } ); diff --git a/lib/contracts/deploy_manager.js b/lib/contracts/deploy_manager.js index 975abae0..08bc2075 100644 --- a/lib/contracts/deploy_manager.js +++ b/lib/contracts/deploy_manager.js @@ -71,8 +71,11 @@ class DeployManager { chainConfig: self.chainConfig, env: self.config.env }); - deploy.deployAll(function () { - self.events.emit('contractsDeployed', contractsManager); + deploy.deployAll(function (err) { + if (!err) { + self.events.emit('contractsDeployed', contractsManager); + } + //callback(err, contractsManager); callback(null, contractsManager); }); } diff --git a/test_app/config/contracts.json b/test_app/config/contracts.json index a8a3541d..03c65827 100644 --- a/test_app/config/contracts.json +++ b/test_app/config/contracts.json @@ -36,7 +36,7 @@ }, "Test": { "onDeploy": [ - "Test.changeAddress(web3.eth.accounts[0])" + "Test.changeAddress('$MyToken')" ] }, "MyToken": { diff --git a/test_app/test/token_spec.js b/test_app/test/token_spec.js index d24dbe58..a347447e 100644 --- a/test_app/test/token_spec.js +++ b/test_app/test/token_spec.js @@ -22,6 +22,11 @@ describe("Token", function() { "AlreadyDeployedToken": { "address": "0x123", instanceOf: "Token" + }, + "Test": { + onDeploy: [ + "Test.changeAddress('$MyToken', function(){})" + ] } }; EmbarkSpec.deployAll(contractsConfig, done); @@ -57,4 +62,11 @@ describe("Token", function() { done(); }); + it("should use onDeploy", function(done) { + Test.addr(function(err, result) { + assert.equal(result, MyToken.address) + done(); + }); + }); + });