From c550b688d3f6c9510945a314f1050ef18d8c7ea9 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 9 Oct 2015 10:18:18 -0400 Subject: [PATCH] add flag to include provider in abi; update spec --- demo/spec/contracts/simple_storage_spec.js | 46 ++++++++++++++-------- lib/chain_manager.js | 12 +++--- lib/deploy.js | 25 +++++++----- lib/index.js | 20 +++++++--- 4 files changed, 67 insertions(+), 36 deletions(-) diff --git a/demo/spec/contracts/simple_storage_spec.js b/demo/spec/contracts/simple_storage_spec.js index 343612c2..7d34c913 100644 --- a/demo/spec/contracts/simple_storage_spec.js +++ b/demo/spec/contracts/simple_storage_spec.js @@ -1,7 +1,14 @@ var Embark = require('embark-framework'); var ethersim = require('ethersim'); var web3 = require('web3'); -Embark.init(); + +var Manager = ethersim.Manager; +var Provider = ethersim.Provider; + +var manager = new Manager(); +web3.setProvider(new Provider(manager)); + +Embark.init(web3); Embark.blockchainConfig.loadConfigFile('config/blockchain.yml'); Embark.contractsConfig.loadConfigFile('config/contracts.yml'); @@ -9,29 +16,34 @@ var files = ["app/contracts/simple_storage.sol"]; Embark.contractsConfig.init(files, 'development'); -var Manager = ethersim.Manager; -var Provider = ethersim.Provider; - console.log("initializing"); -var manager = new Manager(); - -web3.setProvider(new Provider(manager)); -abi = Embark.deployContracts('development', files, "/tmp/abi.js", "chains.json", web3); -console.log(abi); -eval(abi); describe("SimpleStorage", function() { - beforeAll(function() { - SimpleStorage = EmbarkSpec.request("SimpleStorage", [150]); + beforeAll(function(done) { + Embark.deployContracts('development', files, "/tmp/abi.js", "chains.json", false, function(abi) { + console.log("return abi"); + console.log(abi); + eval(abi); + done(); + }); + //SimpleStorage = EmbarkSpec.request("SimpleStorage", [150]); }); - it("should set constructor value", function() { - expect(SimpleStorage.storedData()).toEqual('150'); + it("should set constructor value", function(done) { + SimpleStorage.storedData(function(err, result) { + expect(result.toNumber()).toEqual(100); + done(); + }); }); - it("set storage value", function() { - SimpleStorage.set(100); - expect(SimpleStorage.get()).toEqual('100'); + it("set storage value", function(done) { + SimpleStorage.set(150, function() { + SimpleStorage.get(function(err, result) { + console.log(arguments); + expect(result.toNumber()).toEqual(150); + done(); + }); + }); }); }) diff --git a/lib/chain_manager.js b/lib/chain_manager.js index abd0aea9..ca8cff78 100644 --- a/lib/chain_manager.js +++ b/lib/chain_manager.js @@ -2,11 +2,11 @@ var fs = require('fs'); var web3 = require('web3'); var sha3_256 = require('js-sha3').sha3_256; -ChainManager = function() { +ChainManager = function(_web3) { this.chainManagerConfig = {}; this.currentChain = {}; this.file = ""; - this.web3 = null; + this.web3 = _web3; } ChainManager.prototype.loadConfigFile = function(filename) { @@ -26,9 +26,11 @@ ChainManager.prototype.loadConfig = function(config) { }; ChainManager.prototype.init = function(env, config) { - web3.setProvider(new web3.providers.HttpProvider("http://" + config.rpcHost + ":" + config.rpcPort)); + if (this.web3 === undefined) { + web3.setProvider(new web3.providers.HttpProvider("http://" + config.rpcHost + ":" + config.rpcPort)); + } - var block = web3.eth.getBlock(0); + var block = this.web3.eth.getBlock(0); if(!block){ throw new Error("Cannot get the genesis block, is embark blockchain running ?"); } @@ -39,7 +41,7 @@ ChainManager.prototype.init = function(env, config) { } this.currentChain = this.chainManagerConfig[chainId]; - this.web3 = web3; + //this.web3 = web3; } ChainManager.prototype.addContract = function(contractName, code, args, address) { diff --git a/lib/deploy.js b/lib/deploy.js index 9cabca33..7e2c9354 100644 --- a/lib/deploy.js +++ b/lib/deploy.js @@ -10,7 +10,10 @@ sleep = function sleep(ms) { while (new Date().getTime() < start + ms); } -Deploy = function(env, contractFiles, blockchainConfig, contractsConfig, chainManager) { +Deploy = function(env, contractFiles, blockchainConfig, contractsConfig, chainManager, _web3) { + if (_web3 !== undefined) { + web3 = _web3; + } //this.blockchainConfig = (new Config.Blockchain()).loadConfigFile('config/blockchain.yml').config(env); this.blockchainConfig = blockchainConfig; this.chainManager = chainManager; @@ -22,7 +25,9 @@ Deploy = function(env, contractFiles, blockchainConfig, contractsConfig, chainMa this.deployedContracts = {}; try { - web3.setProvider(new web3.providers.HttpProvider("http://" + this.blockchainConfig.rpcHost + ":" + this.blockchainConfig.rpcPort)); + if (_web3 === undefined) { + web3.setProvider(new web3.providers.HttpProvider("http://" + this.blockchainConfig.rpcHost + ":" + this.blockchainConfig.rpcPort)); + } primaryAddress = web3.eth.coinbase; web3.eth.defaultAccount = primaryAddress; } catch (e) { @@ -185,14 +190,16 @@ Deploy.prototype.execute_cmds = function(cmds) { } } -Deploy.prototype.generate_abi_file = function(web3) { - var result; +Deploy.prototype.generate_provider_file = function() { + var result = ""; + result = "web3.setProvider(new web3.providers.HttpProvider('http://" + this.blockchainConfig.rpcHost + ":" + this.blockchainConfig.rpcPort + "'));"; + result += "web3.eth.defaultAccount = web3.eth.accounts[0];"; - result = ""; - if (web3 === undefined) { - result = "web3.setProvider(new web3.providers.HttpProvider('http://" + this.blockchainConfig.rpcHost + ":" + this.blockchainConfig.rpcPort + "'));"; - result += "web3.eth.defaultAccount = web3.eth.accounts[0];"; - } + return result; +} + +Deploy.prototype.generate_abi_file = function() { + var result = ""; for(className in this.deployedContracts) { var deployedContract = this.deployedContracts[className]; diff --git a/lib/index.js b/lib/index.js index b41ba260..19bb8b7c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -19,11 +19,17 @@ var Compiler = require('./compiler.js'); var ChainManager = require('./chain_manager.js'); Embark = { - init: function() { + init: function(_web3) { this.blockchainConfig = (new Config.Blockchain()); this.compiler = (new Compiler(this.blockchainConfig)); this.contractsConfig = (new Config.Contracts(this.blockchainConfig, this.compiler)); - this.chainManager = (new ChainManager()); + if (_web3 !== undefined) { + this.web3 = _web3; + } + else { + this.web3 = web3; + } + this.chainManager = (new ChainManager(this.web3)); }, //tests: function() { @@ -45,14 +51,18 @@ Embark = { return chain.getStartChainCommand(use_tmp); }, - deployContracts: function(env, contractFiles, destFile, chainFile, cb) { + deployContracts: function(env, contractFiles, destFile, chainFile, withProvider, cb) { this.contractsConfig.init(contractFiles, env); this.chainManager.loadConfigFile(chainFile) - var deploy = new Deploy(env, contractFiles, this.blockchainConfig.config(env), this.contractsConfig, this.chainManager); + var deploy = new Deploy(env, contractFiles, this.blockchainConfig.config(env), this.contractsConfig, this.chainManager, this.web3); deploy.deploy_contracts(env, function() { console.log("contracts deployed; generating abi file"); - var result = deploy.generate_abi_file(); + var result = "" + if (withProvider) { + result += deploy.generate_provider_file(); + } + result += deploy.generate_abi_file(); cb(result); }); },