mirror of
https://github.com/embarklabs/embark.git
synced 2025-01-11 14:24:24 +00:00
feat(web3-connector): convert web3connector to class and add connect
This commit is contained in:
parent
a0d336e49e
commit
7eceaf6c0a
@ -1,82 +1,59 @@
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const fs = require('fs');
|
|
||||||
|
|
||||||
function whenRuncodeReady(embark) {
|
class EmbarkJSWeb3Connector {
|
||||||
return new Promise((resolve) => {
|
constructor(embark, _options) {
|
||||||
embark.events.on('runcode:ready', () => {
|
this.embark = embark;
|
||||||
resolve();
|
this.events = embark.events;
|
||||||
});
|
this.fs = embark.fs;
|
||||||
});
|
this.config = embark.config;
|
||||||
}
|
this.constants = embark.constants;
|
||||||
|
this.registerProvider();
|
||||||
|
}
|
||||||
|
|
||||||
function getWeb3Location(embark) {
|
async registerProvider() {
|
||||||
return new Promise((resolve, reject) => {
|
let blockchainConnectorReady = false;
|
||||||
embark.events.request("version:get:web3", (web3Version) => {
|
await this.whenRuncodeReady();
|
||||||
if (web3Version === "1.0.0-beta") {
|
|
||||||
const nodePath = embark.embarkPath('node_modules');
|
const web3LocationPromise = this.getWeb3Location();
|
||||||
const web3Path = require.resolve("web3", {paths: [nodePath]});
|
|
||||||
return resolve(web3Path);
|
this.events.setCommandHandler('blockchain:connector:ready', (cb) => {
|
||||||
|
if (blockchainConnectorReady) {
|
||||||
|
return cb();
|
||||||
}
|
}
|
||||||
embark.events.request("version:getPackageLocation", "web3", web3Version, (err, location) => {
|
this.events.once("blockchain:connector:ready", () => {
|
||||||
if (err) {
|
cb();
|
||||||
return reject(err);
|
|
||||||
}
|
|
||||||
const locationPath = embark.embarkPath(location);
|
|
||||||
resolve(locationPath);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function generateSymlink(embark, location) {
|
web3LocationPromise.then((_web3Location) => {
|
||||||
return new Promise((resolve, reject) => {
|
blockchainConnectorReady = true;
|
||||||
embark.events.request('code-generator:symlink:generate', location, 'web3', (err, symlinkDest) => {
|
this.events.emit('blockchain:connector:ready');
|
||||||
if (err) {
|
|
||||||
return reject(err);
|
|
||||||
}
|
|
||||||
resolve(symlinkDest);
|
|
||||||
});
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = async (embark) => {
|
let web3Location = await web3LocationPromise;
|
||||||
let blockchainConnectorReady = false;
|
web3Location = web3Location.replace(/\\/g, '/');
|
||||||
await whenRuncodeReady(embark);
|
|
||||||
|
|
||||||
const web3LocationPromise = getWeb3Location(embark);
|
await this.registerVar('__Web3', require(web3Location));
|
||||||
|
|
||||||
embark.events.setCommandHandler('blockchain:connector:ready', (cb) => {
|
const symlinkLocation = await this.generateSymlink(web3Location);
|
||||||
if (blockchainConnectorReady) {
|
|
||||||
return cb();
|
|
||||||
}
|
|
||||||
embark.events.once("blockchain:connector:ready", () => {
|
|
||||||
cb();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
web3LocationPromise.then((_web3Location) => {
|
|
||||||
blockchainConnectorReady = true;
|
|
||||||
embark.events.emit('blockchain:connector:ready');
|
|
||||||
});
|
|
||||||
|
|
||||||
let web3Location = await web3LocationPromise;
|
|
||||||
|
|
||||||
web3Location = web3Location.replace(/\\/g, '/');
|
|
||||||
|
|
||||||
embark.events.emit('runcode:register', '__Web3', require(web3Location), async () => {
|
|
||||||
const symlinkLocation = await generateSymlink(embark, web3Location);
|
|
||||||
|
|
||||||
let code = `\nconst Web3 = global.__Web3 || require('${symlinkLocation}');`;
|
let code = `\nconst Web3 = global.__Web3 || require('${symlinkLocation}');`;
|
||||||
code += `\nglobal.Web3 = Web3;`;
|
code += `\nglobal.Web3 = Web3;`;
|
||||||
|
|
||||||
const connectorCode = fs.readFileSync(path.join(__dirname, 'embarkJSWeb3Connector.js'), 'utf8');
|
const connectorCode = this.fs.readFileSync(path.join(__dirname, 'embarkJSWeb3Connector.js'), 'utf8');
|
||||||
code += connectorCode;
|
code += connectorCode;
|
||||||
|
|
||||||
code += "\nEmbarkJS.Blockchain.registerProvider('web3', embarkJSWeb3Connector);";
|
code += "\nEmbarkJS.Blockchain.registerProvider('web3', embarkJSWeb3Connector);";
|
||||||
|
|
||||||
code += "\nEmbarkJS.Blockchain.setProvider('web3', {});";
|
code += "\nEmbarkJS.Blockchain.setProvider('web3', {});";
|
||||||
|
|
||||||
embark.addCodeToEmbarkJS(code);
|
const configPath = this.fs.dappPath(this.config.embarkConfig.generationDir, this.constants.dappArtifacts.dir, this.constants.dappArtifacts.blockchain).replace(/\\/g, '/');
|
||||||
|
|
||||||
|
code += `\nif (!global.__Web3) {`; // Only connect when in the Dapp
|
||||||
|
code += `\n const web3ConnectionConfig = require('${configPath}');`;
|
||||||
|
code += `\n EmbarkJS.Blockchain.connect(web3ConnectionConfig, (err) => {if (err) { console.error(err); } });`;
|
||||||
|
code += `\n}`;
|
||||||
|
|
||||||
|
this.embark.addCodeToEmbarkJS(code);
|
||||||
|
|
||||||
code = "EmbarkJS.Blockchain.setProvider('web3', {web3});";
|
code = "EmbarkJS.Blockchain.setProvider('web3', {web3});";
|
||||||
|
|
||||||
@ -84,6 +61,54 @@ module.exports = async (embark) => {
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
embark.addConsoleProviderInit('blockchain', code, shouldInit);
|
this.embark.addConsoleProviderInit('blockchain', code, shouldInit);
|
||||||
});
|
}
|
||||||
};
|
|
||||||
|
whenRuncodeReady() {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
this.events.on('runcode:ready', () => {
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
getWeb3Location() {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.events.request("version:get:web3", (web3Version) => {
|
||||||
|
if (web3Version === "1.0.0-beta") {
|
||||||
|
const nodePath = this.fs.embarkPath('node_modules');
|
||||||
|
const web3Path = require.resolve("web3", {paths: [nodePath]});
|
||||||
|
return resolve(web3Path);
|
||||||
|
}
|
||||||
|
this.events.request("version:getPackageLocation", "web3", web3Version, (err, location) => {
|
||||||
|
if (err) {
|
||||||
|
return reject(err);
|
||||||
|
}
|
||||||
|
const locationPath = this.fs.embarkPath(location);
|
||||||
|
resolve(locationPath);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
generateSymlink(location) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.events.request('code-generator:symlink:generate', location, 'web3', (err, symlinkDest) => {
|
||||||
|
if (err) {
|
||||||
|
return reject(err);
|
||||||
|
}
|
||||||
|
resolve(symlinkDest);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
registerVar(name, code) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
this.events.emit('runcode:register', name, code, () => {
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = EmbarkJSWeb3Connector;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user