Skip to content

Commit

Permalink
feat(embarkjs/blockchain): remove dependency on web3instance.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jrainville authored and iurimatias committed Feb 12, 2019
1 parent fdd8ad5 commit bd9fc66
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 25 deletions.
20 changes: 0 additions & 20 deletions packages/embark/src/lib/modules/blockchain_connector/index.js
Expand Up @@ -71,7 +71,6 @@ class BlockchainConnector {
this.registerWeb3Object();
this.registerEvents();
this.subscribeToPendingTransactions();
this.addWeb3ToEmbarkJS();
}

initWeb3(cb) {
Expand Down Expand Up @@ -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...');
Expand Down
6 changes: 2 additions & 4 deletions packages/embark/src/lib/modules/code_generator/index.js
Expand Up @@ -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";
Expand Down Expand Up @@ -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}";
Expand Down
2 changes: 1 addition & 1 deletion packages/embarkjs/src/blockchain.js
Expand Up @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions 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);
};
25 changes: 25 additions & 0 deletions 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"
}
71 changes: 71 additions & 0 deletions 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;
71 changes: 71 additions & 0 deletions 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;

0 comments on commit bd9fc66

Please sign in to comment.