feat(generattion): remove web3 generation to let EmbarkJS handle it

This commit is contained in:
Jonathan Rainville 2019-01-28 08:25:03 -05:00 committed by Iuri Matias
parent 52aebebf9e
commit 4023392ea9
7 changed files with 43 additions and 75 deletions

View File

@ -486,7 +486,8 @@ Config.prototype.loadEmbarkConfigFile = function() {
"optimize": true,
"optimize-runs": 200
}
}
},
"generationDir": "embarkArtifacts"
};
this.embarkConfig = utils.recursiveMerge(configObject, this.embarkConfig);

View File

@ -1,4 +0,0 @@
EmbarkJS.Blockchain.autoEnable = <%= autoEnable %>;
EmbarkJS.Blockchain.connect(<%- connectionList %>, {warnAboutMetamask: <%= warnAboutMetamask %>, blockchainClient: "<%= blockchainClient %>"}, function(err) {
<%- done %>
});

View File

@ -12,7 +12,6 @@ const Templates = {
define_when_env_loaded: require('./code_templates/define-when-env-loaded.js.ejs'),
main_context: require('./code_templates/main-context.js.ejs'),
define_web3_simple: require('./code_templates/define-web3-simple.js.ejs'),
web3_connector: require('./code_templates/web3-connector.js.ejs'),
do_when_loaded: require('./code_templates/do-when-loaded.js.ejs'),
exec_when_env_loaded: require('./code_templates/exec-when-env-loaded.js.ejs')
};
@ -49,12 +48,6 @@ class CodeGenerator {
this.generateConfigs(contractConfig);
});
this.events.setCommandHandler('provider-code', function(cb) {
let providerCode = self.generateProvider(false);
cb(providerCode);
});
this.events.setCommandHandler('code', function(cb) {
self.events.request("contracts:list", (_err, contractsList) => {
let embarkJSABI = self.generateABI(contractsList, {useEmbarkJS: true});
@ -98,55 +91,6 @@ class CodeGenerator {
});
}
generateProvider(isDeployment) {
let self = this;
let result = "";
let providerPlugins;
result += Templates.main_context();
result += Templates.load_manager();
result += Templates.define_when_env_loaded();
if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) {
return result;
}
if (this.plugins) {
providerPlugins = this.plugins.getPluginsFor('clientWeb3Provider');
}
if (this.plugins && providerPlugins.length > 0) {
providerPlugins.forEach(function(plugin) {
result += plugin.generateProvider(self) + "\n";
});
} else {
let web3Load;
if (this.contractsConfig === {} || this.contractsConfig.enabled === false) {
return result;
}
if (isDeployment) {
let connection = utils.buildUrlFromConfig(this.contractsConfig.deployment);
web3Load = Templates.define_web3_simple({url: connection, done: 'done();'});
} else {
let connectionList = "[" + this.contractsConfig.dappConnection.map((x) => '"' + x + '"').join(',') + "]";
let isDev = self.blockchainConfig.isDev;
web3Load = Templates.web3_connector({
autoEnable: this.contractsConfig.dappAutoEnable,
connectionList: connectionList,
done: 'done(err);',
warnAboutMetamask: isDev || false,
blockchainClient: this.blockchainConfig.ethereumClientName
});
}
result += Templates.do_when_loaded({block: web3Load, environment: this.env});
}
return result;
}
generateContracts(contractsList, useEmbarkJS, isDeployment, useLoader) {
let self = this;
let result = "\n";
@ -193,15 +137,37 @@ class CodeGenerator {
return result;
}
checkIfNeedsUpdate(file, newOutput, callback) {
fs.readFile(file, (err, content) => {
if (err) {
return callback(null, true);
}
callback(null, content.toString() !== newOutput);
});
}
generateConfigs(contractConfig) {
this.dappConfigs.blockchain = {dappConnection: contractConfig.dappConnection};
this.dappConfigs.blockchain = {
dappConnection: contractConfig.dappConnection,
dappAutoEnable: contractConfig.dappAutoEnable,
warnIfMetamask: this.blockchainConfig.isDev,
blockchainClient: this.blockchainConfig.ethereumClientName
};
const dir = utils.joinPath(this.embarkConfig.generationDir, constants.dappConfig.dir);
const filePath = utils.joinPath(dir, constants.dappConfig.blockchain);
const configString = JSON.stringify(this.dappConfigs.blockchain, null, 2);
async.waterfall([
(next) => {
fs.mkdirp(utils.joinPath(this.embarkConfig.buildDir, constants.dappConfig.dir), next);
fs.mkdirp(dir, next);
},
(_dir, next) => {
fs.writeFile(utils.joinPath(this.embarkConfig.buildDir, constants.dappConfig.dir, constants.dappConfig.blockchain),
JSON.stringify(this.dappConfigs.blockchain, null, 2), next);
this.checkIfNeedsUpdate(filePath, configString, next);
},
(needsUpdate, next) => {
if (!needsUpdate) {
return next();
}
fs.writeFile(filePath, configString, next);
}
], (err) => {
if (err) {
@ -274,7 +240,6 @@ class CodeGenerator {
generateABI(contractsList, options) {
let result = "";
result += this.generateProvider(options.deployment);
result += this.generateContracts(contractsList, options.useEmbarkJS, options.deployment, true);
result += this.generateStorageInitialization(options.useEmbarkJS);
result += this.generateCommunicationInitialization(options.useEmbarkJS);
@ -419,12 +384,10 @@ class CodeGenerator {
code += `\nimport Web3 from '${web3Location}';\n`;
code += "\nglobal.Web3 = Web3;\n";
code += "\n if (typeof web3 === 'undefined') {";
code += "\n var web3 = new Web3();\n";
code += "\n }";
code += "\nif (typeof web3 === 'undefined') {";
code += "\n var web3 = new Web3();";
code += "\n}";
let providerCode = self.generateProvider(false);
code += providerCode;
code += "\nexport default web3;\n";
next(null, code);
}

View File

@ -1,6 +1,11 @@
import EmbarkJS from 'Embark/EmbarkJS';
import config from '../../embarkArtifacts/config/blockchain.json';
// import your contracts
// e.g if you have a contract named SimpleStorage:
//import SimpleStorage from 'Embark/contracts/SimpleStorage';
EmbarkJS.Blockchain.connect(config, (err) => {
// You can execute contract calls after the connection
});

View File

@ -20,5 +20,6 @@
"optimize": true,
"optimize-runs": 200
}
}
},
"generationDir": "embarkArtifacts"
}

View File

@ -7,6 +7,7 @@ import Blockchain from './components/blockchain';
import Whisper from './components/whisper';
import Storage from './components/storage';
import ENS from './components/ens';
import config from '../embarkArtifacts/config/blockchain';
import './dapp.css';
@ -27,7 +28,7 @@ class App extends React.Component {
}
componentDidMount() {
EmbarkJS.onReady((err) => {
EmbarkJS.Blockchain.connect(config, (err) => {
this.setState({blockchainEnabled: true});
if (err) {
// If err is not null then it means something went wrong connecting to ethereum

View File

@ -19,5 +19,6 @@
"optimize": true,
"optimize-runs": 200
}
}
},
"generationDir": "embarkArtifacts"
}