feat(embarkjs/blockchain): remove dependency on web3instance.js

This commit is contained in:
Jonathan Rainville 2019-02-11 10:35:28 -05:00 committed by Iuri Matias
parent fdd8ad5321
commit bd9fc6683a
7 changed files with 194 additions and 25 deletions

View File

@ -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...');

View File

@ -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}";

View File

@ -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);

View File

@ -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);
};

View File

@ -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"
}

View File

@ -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;

View File

@ -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;