diff --git a/lib/contracts/blockchain.js b/lib/contracts/blockchain.js index c7fd3867..3ccee400 100644 --- a/lib/contracts/blockchain.js +++ b/lib/contracts/blockchain.js @@ -4,14 +4,16 @@ class Blockchain { constructor(options) { this.plugins = options.plugins; this.logger = options.logger; - this.config = options.config; - this.contractsConfig = this.config.contractsConfig; + this.events = options.events; + this.contractsConfig = options.contractsConfig; this.web3 = options.web3; + this.addCheck = options.addCheck; if (!this.web3) { this.initWeb3(); } this.registerServiceCheck(); + this.registerAccountRequests(); } initWeb3() { @@ -55,6 +57,31 @@ class Blockchain { }); } + registerAccountRequests() { + const self = this; + + this.events.setCommandHandler("blockchain:defaultAccount:get", function(cb) { + cb(self.defaultAccount); + }); + + this.events.setCommandHandler("blockchain:defaultAccount:set", function(account, cb) { + self.setDefaultAccount(account); + cb(); + }); + } + + defaultAccount() { + return this.web3.eth.defaultAccount; + } + + setDefaultAccount(account) { + this.web3.eth.defaultAccount = account; + } + + getAccounts(cb) { + this.web3.eth.getAccounts(cb); + } + } module.exports = Blockchain; diff --git a/lib/contracts/deploy.js b/lib/contracts/deploy.js index 9ae358f9..0d690230 100644 --- a/lib/contracts/deploy.js +++ b/lib/contracts/deploy.js @@ -9,7 +9,8 @@ let CodeGenerator = require('./code_generator.js'); class Deploy { constructor(options) { - this.web3 = options.web3; + this.blockchain = options.blockchain; + this.web3 = this.blockchain.web3; this.contractsManager = options.contractsManager; this.logger = options.logger; this.events = options.events; @@ -220,12 +221,12 @@ class Deploy { let accounts = []; let contractParams = (params || contract.args).slice(); let contractCode = contract.code; - let deploymentAccount = self.web3.eth.defaultAccount; + let deploymentAccount = self.blockchain.defaultAccount(); let deployObject; async.waterfall([ function getAccounts(next) { - self.web3.eth.getAccounts(function (err, _accounts) { + self.blockchain.getAccounts(function (err, _accounts) { if (err) { return next(new Error(err)); } diff --git a/lib/contracts/deploy_manager.js b/lib/contracts/deploy_manager.js index 0c21e642..0bf5f9ef 100644 --- a/lib/contracts/deploy_manager.js +++ b/lib/contracts/deploy_manager.js @@ -11,7 +11,8 @@ class DeployManager { this.events = options.events; this.plugins = options.plugins; - this.web3 = options.web3; + this.blockchain = options.blockchain; + this.web3 = this.blockchain.web3; this.chainConfig = (options.trackContracts !== false) ? this.config.chainTracker : false; this.contractsManager = options.contractsManager; this.gasLimit = false; @@ -52,7 +53,7 @@ class DeployManager { return callback(Error("error connecting to blockchain node")); } - self.web3.eth.getAccounts(function(err, _accounts) { + self.blockchain.getAccounts(function(err, _accounts) { if (err) { self.logger.error(__("Couldn't connect to an Ethereum node are you sure it's on?").red); self.logger.info(__("make sure you have an Ethereum node or simulator running. e.g '%s'", 'embark blockchain').magenta); @@ -62,20 +63,20 @@ class DeployManager { }); }, function setDefaultAccount(contractsManager, web3, callback) { - web3.eth.getAccounts(function (err, accounts) { + self.blockchain.getAccounts(function (err, accounts) { if (err) { self.logger.error(err); return callback(new Error(err)); } let accountConfig = self.config.blockchainConfig.account; let selectedAccount = accountConfig && accountConfig.address; - web3.eth.defaultAccount = (selectedAccount || accounts[0]); + self.blockchain.setDefaultAccount(selectedAccount || accounts[0]); callback(null, contractsManager, web3); }); }, function deployAllContracts(contractsManager, web3, callback) { let deploy = new Deploy({ - web3: web3, + blockchain: self.blockchain, contractsManager: contractsManager, logger: self.logger, events: self.events, diff --git a/lib/core/engine.js b/lib/core/engine.js index f54e9f11..5d133601 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -213,7 +213,7 @@ class Engine { }); this.deployManager = new DeployManager({ - web3: options.web3 || self.web3, + blockchain: this.blockchain, trackContracts: options.trackContracts, config: this.config, logger: this.logger, @@ -278,7 +278,7 @@ class Engine { addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor), events: this.events, logger: this.logger, - options: options.web3 // TODO: might not be necessary + web3: options.web3 }); this.web3 = this.blockchain.web3; diff --git a/lib/tests/test.js b/lib/tests/test.js index 1dd2967e..d89a649c 100644 --- a/lib/tests/test.js +++ b/lib/tests/test.js @@ -69,8 +69,10 @@ Test.prototype.deployAll = function(contractsConfig, cb) { //{abiType: 'contracts', embarkJS: false} self.engine.startService("libraryManager"); self.engine.startService("codeGenerator"); + self.engine.startService("web3", { + web3: self.web3 + }); self.engine.startService("deployment", { - web3: self.web3, trackContracts: false }); callback();