Skip to content

Commit

Permalink
feat: support selecting what library to generate artifacts
Browse files Browse the repository at this point in the history
feat: support selecting what library to generate artifacts

feat: support selecting what library to generate artifacts

feat: support selecting what library to generate artifacts

feat: support selecting what library to generate artifacts

working web3 artifacts

remove unnecessary request

address code review issues

fixes

update tests

WIP: add index.js in packages/plugins/embarkjs/

This is a pattern established in #2285

remove comment

fix some code review issues
  • Loading branch information
iurimatias committed Mar 12, 2020
1 parent db9e959 commit ee1eb4e
Show file tree
Hide file tree
Showing 25 changed files with 169 additions and 51 deletions.
9 changes: 8 additions & 1 deletion dapps/templates/boilerplate/app/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import EmbarkJS from 'Embark/EmbarkJS';
// e.g if you have a contract named SimpleStorage:
//import SimpleStorage from 'Embark/contracts/SimpleStorage';


EmbarkJS.onReady((err) => {
// You can execute contract calls after the connection
});

// OR if using "library: 'web3'" in config/contracts.js

// import web3 from '../../embarkArtifacts/web3.js';
// import SimpleStorage from '../embarkArtifacts/contracts/SimpleStorage';
// web3.onReady(async () => {
// let accounts = await web3.eth.getAccounts();
//})
2 changes: 2 additions & 0 deletions dapps/templates/boilerplate/config/contracts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module.exports = {
// default applies to all environments
default: {
library: 'embarkjs', // can also be 'web3'

// order of connections the dapp should connect to
dappConnection: [
"$EMBARK",
Expand Down
2 changes: 2 additions & 0 deletions dapps/templates/demo/config/contracts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module.exports = {
// default applies to all environments
default: {
library: 'embarkjs', // can be also be 'web3'

// order of connections the dapp should connect to
dappConnection: [
"$EMBARK",
Expand Down
1 change: 1 addition & 0 deletions packages/core/core/src/configDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function getContractDefaults(embarkConfigVersions) {

return {
default: {
library: "embarkjs",
versions,
dappConnection: [
"$WEB3",
Expand Down
6 changes: 3 additions & 3 deletions packages/core/engine/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
{
"path": "../../plugins/debugger"
},
{
"path": "../../plugins/embarkjs"
},
{
"path": "../../plugins/ens"
},
Expand Down Expand Up @@ -88,9 +91,6 @@
{
"path": "../../stack/deployment"
},
{
"path": "../../stack/embarkjs"
},
{
"path": "../../stack/library-manager"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/embark/src/test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ describe('embark.Config', function () {
dappConnection: ['$WEB3', 'ws://localhost:8546', 'localhost:8545'],
dappAutoEnable: true,
"gas": "400000",
"library": "embarkjs",
"strategy": "implicit",
"contracts": {
"SimpleStorage": {
Expand All @@ -234,6 +235,7 @@ describe('embark.Config', function () {
dappConnection: ['$WEB3', 'ws://localhost:8546', 'localhost:8545'],
dappAutoEnable: true,
"gas": "auto",
"library": "embarkjs",
"strategy": "implicit",
"contracts": {
"SimpleStorage": {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions packages/plugins/embarkjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./dist');
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"author": "Iuri Matias <[email protected]>",
"contributors": [],
"description": "EmbarkJS APIs for Embark",
"homepage": "https://github.com/embarklabs/embark/tree/master/packages/stack/embarkjs#readme",
"homepage": "https://github.com/embarklabs/embark/tree/master/packages/plugins/embarkjs#readme",
"bugs": "https://github.com/embarklabs/embark/issues",
"keywords": [
"blockchain",
Expand All @@ -21,7 +21,7 @@
],
"license": "MIT",
"repository": {
"directory": "packages/stack/embarkjs",
"directory": "packages/plugins/embarkjs",
"type": "git",
"url": "https://github.com/embarklabs/embark.git"
},
Expand Down Expand Up @@ -50,7 +50,8 @@
"ejs": "2.6.1",
"embark-core": "^5.3.0-nightly.7",
"embark-i18n": "^5.3.0-nightly.5",
"embarkjs": "^5.3.0-nightly.6"
"embarkjs": "^5.3.0-nightly.6",
"web3": "1.2.6"
},
"devDependencies": {
"embark-solo": "^5.2.3",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {__} from 'embark-i18n';
import Web3 from 'web3';

require('ejs');
const Templates = {
Expand All @@ -9,17 +10,21 @@ const Templates = {

class EmbarkJS {

constructor(embark, _options) {
constructor(embark) {
this.embark = embark;
this.embarkConfig = embark.config.embarkConfig;
this.blockchainConfig = embark.config.blockchainConfig;
this.events = embark.events;
this.logger = embark.logger;
this.config = embark.config;
this.contractArtifacts = {};
this.enabled = true;

this.events.request("runcode:whitelist", 'embarkjs', () => {
this.registerEmbarkJS();
});
// note: since other plugins like ens currently expect these command handlers to exist
// we used a condition instead just returning immediatly so that the handlers still exist
if (!this.config.blockchainConfig.enabled || this.config.contractsConfig.library !== 'embarkjs') {
this.enabled = false;
}

this.embarkJSPlugins = {};
this.customEmbarkJSPlugins = {};
Expand All @@ -46,11 +51,61 @@ class EmbarkJS {
this.events.setCommandHandler("embarkjs:contract:generate", this.addContractArtifact.bind(this));
this.events.setCommandHandler("embarkjs:contract:runInVm", this.runInVm.bind(this));

if (!this.enabled) return;

this.events.request("runcode:whitelist", 'embarkjs', () => {
this.registerEmbarkJS();
});

embark.registerActionForEvent("pipeline:generateAll:before", this.addEmbarkJSArtifact.bind(this));
embark.registerActionForEvent("pipeline:generateAll:before", this.addContractIndexArtifact.bind(this));

this.setupEmbarkJS();

embark.registerActionForEvent("deployment:contract:deployed", {priority: 40}, this.registerInVm.bind(this));
embark.registerActionForEvent("deployment:contract:undeployed", this.registerInVm.bind(this));
embark.registerActionForEvent("deployment:contract:deployed", this.registerArtifact.bind(this));
embark.registerActionForEvent("deployment:contract:undeployed", this.registerArtifact.bind(this));
}

async setupEmbarkJS() {
this.events.on("blockchain:started", async () => {
await this.registerWeb3Object();
this.events.request("embarkjs:console:setProvider", 'blockchain', 'web3', '{web3}');
});
this.events.request("embarkjs:plugin:register", 'blockchain', 'web3', 'embarkjs-web3');
await this.events.request2("embarkjs:console:register", 'blockchain', 'web3', 'embarkjs-web3');
}

async registerWeb3Object() {
if (!this.enabled) return;
const provider = await this.events.request2("blockchain:client:provider", "ethereum");
const web3 = new Web3(provider);
this.events.request("runcode:whitelist", 'web3', () => {});
await this.events.request2("runcode:register", 'web3', web3);
const accounts = await web3.eth.getAccounts();
if (accounts.length) {
await this.events.request2('runcode:eval', `web3.eth.defaultAccount = '${accounts[0]}'`);
}

this.events.request('console:register:helpCmd', {
cmdName: "web3",
cmdHelp: __("instantiated web3.js object configured to the current environment")
}, () => {});
}

async registerInVm(params, cb) {
if (!this.enabled) return;
this.events.request("embarkjs:contract:runInVm", params.contract, cb);
}

registerArtifact(params, cb) {
if (!this.enabled) return;
this.events.request("embarkjs:contract:generate", params.contract, cb);
}

async registerEmbarkJS() {
if (!this.enabled) return;
const checkEmbarkJS = `return (typeof EmbarkJS === 'undefined');`;
const embarkJSNotDefined = await this.events.request2('runcode:eval', checkEmbarkJS);

Expand All @@ -59,6 +114,7 @@ class EmbarkJS {
}

async registerEmbarkJSPlugin(stackName, pluginName, packageName, cb) {
if (!this.enabled) return cb();
await this.registerEmbarkJS();

let moduleName = stackName;
Expand All @@ -77,6 +133,7 @@ class EmbarkJS {
}

async registerCustomEmbarkJSPluginInVm(stackName, pluginName, packageName, options, cb) {
if (!this.enabled) return cb();
await this.registerEmbarkJS();

const customPluginCode = `
Expand All @@ -92,6 +149,7 @@ class EmbarkJS {
}

addEmbarkJSArtifact(_params, cb) {
if (!this.enabled) return cb();
const embarkjsCode = Templates.embarkjs_artifact({
plugins: this.embarkJSPlugins,
hasWebserver: this.embark.config.webServerConfig.enabled,
Expand All @@ -108,6 +166,7 @@ class EmbarkJS {
}

async setProvider(stackName, pluginName, config) {
if (!this.enabled) return;
let moduleName = stackName;
if (moduleName === 'messages') moduleName = 'Messages';
if (moduleName === 'storage') moduleName = 'Storage';
Expand Down Expand Up @@ -136,6 +195,7 @@ class EmbarkJS {
}

async addContractArtifact(contract, cb) {
if (!this.enabled) return cb();
const abi = JSON.stringify(contract.abiDefinition);
const gasLimit = 6000000;
this.contractArtifacts[contract.className] = contract.className + '.js';
Expand All @@ -151,6 +211,7 @@ class EmbarkJS {
}

async addContractIndexArtifact(_options, cb) {
if (!this.enabled) return cb();
let indexCode = 'module.exports = {';
Object.keys(this.contractArtifacts).forEach(className => {
indexCode += `\n"${className}": require('./${this.contractArtifacts[className]}').default,`;
Expand All @@ -166,6 +227,7 @@ class EmbarkJS {
}

async runInVm(contract, cb) {
if (!this.enabled) return cb();
const abi = contract.abiDefinition;
const gasLimit = 6000000;
const provider = await this.events.request2("blockchain:client:provider", "ethereum");
Expand Down
File renamed without changes.
6 changes: 0 additions & 6 deletions packages/plugins/specialconfigs/src/functionConfigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,6 @@ class FunctionConfigs {
// TODO: for this to work correctly we need to add a default from address to the contract
if (contract.deploy === false) continue;
// eslint-disable-next-line no-await-in-loop
const contractRegisteredInVM = await this.checkContractRegisteredInVM(contract);
if (!contractRegisteredInVM) {
// eslint-disable-next-line no-await-in-loop
await this.events.request2("embarkjs:contract:runInVm", contract);
}
// eslint-disable-next-line no-await-in-loop
let contractInstance = await this.events.request2("runcode:eval", contract.className);
args.contracts[contract.className] = contractInstance;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<%- className %>Abi = <%- abi %>;
<%- className %>Config = <%- JSON.stringify(contract) %>;
<%- className %>Abi = <%- JSON.stringify(abi) %>;
<%- className %> = new web3.eth.Contract(<%- className %>Abi);
<%- className %>.options.address = '<%- contract.deployedAddress %>';
<%- className %>.address = '<%- contract.deployedAddress %>';
Expand Down
4 changes: 2 additions & 2 deletions packages/plugins/web3/src/contract-artifact.js.ejs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const web3 = require("./web3_init.js").default
const web3 = require("../web3.js").default

let <%- className %>Abi = <%- abi %>;
let <%- className %> = new web3.eth.Contract(<%- className %>Abi);
<%- className %>.options.address = '<%- contract.deployedAddress %>';
<%- className %>.address = '<%- contract.deployedAddress %>';
<%- className %>.options.from = web3.eth.defaultAccount;

web3.execWhenReady(() => {
web3.onReady(() => {
<%- className %>.options.from = web3.eth.defaultAccount;
if (!<%- className %>.currentProvider) {
<%- className %>.setProvider(web3.currentProvider);
Expand Down
6 changes: 0 additions & 6 deletions packages/plugins/web3/src/embarkjs-contract-artifact.js.ejs

This file was deleted.

Loading

0 comments on commit ee1eb4e

Please sign in to comment.