diff --git a/packages/embark/src/lib/core/engine.js b/packages/embark/src/lib/core/engine.js index 897b50a4f..090bb31ad 100644 --- a/packages/embark/src/lib/core/engine.js +++ b/packages/embark/src/lib/core/engine.js @@ -2,6 +2,7 @@ const async = require('async'); const utils = require('../utils/utils'); const IPC = require('./ipc'); +const fs = require('./fs'); class Engine { constructor(options) { @@ -30,6 +31,7 @@ class Engine { let options = _options || {}; this.events = options.events || this.events || new Events(); + this.registerCommands(); this.logger = options.logger || new Logger({context: this.context, logLevel: options.logLevel || this.logLevel || 'debug', events: this.events, logFile: this.logFile}); this.config = new Config({env: this.env, logger: this.logger, events: this.events, context: this.context, webServerConfig: this.webServerConfig, version: this.version}); this.config.loadConfigFiles({embarkConfig: this.embarkConfig, interceptLogs: this.interceptLogs}); @@ -49,9 +51,23 @@ class Engine { this.ipc.serve(); return callback(); } + callback(); } + registerCommands() { + this.events.setCommandHandler('embark:path', function () { + const cb = arguments[arguments.length - 1]; + const pathParts = Array.from(arguments).splice(0, arguments.length - 1); + cb(fs.embarkPath(...pathParts)); + }); + this.events.setCommandHandler('dapp:path', function () { + const cb = arguments[arguments.length - 1]; + const pathParts = Array.from(arguments).splice(0, arguments.length - 1); + cb(fs.dappPath(...pathParts)); + }); + } + registerModule(moduleName, options) { this.plugins.loadInternalPlugin(moduleName, options || {}); } diff --git a/packages/embark/src/lib/modules/code_generator/code_templates/define-web3-simple.js.ejs b/packages/embark/src/lib/modules/code_generator/code_templates/define-web3-simple.js.ejs deleted file mode 100644 index 2bcc5d843..000000000 --- a/packages/embark/src/lib/modules/code_generator/code_templates/define-web3-simple.js.ejs +++ /dev/null @@ -1,9 +0,0 @@ -__mainContext.web3 = undefined; -web3 = new Web3(new Web3.providers.HttpProvider("<%- url -%>")); -web3.eth.getAccounts(function(err, accounts) { - if (err) { - return done(new Error(err)); - } - web3.defaultAccount = accounts[0]; - <%- done %> -}); diff --git a/packages/embark/src/lib/modules/code_generator/index.js b/packages/embark/src/lib/modules/code_generator/index.js index 673b17059..ef09a0574 100644 --- a/packages/embark/src/lib/modules/code_generator/index.js +++ b/packages/embark/src/lib/modules/code_generator/index.js @@ -10,7 +10,6 @@ const Templates = { load_manager: require('./code_templates/load-manager.js.ejs'), define_when_env_loaded: require('./code_templates/define-when-env-loaded.js.ejs'), main_context: require('./code_templates/main-context.js.ejs'), - define_web3_simple: require('./code_templates/define-web3-simple.js.ejs'), do_when_loaded: require('./code_templates/do-when-loaded.js.ejs'), exec_when_env_loaded: require('./code_templates/exec-when-env-loaded.js.ejs') }; @@ -299,20 +298,7 @@ class CodeGenerator { let code = ""; async.waterfall([ - function getWeb3Location(next) { - self.events.request("version:get:web3", function(web3Version) { - if (web3Version === "1.0.0-beta") { - return next(null, require.resolve("web3", {paths: [self.fs.embarkPath("node_modules")]})); - } - self.events.request("version:getPackageLocation", "web3", web3Version, function(err, location) { - return next(null, self.fs.dappPath(location)); - }); - }); - }, - function getImports(web3Location, next) { - web3Location = web3Location.replace(/\\/g, '/'); // Import paths must always have forward slashes - code += `\nimport Web3 from '${web3Location}';\n`; - code += `\nglobal.Web3 = Web3;`; + function getImports(next) { code += "\nimport IpfsApi from 'ipfs-api';\n"; next(); diff --git a/packages/embark/src/lib/modules/pipeline/index.js b/packages/embark/src/lib/modules/pipeline/index.js index 644bb8751..9a6a96536 100644 --- a/packages/embark/src/lib/modules/pipeline/index.js +++ b/packages/embark/src/lib/modules/pipeline/index.js @@ -156,7 +156,6 @@ class Pipeline { (next) => self.buildContracts(next), function createImportList(next) { importsList["Embark/EmbarkJS"] = self.fs.dappPath(self.embarkConfig.generationDir, constants.dappConfig.embarkjs); - importsList["Embark/web3"] = self.fs.dappPath(".embark", 'web3_instance.js'); importsList["Embark/contracts"] = contractsDir; self.plugins.getPluginsProperty('imports', 'imports').forEach(importObject => { diff --git a/packages/embarkjs/src/blockchain.js b/packages/embarkjs/src/blockchain.js index 93ba8b4d6..50bf0e340 100644 --- a/packages/embarkjs/src/blockchain.js +++ b/packages/embarkjs/src/blockchain.js @@ -215,12 +215,11 @@ let Contract = function(options) { this.code = '0x' + options.code; this.blockchainConnector = Blockchain.blockchainConnector; - this.web3 = this.blockchainConnector.getInstance(); ContractClass = this.blockchainConnector.newContract({abi: this.abi, address: this.address}); contracts.push(ContractClass); ContractClass.options.data = this.code; - const from = this.from || self.blockchainConnector.getDefaultAccount() || this.web3.eth.defaultAccount; + const from = this.from || self.blockchainConnector.getDefaultAccount(); if (from) { ContractClass.options.from = from; } @@ -232,9 +231,9 @@ let Contract = function(options) { Blockchain.execWhenReady(function(_err, _web3) { if (!ContractClass.currentProvider) { - ContractClass.setProvider(self.blockchainConnector.getCurrentProvider() || self.web3.currentProvider); + ContractClass.setProvider(self.blockchainConnector.getCurrentProvider()); } - ContractClass.options.from = self.blockchainConnector.getDefaultAccount() ||self.web3.eth.defaultAccount; + ContractClass.options.from = self.blockchainConnector.getDefaultAccount(); }); ContractClass._jsonInterface.forEach((abi) => { @@ -288,7 +287,7 @@ Contract.prototype.deploy = function(args, _options) { contractParams = args || []; contractParams.push({ - from: this.blockchainConnector.getDefaultAccount() || this.web3.eth.accounts[0], + from: this.blockchainConnector.getDefaultAccount(), data: this.code, gas: options.gas || 800000 }); diff --git a/packages/web3Connector/index.js b/packages/web3Connector/index.js index f83fd19e9..c7dde503b 100644 --- a/packages/web3Connector/index.js +++ b/packages/web3Connector/index.js @@ -1,24 +1,48 @@ const path = require('path'); +const fs = require('fs'); module.exports = (embark) => { embark.events.on('runcode:ready', () => { - embark.events.emit('runcode:register', '__web3Connector', require('./web3Connector'), false); + const pathPromise = new Promise((resolve, reject) => { + embark.events.request("version:get:web3", (web3Version) => { + if (web3Version === "1.0.0-beta") { + return embark.events.request('embark:path', 'node_modules', (result) => { + const web3Path = require.resolve("web3", {paths: [result]}); + resolve(web3Path); + }); + } + embark.events.request("version:getPackageLocation", "web3", web3Version, (err, location) => { + if (err) { + return reject(err); + } + embark.events.request('dapp:path', location, resolve); + }); + }); + }); + + pathPromise.then((web3Location) => { + web3Location = web3Location.replace(/\\/g, '/'); + embark.events.emit('runcode:register', 'Web3', require(web3Location), false); + + let code = `\nconst Web3 = global.Web3 || require('${web3Location}');`; + code += `\nglobal.Web3 = Web3;`; + + const connectorCode = fs.readFileSync(path.join(__dirname, 'web3Connector.js'), 'utf8'); + code += connectorCode; + + code += "\nEmbarkJS.Blockchain.registerProvider('web3', web3Connector);"; + + code += "\nEmbarkJS.Blockchain.setProvider('web3', {});"; + + embark.addCodeToEmbarkJS(code); + + code = "EmbarkJS.Blockchain.setProvider('web3', {});"; + + const shouldInit = (_config) => { + return true; + }; + + embark.addConsoleProviderInit('blockchain', code, shouldInit); + }); }); - - let code = `\nconst __embarkWeb3 = global.__web3Connector || require('${path.join(__dirname, 'web3ConnectorBrowser.js').replace(/\\/g, '/')}').default;`; - - code += "\nEmbarkJS.Blockchain.registerProvider('web3', __embarkWeb3);"; - - // TODO when we refactor code generator, refactor this to actually do something like connect - code += "\nEmbarkJS.Blockchain.setProvider('web3', {});"; - - embark.addCodeToEmbarkJS(code); - - code = "EmbarkJS.Blockchain.setProvider('web3', {});"; - - const shouldInit = (_config) => { - return true; - }; - - embark.addConsoleProviderInit('blockchain', code, shouldInit); }; diff --git a/packages/web3Connector/package.json b/packages/web3Connector/package.json index b2a798c88..e1ed5722b 100644 --- a/packages/web3Connector/package.json +++ b/packages/web3Connector/package.json @@ -4,7 +4,7 @@ "description": "Web3.js Connector for EmbarkJS", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 0" }, "repository": { "type": "git", @@ -17,9 +17,6 @@ "ethereum", "smart-contract" ], - "dependencies": { - "web3": "1.0.0-beta.37" - }, "author": "Jonathan Rainville", "license": "ISC" } diff --git a/packages/web3Connector/web3Connector.js b/packages/web3Connector/web3Connector.js index 473a804f3..e107b1e9a 100644 --- a/packages/web3Connector/web3Connector.js +++ b/packages/web3Connector/web3Connector.js @@ -1,14 +1,10 @@ -const Web3 = require('web3'); - +/*global Web3*/ const web3Connector = {}; -// const _web3 = require('./web3_instance').default; -// global.wtf = _web3; web3Connector.init = function(_config) { // Check if the global web3 object uses the old web3 (0.x) if (global.web3 && typeof global.web3.version !== 'string') { // If so, use a new instance using 1.0, but use its provider - // _web3.setProvider(global.web3.currentProvider); this.web3 = new Web3(global.web3.currentProvider); } else { this.web3 = global.web3 || new Web3(); @@ -29,7 +25,6 @@ web3Connector.getNewProvider = function (providerName, ...args) { }; web3Connector.setProvider = function (provider) { - // _web3.setProvider(provider); return this.web3.setProvider(provider); }; @@ -60,12 +55,3 @@ web3Connector.toWei = function () { web3Connector.getNetworkId = function () { return this.web3.eth.net.getId(); }; - -module.exports = web3Connector; -// if (typeof module !== 'undefined' && module.exports) { -// module.exports = web3Connector; -// return; -// } else { -// export default web3Connector; -// } -// exports.default = web3Connector; diff --git a/packages/web3Connector/web3ConnectorBrowser.js b/packages/web3Connector/web3ConnectorBrowser.js deleted file mode 100644 index aac713201..000000000 --- a/packages/web3Connector/web3ConnectorBrowser.js +++ /dev/null @@ -1,71 +0,0 @@ -const Web3 = require('web3'); - -const web3Connector = {}; -// const _web3 = require('./web3_instance').default; -// global.wtf = _web3; - -web3Connector.init = function(_config) { - // Check if the global web3 object uses the old web3 (0.x) - if (global.web3 && typeof global.web3.version !== 'string') { - // If so, use a new instance using 1.0, but use its provider - // _web3.setProvider(global.web3.currentProvider); - this.web3 = new Web3(global.web3.currentProvider); - } else { - this.web3 = global.web3 || new Web3(); - } - global.web3 = this.web3; -}; - -web3Connector.getInstance = function () { - return this.web3; -}; - -web3Connector.getAccounts = function () { - return this.web3.eth.getAccounts(...arguments); -}; - -web3Connector.getNewProvider = function (providerName, ...args) { - return new Web3.providers[providerName](...args); -}; - -web3Connector.setProvider = function (provider) { - // _web3.setProvider(provider); - return this.web3.setProvider(provider); -}; - -web3Connector.getCurrentProvider = function () { - return this.web3.currentProvider; -}; - -web3Connector.getDefaultAccount = function () { - return this.web3.eth.defaultAccount; -}; - -web3Connector.setDefaultAccount = function (account) { - this.web3.eth.defaultAccount = account; -}; - -web3Connector.newContract = function (options) { - return new this.web3.eth.Contract(options.abi, options.address); -}; - -web3Connector.send = function () { - return this.web3.eth.sendTransaction(...arguments); -}; - -web3Connector.toWei = function () { - return this.web3.toWei(...arguments); -}; - -web3Connector.getNetworkId = function () { - return this.web3.eth.net.getId(); -}; - - -// if (typeof module !== 'undefined' && module.exports) { -// module.exports = web3Connector; -// return; -// } else { -// export default web3Connector; -// } -export default web3Connector; diff --git a/test_dapps/test_app/embark.json b/test_dapps/test_app/embark.json index bac797e50..2f1c00792 100644 --- a/test_dapps/test_app/embark.json +++ b/test_dapps/test_app/embark.json @@ -20,7 +20,8 @@ "ipfs-api": "17.2.7" }, "plugins": { - "embark-service": {} + "embark-service": {}, + "web3Connector": {} }, "options": { "solc": { diff --git a/test_dapps/test_app/package.json b/test_dapps/test_app/package.json index 853ce4619..3bcfcf3a5 100644 --- a/test_dapps/test_app/package.json +++ b/test_dapps/test_app/package.json @@ -11,7 +11,8 @@ "react-bootstrap": "0.32.4", "react-dom": "16.7.0", "rimraf": "2.6.3", - "zeppelin-solidity": "1.12.0" + "zeppelin-solidity": "1.12.0", + "web3Connector": "file:../../../packages/web3Connector" }, "name": "test_app", "private": true,