chore: make contract artifacts usable in node

This commit is contained in:
Andre Medeiros 2019-05-21 14:38:15 -04:00 committed by Michael Bradley
parent f2903e7577
commit 0f633cd04e
10 changed files with 108 additions and 34 deletions

View File

@ -313,6 +313,7 @@ class CodeGenerator {
const self = this;
let embarkjsCode = '';
let code = "/* eslint-disable */";
const deps = ['ipfs', 'swarm', 'whisper'];
async.waterfall([
// TODO: here due to a race condition when running embark build
@ -326,7 +327,7 @@ class CodeGenerator {
function getEmbarkJsLocation(next) {
self.events.request('version:downloadIfNeeded', 'embarkjs', (err, location) => {
if (err) {
this.logger.error(__('Error downloading EmbarkJS'));
self.logger.error(__('Error downloading EmbarkJS'));
return next(err);
}
next(null, location);
@ -335,7 +336,7 @@ class CodeGenerator {
function generateSymlink(location, next) {
self.generateSymlink(location, 'embarkjs', (err, symlinkDest) => {
if (err) {
this.logger.error(__('Error creating a symlink to EmbarkJS'));
self.logger.error(__('Error creating a symlink to EmbarkJS'));
return next(err);
}
embarkjsCode += `\nconst EmbarkJS = require("${symlinkDest}").default || require("${symlinkDest}");`;
@ -344,6 +345,24 @@ class CodeGenerator {
next();
});
},
...deps.map((dep) => {
return function(next) {
self.events.request('version:downloadIfNeeded', `embarkjs-${dep}`, (err, location) => {
if (err) {
self.logger.error(__(`Error downloading embarkjs-${dep}`));
return next(err);
}
self.generateSymlink(location, `embarkjs-${dep}`, (err, _symlinkDest) => {
if (err) {
self.logger.error(__(`Error creating a symlink to embarkjs-${dep}`));
return next(err);
}
return next();
});
});
};
}),
function getJSCode(next) {
code += "\n" + embarkjsCode + "\n";
@ -372,10 +391,10 @@ class CodeGenerator {
"presets": [
[
"@babel/preset-env", {
"targets": {
"node": "8.11.3"
"targets": {
"node": "8.11.3"
}
}
}
]
]
}, (err, result) => {
@ -422,11 +441,24 @@ class CodeGenerator {
}
buildContractJS(contractName, contractJSON, cb) {
let contractCode = "import EmbarkJS from '../embarkjs';\n";
contractCode += `let ${contractName}JSONConfig = ${JSON.stringify(contractJSON)};\n`;
contractCode += `let ${contractName} = new EmbarkJS.Blockchain.Contract(${contractName}JSONConfig);\n`;
const contractCode = `
"use strict";
contractCode += "export default " + contractName + ";\n";
const isNode = (typeof process !== 'undefined' && process.versions && process.versions.node);
const lib = isNode ? '../embarkjs.node' : '../embarkjs';
const EmbarkJSNode = isNode && require('../embarkjs.node');
let EmbarkJSBrowser;
try {
EmbarkJSBrowser = require('../embarkjs').default;
} catch(e) {};
const EmbarkJS = isNode ? EmbarkJSNode : EmbarkJSBrowser;
let ${contractName}JSONConfig = ${JSON.stringify(contractJSON)};
let ${contractName} = new EmbarkJS.Blockchain.Contract(${contractName}JSONConfig);
module.exports = ${contractName};
`.trim().replace(/^[\t\s]+/gm, '');
this.generateArtifact(contractCode, contractName + '.js', constants.dappArtifacts.contractsJs, (err, path, _updated) => {
cb(err, path);

View File

@ -33,18 +33,19 @@ class VM {
require: {
builtin: ["path", "util"],
external: [
"@babel/runtime-corejs2/helpers/interopRequireDefault",
"@babel/runtime-corejs2/core-js/json/stringify",
"@babel/runtime-corejs2/core-js/promise",
"@babel/runtime-corejs2/core-js/object/assign",
"eth-ens-namehash",
"swarm-api",
"embarkjs-whisper",
"ipfs-api",
"@babel/runtime-corejs2/core-js/promise",
"@babel/runtime-corejs2/helpers/interopRequireDefault",
"embark-utils",
"embarkjs-ipfs",
"embarkjs-swarm",
"embarkjs-whisper",
"eth-ens-namehash",
"ipfs-api",
"rxjs",
"rxjs/operators",
"swarm-api",
],
},
sandbox: { __dirname: dappPath() },

View File

@ -22,6 +22,7 @@ export interface Config {
versions: {
solc: string;
};
generationDir: string;
};
plugins: Plugins;
reloadConfig(): void;

View File

@ -48,6 +48,7 @@
"dependencies": {
"@babel/runtime-corejs2": "7.3.1",
"async": "2.6.1",
"embark-core": "^4.1.0-beta.2",
"embark-i18n": "^4.1.0-beta.1",
"embark-utils": "^4.1.0-beta.2",
"embarkjs-whisper": "^4.1.0-beta.2",

View File

@ -1,11 +1,13 @@
/* global __dirname module require setTimeout */
import { __ } from 'embark-i18n';
import {canonicalHost, defaultHost} from 'embark-utils';
import {dappPath, canonicalHost, defaultHost} from 'embark-utils';
let Web3 = require('web3');
const {parallel} = require('async');
const {fromEvent} = require('rxjs');
const {map, takeUntil} = require('rxjs/operators');
const constants = require('embark-core/constants');
import * as path from 'path';
const EMBARK_RESOURCE_ORIGIN = "http://embark";
@ -24,6 +26,7 @@ class Whisper {
this.embark = embark;
this.web3Ready = false;
this.webSocketsChannels = {};
this.modulesPath = dappPath(embark.config.embarkConfig.generationDir, constants.dappArtifacts.symlinkDir);
if (embark.currentContext.includes('test') && options.node &&options.node === 'vm') {
this.logger.info(__('Whisper disabled in the tests'));
@ -123,10 +126,13 @@ class Whisper {
return;
}
let code = "";
let linkedModulePath = path.join(this.modulesPath, 'embarkjs-whisper');
if (process.platform === 'win32') linkedModulePath = linkedModulePath.replace(/\\/g, '\\\\');
code += "\nconst __embarkWhisperNewWeb3 = require('embarkjs-whisper')";
code += "\nEmbarkJS.Messages.registerProvider('whisper', __embarkWhisperNewWeb3.default || __embarkWhisperNewWeb3);";
const code = `
const __embarkWhisperNewWeb3 = EmbarkJS.isNode ? require('${linkedModulePath}') : require('embarkjs-whisper');
EmbarkJS.Messages.registerProvider('whisper', __embarkWhisperNewWeb3.default || __embarkWhisperNewWeb3);
`;
self.embark.addCodeToEmbarkJS(code);
}

View File

@ -164,7 +164,10 @@ class EmbarkController {
engine.startService("webServer");
}
engine.startService("fileWatcher");
callback();
engine.events.request('code-generator:embarkjs:build', () => {
callback();
});
},
function startDashboard(callback) {
if (!options.useDashboard) {
@ -239,7 +242,9 @@ class EmbarkController {
engine.startService('compiler');
}
callback();
engine.events.request('code-generator:embarkjs:build', () => {
callback();
});
},
function buildOrBuildAndDeploy(callback) {
if (options.onlyCompile) {
@ -323,7 +328,10 @@ class EmbarkController {
engine.startService("storage");
engine.startService("cockpit");
engine.startService("pluginCommand");
engine.events.request('blockchain:ready', callback);
engine.events.request('code-generator:embarkjs:build', () => {
engine.events.request('blockchain:ready', callback);
});
},
function ipcConnect(callback) {
// Do specific work in case we are connected to a socket:
@ -404,7 +412,10 @@ class EmbarkController {
engine.startService("compiler");
engine.startService("codeGenerator");
engine.startService("graph");
engine.events.request('contracts:build', {}, callback);
engine.events.request('code-generator:embarkjs:build', () => {
engine.events.request('contracts:build', {}, callback);
});
}
], (err) => {
if (err) {
@ -579,7 +590,10 @@ class EmbarkController {
engine.startService("deployment");
engine.startService("storage");
engine.startService("codeGenerator");
callback();
engine.events.request('code-generator:embarkjs:build', () => {
callback();
});
},
function listLoadedPlugin(callback) {
let pluginList = engine.plugins.listPlugins();
@ -656,7 +670,6 @@ class EmbarkController {
engine.init({}, callback);
},
function startServices(callback) {
engine.startService("processManager");
engine.startService("libraryManager");
engine.startService("codeRunner");
@ -674,7 +687,10 @@ class EmbarkController {
engine.startService("codeCoverage");
}
engine.startService("testRunner");
callback();
engine.events.request('code-generator:embarkjs:build', () => {
callback();
});
},
function runTests(callback) {
engine.events.request('tests:run', options, callback);

View File

@ -6,6 +6,7 @@ const IpfsApi = require('ipfs-api');
const StorageProcessesLauncher = require('../storage/storageProcessesLauncher');
const constants = require('embark-core/constants');
import { buildUrlFromConfig, dappPath, embarkPath } from 'embark-utils';
import * as path from 'path';
class IPFS {
@ -23,6 +24,7 @@ class IPFS {
this.addedToConsole = false;
this.storageProcessesLauncher = null;
this.usingRunningNode = false;
this.modulesPath = dappPath(embark.config.embarkConfig.generationDir, constants.dappArtifacts.symlinkDir);
this.webServerConfig = embark.config.webServerConfig;
this.blockchainConfig = embark.config.blockchainConfig;
@ -144,9 +146,13 @@ class IPFS {
}
this.events.emit('runcode:register', 'IpfsApi', require('ipfs-api'), () => {
let code = "";
code += "\nconst __embarkIPFS = require('embarkjs-ipfs')";
code += "\nEmbarkJS.Storage.registerProvider('ipfs', __embarkIPFS.default || __embarkIPFS);";
let linkedModulePath = path.join(this.modulesPath, 'embarkjs-ipfs');
if (process.platform === 'win32') linkedModulePath = linkedModulePath.replace(/\\/g, '\\\\');
const code = `
const __embarkIPFS = EmbarkJS.isNode ? require('${linkedModulePath}') : require('embarkjs-ipfs');
EmbarkJS.Storage.registerProvider('ipfs', __embarkIPFS.default || __embarkIPFS);
`;
this.embark.addCodeToEmbarkJS(code);
this.embark.addConsoleProviderInit("storage", code, (storageConfig) => storageConfig.enabled);

View File

@ -5,7 +5,8 @@ const SwarmAPI = require('swarm-api');
const StorageProcessesLauncher = require('../storage/storageProcessesLauncher');
const constants = require('embark-core/constants');
require('colors');
import { buildUrl } from 'embark-utils';
import { dappPath, buildUrl } from 'embark-utils';
import * as path from 'path';
class Swarm {
@ -23,6 +24,7 @@ class Swarm {
this.addedToConsole = false;
this.storageProcessesLauncher = null;
this.usingRunningNode = false;
this.modulesPath = dappPath(embark.config.embarkConfig.generationDir, constants.dappArtifacts.symlinkDir);
this.webServerConfig = embark.config.webServerConfig;
this.blockchainConfig = embark.config.blockchainConfig;
@ -123,9 +125,14 @@ class Swarm {
addProviderToEmbarkJS() {
if(this.addedToEmbarkJs) return;
this.addedToEmbarkJs = true;
let code = "";
code += "\nconst __embarkSwarm = require('embarkjs-swarm')";
code += "\nEmbarkJS.Storage.registerProvider('swarm', __embarkSwarm.default || __embarkSwarm);";
let linkedModulePath = path.join(this.modulesPath, 'embarkjs-swarm');
if (process.platform === 'win32') linkedModulePath = linkedModulePath.replace(/\\/g, '\\\\');
const code = `
const __embarkSwarm = EmbarkJS.isNode ? require('${linkedModulePath}') : require('embarkjs-swarm');
EmbarkJS.Storage.registerProvider('swarm', __embarkSwarm.default || __embarkSwarm);
`;
this.embark.addCodeToEmbarkJS(code);
}

View File

@ -195,7 +195,8 @@ Blockchain.doConnect = function(connectionList, opts, doneCb) {
// TODO find a way to share the port number
console.warn("%cNote: There is a known issue with Geth (when in `--dev` mode) that may cause transactions to get stuck. To enable a workaround, start an Embark console (run command `embark console` in your terminal) or open the dashboard in Cockpit (http://localhost:55555), then type in the console command `devtxs on`. To disable the workaround, type `devtxs off` in the console.", "font-size: 2em");
}
if ((currentProv && currentProv.isMetaMask) || (window.ethereum && window.ethereum.isMetaMask)) {
if ((currentProv && currentProv.isMetaMask) ||
(typeof window !== 'undefined' && window.ethereum && window.ethereum.isMetaMask)) {
console.warn("%cNote: Embark has detected you are in the development environment and using Metamask, please make sure Metamask is connected to your local node", "font-size: 2em");
if(opts.blockchainClient === 'parity') {
console.warn("%cNote: Parity blocks the connection from browser extensions like Metamask. To resolve this problem, go to https://embark.status.im/docs/blockchain_configuration.html#Using-Parity-and-Metamask", "font-size: 2em");

View File

@ -10,6 +10,9 @@ var EmbarkJS = {
},
enableEthereum: function () {
return Blockchain.enableEthereum();
},
get isNode() {
return typeof process !== 'undefined' && process.versions && process.versions.node;
}
};