diff --git a/lib/contracts/deploy_manager.js b/lib/contracts/deploy_manager.js index 54860326a..b6297489a 100644 --- a/lib/contracts/deploy_manager.js +++ b/lib/contracts/deploy_manager.js @@ -1,12 +1,14 @@ let async = require('async'); let Deploy = require('./deploy.js'); let ContractsManager = require('./contracts.js'); +let RunCode = require('../core/runCode.js'); class DeployManager { constructor(options) { this.config = options.config; this.logger = options.logger; this.blockchainConfig = this.config.blockchainConfig; + this.events = options.events; this.plugins = options.plugins; this.web3 = options.web3; @@ -76,9 +78,48 @@ class DeployManager { if (!err) { self.events.emit('contractsDeployed', contractsManager); } - //callback(err, contractsManager); - callback(null, contractsManager); + callback(null, contractsManager, web3); }); + }, + function runAfterDeployCommands(contractsManager, web3, callback) { + let afterDeployCmds = self.config.contractsConfig.afterDeploy || []; + + let withErrors = false; + let regex = /\$\w+/g; + let onDeployCode = afterDeployCmds.map((cmd) => { + let realCmd = cmd.replace(regex, (match) => { + let referedContractName = match.slice(1); + let referedContract = contractsManager.getContract(referedContractName); + if (!referedContract) { + self.logger.error(referedContractName + ' does not exist'); + self.logger.error("error running afterDeploy: " + cmd); + withErrors = true; + return; + } + if (referedContract && referedContract.deploy === false) { + self.logger.error(referedContractName + " exists but has been set to not deploy"); + self.logger.error("error running afterDeploy: " + cmd); + withErrors = true; + return; + } + if (referedContract && !referedContract.deployedAddress) { + self.logger.error("couldn't find a valid address for " + referedContractName + ". has it been deployed?"); + self.logger.error("error running afterDeploy: " + cmd); + withErrors = true; + return; + } + return referedContract.deployedAddress; + }); + return realCmd; + }); + + if (withErrors) { + return callback(new Error("error running afterDeploy")); + } + + RunCode.doEval(onDeployCode.join(';\n'), web3); + + callback(null, contractsManager); } ], function (err, result) { if (err) { diff --git a/test_app/config/contracts.json b/test_app/config/contracts.json index 03c65827c..c3aa648c2 100644 --- a/test_app/config/contracts.json +++ b/test_app/config/contracts.json @@ -53,7 +53,11 @@ "MyToken3": { "instanceOf": "Tokn" } - } + }, + "afterDeploy": [ + "Test.changeAddress('$MyToken')", + "Test.changeAddress(web3.eth.accounts[1])" + ] }, "development": { "contracts": {