diff --git a/lib/contracts/contracts.js b/lib/contracts/contracts.js index d55ae0f99..6f9fbd815 100644 --- a/lib/contracts/contracts.js +++ b/lib/contracts/contracts.js @@ -8,6 +8,7 @@ const constants = require('../constants'); class ContractsManager { constructor(options) { + const self = this; this.contractFiles = options.contractFiles; this.contractsConfig = options.contractsConfig; this.contracts = {}; @@ -18,14 +19,13 @@ class ContractsManager { this.deployOnlyOnConfig = false; this.events = options.events; - this.events.on(constants.events.contractFilesChanged, (newContractFiles) => { - this.contractFiles = newContractFiles; + self.events.on(constants.events.contractFilesChanged, (newContractFiles) => { + self.contractFiles = newContractFiles; }); - this.events.on(constants.events.contractConfigChanged, (newContracts) => { - this.contractsConfig = newContracts; + self.events.on(constants.events.contractConfigChanged, (newContracts) => { + self.contractsConfig = newContracts; }); - const self = this; self.events.setCommandHandler('contracts:list', (cb) => { cb(self.listContracts()); }); diff --git a/lib/contracts/deploy.js b/lib/contracts/deploy.js index ae86d9ac5..37d00ae00 100644 --- a/lib/contracts/deploy.js +++ b/lib/contracts/deploy.js @@ -181,31 +181,31 @@ class Deploy { }); }, function doLinking(next) { - // Applying linked contracts - let contractsList = self.contractsManager.listContracts(); - for (let contractObj of contractsList) { - let filename = contractObj.filename; - let deployedAddress = contractObj.deployedAddress; - if (deployedAddress) { - deployedAddress = deployedAddress.substr(2); + self.events.request('contracts:list', (contracts) => { + for (let contractObj of contracts) { + let filename = contractObj.filename; + let deployedAddress = contractObj.deployedAddress; + if (deployedAddress) { + deployedAddress = deployedAddress.substr(2); + } + let linkReference = '__' + filename + ":" + contractObj.className; + if (contractCode.indexOf(linkReference) < 0) { + continue; + } + if (linkReference.length > 40) { + return next(new Error(__("{{linkReference}} is too long, try reducing the path of the contract ({{filename}}) and/or its name {{contractName}}", {linkReference: linkReference, filename: filename, contractName: contractObj.className}))); + } + let toReplace = linkReference + "_".repeat(40 - linkReference.length); + if (deployedAddress === undefined) { + let libraryName = contractObj.className; + return next(new Error(__("{{contractName}} needs {{libraryName}} but an address was not found, did you deploy it or configured an address?", {contractName: contract.className, libraryName: libraryName}))); + } + contractCode = contractCode.replace(new RegExp(toReplace, "g"), deployedAddress); } - let linkReference = '__' + filename + ":" + contractObj.className; - if (contractCode.indexOf(linkReference) < 0) { - continue; - } - if (linkReference.length > 40) { - return next(new Error(__("{{linkReference}} is too long, try reducing the path of the contract ({{filename}}) and/or its name {{contractName}}", {linkReference: linkReference, filename: filename, contractName: contractObj.className}))); - } - let toReplace = linkReference + "_".repeat(40 - linkReference.length); - if (deployedAddress === undefined) { - let libraryName = contractObj.className; - return next(new Error(__("{{contractName}} needs {{libraryName}} but an address was not found, did you deploy it or configured an address?", {contractName: contract.className, libraryName: libraryName}))); - } - contractCode = contractCode.replace(new RegExp(toReplace, "g"), deployedAddress); - } - // saving code changes back to contract object - contract.code = contractCode; - next(); + // saving code changes back to contract object + contract.code = contractCode; + next(); + }); }, function applyBeforeDeploy(next) { let beforeDeployPlugins = self.plugins.getPluginsFor('beforeDeploy'); @@ -284,29 +284,30 @@ class Deploy { deployAll(done) { let self = this; this.logger.info(__("deploying contracts")); - let contracts = this.contractsManager.listContracts(); this.events.emit("deploy:beforeAll"); - async.eachOfSeries(contracts, - function (contract, key, callback) { - self.logger.trace(arguments); - self.checkAndDeployContract(contract, null, callback); - }, - function (err, _results) { - if (err) { - self.logger.error(__("error deploying contracts")); - self.logger.error(err.message); - self.logger.debug(err.stack); + self.events.request('contracts:list', (contracts) => { + async.eachOfSeries(contracts, + function (contract, key, callback) { + self.logger.trace(arguments); + self.checkAndDeployContract(contract, null, callback); + }, + function (err, _results) { + if (err) { + self.logger.error(__("error deploying contracts")); + self.logger.error(err.message); + self.logger.debug(err.stack); + } + if (contracts.length === 0) { + self.logger.info(__("no contracts found")); + return done(); + } + self.logger.info(__("finished deploying contracts")); + self.logger.trace(arguments); + done(err); } - if (contracts.length === 0) { - self.logger.info(__("no contracts found")); - return done(); - } - self.logger.info(__("finished deploying contracts")); - self.logger.trace(arguments); - done(err); - } - ); + ); + }); } }