diff --git a/packages/embark/src/lib/modules/blockchain_connector/index.js b/packages/embark/src/lib/modules/blockchain_connector/index.js index c118c2778..84235bd8f 100644 --- a/packages/embark/src/lib/modules/blockchain_connector/index.js +++ b/packages/embark/src/lib/modules/blockchain_connector/index.js @@ -71,7 +71,6 @@ class BlockchainConnector { this.registerWeb3Object(); this.registerEvents(); this.subscribeToPendingTransactions(); - this.addWeb3ToEmbarkJS(); } initWeb3(cb) { @@ -216,25 +215,6 @@ class BlockchainConnector { } } - addWeb3ToEmbarkJS() { - let code = ''; - code += this.fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString(); - 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', {});"; - - this.embark.addCodeToEmbarkJS(code); - - code = "EmbarkJS.Blockchain.setProvider('web3', {});"; - - const shouldInit = (_config) => { - return true; - }; - - this.embark.addConsoleProviderInit('blockchain', code, shouldInit); - } - registerEvents() { this.events.on('check:wentOffline:Ethereum', () => { this.logger.warn('Ethereum went offline: stopping web3 provider...'); diff --git a/packages/embark/src/lib/modules/code_generator/index.js b/packages/embark/src/lib/modules/code_generator/index.js index 49183b040..43fe1b41e 100644 --- a/packages/embark/src/lib/modules/code_generator/index.js +++ b/packages/embark/src/lib/modules/code_generator/index.js @@ -368,11 +368,8 @@ class CodeGenerator { } buildContractJS(contractName, contractJSON, cb) { - let contractCode = ""; - contractCode += "import web3 from 'Embark/web3';\n"; - contractCode += "import EmbarkJS from 'Embark/EmbarkJS';\n"; + let contractCode = "import EmbarkJS from 'Embark/EmbarkJS';\n"; contractCode += `let ${contractName}JSONConfig = ${JSON.stringify(contractJSON)};\n`; - contractCode += `${contractName}JSONConfig.web3 = web3;\n`; contractCode += `let ${contractName} = new EmbarkJS.Blockchain.Contract(${contractName}JSONConfig);\n`; contractCode += "export default " + contractName + ";\n"; @@ -400,6 +397,7 @@ class CodeGenerator { code += `\nimport Web3 from '${web3Location}';\n`; code += "\nglobal.Web3 = Web3;\n"; + code += "\nconsole.log('GLOBAL WEB3', global.web3);"; code += "\nif (typeof web3 === 'undefined') {"; code += "\n var web3 = new Web3();"; code += "\n}"; diff --git a/packages/embarkjs/src/blockchain.js b/packages/embarkjs/src/blockchain.js index 0b8933264..93ba8b4d6 100644 --- a/packages/embarkjs/src/blockchain.js +++ b/packages/embarkjs/src/blockchain.js @@ -214,8 +214,8 @@ let Contract = function(options) { this.gas = options.gas; this.code = '0x' + options.code; - this.web3 = options.web3; this.blockchainConnector = Blockchain.blockchainConnector; + this.web3 = this.blockchainConnector.getInstance(); ContractClass = this.blockchainConnector.newContract({abi: this.abi, address: this.address}); contracts.push(ContractClass); diff --git a/packages/web3Connector/index.js b/packages/web3Connector/index.js new file mode 100644 index 000000000..f83fd19e9 --- /dev/null +++ b/packages/web3Connector/index.js @@ -0,0 +1,24 @@ +const path = require('path'); + +module.exports = (embark) => { + embark.events.on('runcode:ready', () => { + embark.events.emit('runcode:register', '__web3Connector', require('./web3Connector'), false); + }); + + 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 new file mode 100644 index 000000000..b2a798c88 --- /dev/null +++ b/packages/web3Connector/package.json @@ -0,0 +1,25 @@ +{ + "name": "web3connector", + "version": "1.0.0", + "description": "Web3.js Connector for EmbarkJS", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "embark/packages/Web3Connector" + }, + "keywords": [ + "embark", + "web3", + "node", + "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 new file mode 100644 index 000000000..473a804f3 --- /dev/null +++ b/packages/web3Connector/web3Connector.js @@ -0,0 +1,71 @@ +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(); +}; + +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 new file mode 100644 index 000000000..aac713201 --- /dev/null +++ b/packages/web3Connector/web3ConnectorBrowser.js @@ -0,0 +1,71 @@ +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;