From 9d6e7b79bdb7111501eb576d9baa55b339465600 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 2 Oct 2016 17:57:33 -0400 Subject: [PATCH] implement onDeploy --- demo/chains.json | 4 ++-- demo/config/contracts.json | 3 +++ lib/deploy.js | 16 ++++++++++++++++ lib/index.js | 23 ++++++++++++++--------- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/demo/chains.json b/demo/chains.json index 16e0e52c..25955750 100644 --- a/demo/chains.json +++ b/demo/chains.json @@ -7,7 +7,7 @@ "name": "token" }, "577bcb0c85129a6ff9e4dcde08f04e6f094e94c74a4423fc90e5947df9a21b82": { - "address": "0xf88f536ed745bb1242a10c085feb5aca8701711d", + "address": "0xb81f1e90ac9e4eb49271c8bbf574dde2b8380078", "name": "SimpleStorage" }, "708fa6b699f419627ab3c4c2d9c82f8f1a6fab03c122d0a9ee55d2d0d0ad1e4b": { @@ -15,7 +15,7 @@ "name": "token" }, "e0d35d6564373021d9749a7a8815cf58cc5ca7b7edaf4740c1913898561531c3": { - "address": "0x7d4d2c27d1ed2d40df055bbc1586c12ce97f19f8", + "address": "0x5f6dc5474cd23ff54f37cfa12c78726c2fa1bce6", "name": "SimpleStorage2" }, "f3765f8b702ccb44eb19f1adecbf5a216175713fbd41d9fae100d8e3dfc5e74f": { diff --git a/demo/config/contracts.json b/demo/config/contracts.json index d5b177d0..ca484981 100644 --- a/demo/config/contracts.json +++ b/demo/config/contracts.json @@ -12,6 +12,9 @@ "args": [ 100, "$token" + ], + "onDeploy": [ + "SimpleStorage.set(150)" ] }, "SimpleStorage2": { diff --git a/lib/deploy.js b/lib/deploy.js index b1535aa3..6003d37b 100644 --- a/lib/deploy.js +++ b/lib/deploy.js @@ -1,6 +1,8 @@ var async = require('async'); var Compiler = require('./compiler.js'); var DeployTracker = require('./deploy_tracker.js'); +var ABIGenerator = require('./abi.js'); +var web3; var Deploy = function(options) { this.web3 = options.web3; @@ -68,6 +70,20 @@ Deploy.prototype.checkAndDeployContract = function(contract, params, callback) { self.deployTracker.trackContract(contract.className, contract.code, realArgs, address); self.deployTracker.save(); self.logger.contractsState(self.contractsManager.contractsState()); + + // TODO: replace with separate process so it's isolated and with + // a callback + if (contract.onDeploy !== undefined) { + self.logger.info('executing onDeploy commands'); + var abiGenerator = new ABIGenerator({}, self.contractsManager); + web3 = self.web3; + var abi = abiGenerator.generateContracts(false); + eval(abi); + + var cmds = contract.onDeploy.join(';\n'); + eval(cmds); + } + callback(); }); } diff --git a/lib/index.js b/lib/index.js index af1a6be7..78284bcb 100644 --- a/lib/index.js +++ b/lib/index.js @@ -212,16 +212,21 @@ var Embark = { exit(); } - var deploy = new Deploy({ - web3: web3, - contractsManager: contractsManager, - logger: Embark.logger, - chainConfig: self.config.chainTracker, - env: self.config.env - }); - deploy.deployAll(function() { - callback(null, contractsManager); + web3.eth.getAccounts(function(err, accounts) { + web3.eth.defaultAccount = accounts[0]; + + var deploy = new Deploy({ + web3: web3, + contractsManager: contractsManager, + logger: Embark.logger, + chainConfig: self.config.chainTracker, + env: self.config.env + }); + deploy.deployAll(function() { + callback(null, contractsManager); + }); }); + } ], function(err, result) { done(result);