diff --git a/lib/compiler.js b/lib/compiler.js new file mode 100644 index 000000000..e57af716a --- /dev/null +++ b/lib/compiler.js @@ -0,0 +1,25 @@ +var web3 = require('web3'); + +Compiler = function(blockchainConfig) { + this.blockchainConfig = blockchainConfig; +}; + +Compiler.prototype.init = function(env) { + var config = this.blockchainConfig.config(env); + + try { + web3.setProvider(new web3.providers.HttpProvider("http://" + config.rpcHost + ":" + config.rpcPort)); + primaryAddress = web3.eth.coinbase; + web3.eth.defaultAccount = primaryAddress; + } catch (e) { + throw new Error("can't connect to " + config.rpcHost + ":" + config.rpcPort + " check if an ethereum node is running"); + } + + console.log("address is : " + primaryAddress); +}; + +Compiler.prototype.compile = function(source) { + return web3.eth.compile.solidity(source); +}; + +module.exports = Compiler; diff --git a/lib/config/contracts.js b/lib/config/contracts.js index 4ec407b16..2fe63cab8 100644 --- a/lib/config/contracts.js +++ b/lib/config/contracts.js @@ -2,9 +2,9 @@ var readYaml = require('read-yaml'); var fs = require('fs'); var toposort = require('toposort'); -ContractsConfig = function(blockchainConfig, web3) { +ContractsConfig = function(blockchainConfig, compiler) { this.blockchainConfig = blockchainConfig; - this.web3 = web3; + this.compiler = compiler; this.contractFiles = []; } @@ -17,17 +17,6 @@ ContractsConfig.prototype.init = function(files) { if (this.blockchainConfig.config != undefined) { this.blockchainConfig = this.blockchainConfig.config('development'); } - - try { - this.web3.setProvider(new this.web3.providers.HttpProvider("http://" + this.blockchainConfig.rpcHost + ":" + this.blockchainConfig.rpcPort)); - primaryAddress = this.web3.eth.coinbase; - this.web3.eth.defaultAccount = primaryAddress; - } catch (_error) { - e = _error; - throw new Error("can't connect to " + this.blockchainConfig.rpcHost + ":" + this.blockchainConfig.rpcPort + " check if an ethereum node is running"); - } - - console.log("address is : " + primaryAddress); }; ContractsConfig.prototype.loadConfigFile = function(filename) { @@ -51,6 +40,7 @@ ContractsConfig.prototype.config = function(env) { ContractsConfig.prototype.compileContracts = function(env) { var contractFile, source, j; var contractsConfig = this.config(env); + this.compiler.init(env); if (contractsConfig != null) { for (className in contractsConfig) { @@ -75,7 +65,7 @@ ContractsConfig.prototype.compileContracts = function(env) { source = fs.readFileSync(contractFile).toString() console.log("compiling " + contractFile); - compiled_contracts = this.web3.eth.compile.solidity(source); + compiled_contracts = this.compiler.compile(source); for (className in compiled_contracts) { var contract = compiled_contracts[className]; this.all_contracts.push(className); diff --git a/lib/index.js b/lib/index.js index fc584b384..2d7545577 100644 --- a/lib/index.js +++ b/lib/index.js @@ -17,11 +17,13 @@ var Blockchain = require('./blockchain.js'); var Deploy = require('./deploy.js'); var Release = require('./ipfs.js'); var Config = require('./config/config.js'); +var Compiler = require('./config/compiler.js'); Embark = { init: function() { this.blockchainConfig = (new Config.Blockchain()); - this.contractsConfig = (new Config.Contracts(this.blockchainConfig, web3)); + this.compiler = (new Compiler(this.blockchainConfig())); + this.contractsConfig = (new Config.Contracts(this.blockchainConfig, this.compiler)); }, tests: function(contractFiles) { diff --git a/test/config.contracts.js b/test/config.contracts.js index 78f6381e5..0b9602685 100644 --- a/test/config.contracts.js +++ b/test/config.contracts.js @@ -1,15 +1,17 @@ var Config = require('../lib/config/config.js'); +var Compiler = require('../lib/compiler.js'); var assert = require('assert'); var sinon = require('sinon'); -var web3 = require('web3'); require('mocha-sinon'); describe('embark.config.contracts', function() { - var blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml').config("development"); + var _blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml'); + var blockchainConfig = _blockchainConfig.config("development"); + var compiler = new Compiler(_blockchainConfig); describe('#loadConfigFile', function() { it('should read and load yml file', function() { - var contractsConfig = new Config.Contracts(blockchainConfig, web3); + var contractsConfig = new Config.Contracts(blockchainConfig, compiler); contractsConfig.loadConfigFile('test/support/contracts.yml'); assert.equal(contractsConfig.contractConfig.hasOwnProperty('development'), true) @@ -23,7 +25,7 @@ describe('embark.config.contracts', function() { describe('#loadConfig', function() { it('should load config', function() { - var contractsConfig = new Config.Contracts([], blockchainConfig, web3); + var contractsConfig = new Config.Contracts([], blockchainConfig, compiler); var hsh = { development: {}, staging: {} @@ -43,7 +45,7 @@ describe('embark.config.contracts', function() { 'test/support/contracts/simple_storage.sol', 'test/support/contracts/another_storage.sol' ] - contractsConfig = new Config.Contracts(blockchainConfig, web3); + contractsConfig = new Config.Contracts(blockchainConfig, compiler); contractsConfig.loadConfigFile('test/support/contracts.yml'); contractsConfig.init(files); contractsConfig.compileContracts(); @@ -62,7 +64,7 @@ describe('embark.config.contracts', function() { 'test/support/contracts/another_storage.sol', 'test/support/contracts/wallets.sol' ] - contractsConfig = new Config.Contracts(blockchainConfig, web3); + contractsConfig = new Config.Contracts(blockchainConfig, compiler); contractsConfig.loadConfigFile('test/support/arguments.yml'); contractsConfig.init(files); contractsConfig.compileContracts('development'); diff --git a/test/deploy.js b/test/deploy.js index e8f9bd164..99070a20b 100644 --- a/test/deploy.js +++ b/test/deploy.js @@ -1,7 +1,7 @@ var Config = require('../lib/config/config.js'); var Deploy = require('../lib/deploy.js'); +var Compiler = require('../lib/compiler.js'); var assert = require('assert'); -var web3 = require('web3'); describe('embark.deploy', function() { var files = [ @@ -10,13 +10,16 @@ describe('embark.deploy', function() { 'test/support/contracts/another_storage.sol', 'test/support/contracts/wallets.sol' ]; - var blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml').config("development"); - var contractsConfig = new Config.Contracts(blockchainConfig, web3); + var _blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml'); + var blockchainConfig = _blockchainConfig.config("development"); + var compiler = new Compiler(_blockchainConfig); + var contractsConfig = new Config.Contracts(blockchainConfig, compiler); contractsConfig.loadConfigFile('test/support/arguments.yml'); contractsConfig.init(files); var deploy = new Deploy('development', files, blockchainConfig, contractsConfig); describe('#deploy_contracts', function() { + compiler.init('development'); deploy.deploy_contracts("development"); it("should deploy contracts", function() { @@ -41,6 +44,7 @@ describe('embark.deploy', function() { } it("should deploy contracts", function() { + compiler.init('development'); var result = deploy.generate_abi_file(); assert.strictEqual(result, "web3.setProvider(new web3.providers.HttpProvider('http://localhost:8101'));web3.eth.defaultAccount = web3.eth.accounts[0];var SimpleStorageAbi = 123;var SimpleStorageContract = web3.eth.contract(SimpleStorageAbi);var SimpleStorage = SimpleStorageContract.at('0x123');var AnotherStorageAbi = 234;var AnotherStorageContract = web3.eth.contract(AnotherStorageAbi);var AnotherStorage = AnotherStorageContract.at('0x234');"); diff --git a/test/test.js b/test/test.js index 1682c1c45..1d88d346b 100644 --- a/test/test.js +++ b/test/test.js @@ -1,7 +1,7 @@ var Config = require('../lib/config/config.js'); var Test = require('../lib/test.js'); +var Compiler = require('../lib/compiler.js'); var assert = require('assert'); -var web3 = require('web3'); //var contractFiles = grunt.file.expand("./app/contracts/**/*.sol") @@ -9,8 +9,10 @@ describe('embark.test', function() { var files = [ 'test/support/contracts/simple_storage.sol' ] - var blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml').config("development"); - var contractsConfig = new Config.Contracts(blockchainConfig, web3); + var _blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml'); + var blockchainConfig = _blockchainConfig.config("development"); + var compiler = new Compiler(_blockchainConfig); + var contractsConfig = new Config.Contracts(blockchainConfig, compiler); contractsConfig.loadConfigFile('test/support/contracts.yml'); contractsConfig.init(files);