diff --git a/lib/contracts/deploy.js b/lib/contracts/deploy.js index a8ee34627..03b66a845 100644 --- a/lib/contracts/deploy.js +++ b/lib/contracts/deploy.js @@ -4,7 +4,6 @@ let utils = require('../utils/utils.js'); let RunCode = require('../core/runCode.js'); -let DeployTracker = require('./deploy_tracker.js'); let CodeGenerator = require('./code_generator.js'); class Deploy { @@ -24,12 +23,6 @@ class Deploy { }); } - initTracker(cb) { - this.deployTracker = new DeployTracker({ - logger: this.logger, chainConfig: this.chainConfig, env: this.env, events: this.events - }, cb); - } - determineArguments(suppliedArgs, contract) { let realArgs = [], l, arg, contractName, referedContract; @@ -76,7 +69,6 @@ class Deploy { checkAndDeployContract(contract, params, callback) { let self = this; - let realArgs; contract.error = false; if (contract.deploy === false) { @@ -84,7 +76,7 @@ class Deploy { return callback(); } - realArgs = self.determineArguments(params || contract.args, contract); + contract.realArgs = self.determineArguments(params || contract.args, contract); if (contract.address !== undefined) { try { @@ -98,29 +90,28 @@ class Deploy { } contract.deployedAddress = contract.address; self.logger.info(contract.className.bold.cyan + __(" already deployed at ").green + contract.address.bold.cyan); - if (this.deployTracker) { - self.deployTracker.trackContract(contract.className, contract.realRuntimeBytecode, realArgs, contract.address); - self.deployTracker.save(); - } + self.events.emit("deploy:contract:deployed", contract); self.events.emit('contractsState', self.contractsManager.contractsState()); return callback(); } - if (!this.deployTracker) { - return self.contractToDeploy(contract, params, callback); - } + //if (!this.deployTracker) { + // return self.contractToDeploy(contract, params, callback); + //} - let trackedContract = self.deployTracker.getContract(contract.className, contract.realRuntimeBytecode, realArgs); - if (!trackedContract) { + // TODO: this should be a plugin API instead, if not existing, it should by default deploy the contract + self.events.request("deploy:contract:shouldDeploy", contract, function(trackedContract) { + if (!trackedContract) { return self.contractToDeploy(contract, params, callback); - } - - this.blockchain.getCode(trackedContract.address, function(_getCodeErr, codeInChain) { - if (codeInChain !== "0x") { - self.contractAlreadyDeployed(contract, trackedContract, callback); - } else { - self.contractToDeploy(contract, params, callback); } + + self.blockchain.getCode(trackedContract.address, function(_getCodeErr, codeInChain) { + if (codeInChain !== "0x") { + self.contractAlreadyDeployed(contract, trackedContract, callback); + } else { + self.contractToDeploy(contract, params, callback); + } + }); }); } @@ -140,14 +131,13 @@ class Deploy { contractToDeploy(contract, params, callback) { const self = this; - let realArgs = self.determineArguments(params || contract.args, contract); + contract.realArgs = self.determineArguments(params || contract.args, contract); - this.deployContract(contract, realArgs, function (err, address) { + this.deployContract(contract, contract.realArgs, function (err, address) { if (err) { return callback(new Error(err)); } - self.deployTracker.trackContract(contract.className, contract.realRuntimeBytecode, realArgs, address); - self.deployTracker.save(); + self.events.emit("deploy:contract:deployed", contract); self.events.emit('contractsState', self.contractsManager.contractsState()); // always run contractCode so other functionality like 'afterDeploy' can also work @@ -357,6 +347,7 @@ class Deploy { 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) { diff --git a/lib/contracts/deploy_manager.js b/lib/contracts/deploy_manager.js index 0bf5f9efc..3418fb266 100644 --- a/lib/contracts/deploy_manager.js +++ b/lib/contracts/deploy_manager.js @@ -86,16 +86,14 @@ class DeployManager { gasLimit: self.gasLimit }); - deploy.initTracker(function() { - deploy.deployAll(function (err) { - if (!err) { - self.events.emit('contractsDeployed', contractsManager); - } - if (err && self.fatalErrors) { - return callback(err); - } - callback(null, contractsManager, web3); - }); + deploy.deployAll(function (err) { + if (!err) { + self.events.emit('contractsDeployed', contractsManager); + } + if (err && self.fatalErrors) { + return callback(err); + } + callback(null, contractsManager, web3); }); }, function runAfterDeployCommands(contractsManager, web3, callback) { diff --git a/lib/core/config.js b/lib/core/config.js index 55e20ad1f..3d76147a3 100644 --- a/lib/core/config.js +++ b/lib/core/config.js @@ -38,7 +38,7 @@ Config.prototype.loadConfigFiles = function(options) { this.embarkConfig = fs.readJSONSync(options.embarkConfig); this.embarkConfig.plugins = this.embarkConfig.plugins || {}; - this.plugins = new Plugins({plugins: this.embarkConfig.plugins, logger: this.logger, interceptLogs: interceptLogs, events: this.events, config: this, context: this.context}); + this.plugins = new Plugins({plugins: this.embarkConfig.plugins, logger: this.logger, interceptLogs: interceptLogs, events: this.events, config: this, context: this.context, env: this.env}); this.plugins.loadPlugins(); this.loadEmbarkConfigFile(); diff --git a/lib/core/engine.js b/lib/core/engine.js index 782c5a514..e6709a236 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -203,6 +203,9 @@ class Engine { logger: this.logger }); + this.registerModule('deploytracker', { + }); + this.contractsManager = new ContractsManager({ contractFiles: this.config.contractsFiles, contractsConfig: this.config.contractsConfig, diff --git a/lib/core/plugin.js b/lib/core/plugin.js index 2e53238c4..b7d611283 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -28,6 +28,7 @@ var Plugin = function(options) { this.logger = options.logger; this.events = options.events; this.config = options.config; + this.env = options.env; this.loaded = false; this.currentContext = options.context; this.acceptedContext = options.pluginConfig.context || [constants.contexts.any]; diff --git a/lib/core/plugins.js b/lib/core/plugins.js index f12f421f1..78a219750 100644 --- a/lib/core/plugins.js +++ b/lib/core/plugins.js @@ -10,6 +10,7 @@ var Plugins = function(options) { this.events = options.events; this.config = options.config; this.context = options.context; + this.env = options.env; }; Plugins.prototype.loadPlugins = function() { @@ -64,7 +65,8 @@ Plugins.prototype.loadInternalPlugin = function(pluginName, pluginConfig) { events: this.events, config: this.config, isInternal: true, - context: this.context + context: this.context, + env: this.env }); pluginWrapper.loadInternalPlugin(); this.plugins.push(pluginWrapper); diff --git a/lib/contracts/deploy_tracker.js b/lib/modules/deploytracker/index.js similarity index 53% rename from lib/contracts/deploy_tracker.js rename to lib/modules/deploytracker/index.js index 994e1b540..6a8082e2d 100644 --- a/lib/contracts/deploy_tracker.js +++ b/lib/modules/deploytracker/index.js @@ -1,17 +1,42 @@ -let fs = require('../core/fs.js'); -let utils = require('../utils/utils.js'); +let utils = require('../../utils/utils.js'); +let fs = require('../../core/fs.js'); class DeployTracker { - constructor(options, cb) { - const self = this; - this.logger = options.logger; - this.events = options.events; - this.env = options.env; - this.chainConfig = options.chainConfig; + constructor(embark, options) { + this.logger = embark.logger; + this.events = embark.events; + + // TODO: unclear where it comes from + this.env = options.env; + //this.chainConfig = options.chainConfig; + this.chainConfig = embark.config.chainTracker; + this.registerEvents(); + } + + registerEvents() { + const self = this; + + this.events.on("deploy:beforeAll", this.setCurrentChain.bind(this)); + + this.events.on("deploy:contract:deployed", (contract) => { + self.trackContract(contract.className, contract.realRuntimeBytecode, contract.realArgs, contract.address); + self.save(); + }); + + this.events.setCommandHandler("deploy:contract:shouldDeploy", (contract, cb) => { + let trackedContract = self.getContract(contract.className, contract.realRuntimeBytecode, contract.realArgs); + cb(trackedContract); + }); + } + + // TODO: just an event might not be enought to the async nature + // it needs to be a plugin api before deploy, that makes the deployment wait + setCurrentChain() { + const self = this; if (this.chainConfig === false) { this.currentChain = {contracts: []}; - return cb(); + //return cb(); } this.events.request("blockchain:block:byNumber", 0, function(_err, block) { @@ -24,7 +49,7 @@ class DeployTracker { self.currentChain = self.chainConfig[chainId]; self.currentChain.name = self.env; - cb(); + //cb(); }); } @@ -56,6 +81,7 @@ class DeployTracker { } fs.writeJSONSync("./chains.json", this.chainConfig, {spaces: 2}); } + } module.exports = DeployTracker;