2018-01-10 15:43:25 +00:00
|
|
|
let async = require('async');
|
2018-07-27 18:54:25 +00:00
|
|
|
let fs = require('../../core/fs.js');
|
|
|
|
const utils = require('../../utils/utils.js');
|
2017-12-13 14:01:53 +00:00
|
|
|
|
2017-10-14 10:12:54 +00:00
|
|
|
require('ejs');
|
2017-10-14 00:01:35 +00:00
|
|
|
const Templates = {
|
|
|
|
vanilla_contract: require('./code_templates/vanilla-contract.js.ejs'),
|
|
|
|
embarkjs_contract: require('./code_templates/embarkjs-contract.js.ejs'),
|
|
|
|
exec_when_ready: require('./code_templates/exec-when-ready.js.ejs'),
|
|
|
|
load_manager: require('./code_templates/load-manager.js.ejs'),
|
|
|
|
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'),
|
2018-05-22 23:33:05 +00:00
|
|
|
exec_when_env_loaded: require('./code_templates/exec-when-env-loaded.js.ejs'),
|
|
|
|
embark_building_placeholder: require('./code_templates/embark-building-placeholder.html.ejs')
|
2017-10-14 09:24:39 +00:00
|
|
|
};
|
2017-10-14 00:01:35 +00:00
|
|
|
|
2017-08-03 23:29:09 +00:00
|
|
|
class CodeGenerator {
|
2018-07-27 18:54:25 +00:00
|
|
|
constructor(embark, options) {
|
|
|
|
this.blockchainConfig = embark.config.blockchainConfig || {};
|
2018-04-25 20:27:47 +00:00
|
|
|
this.rpcHost = this.blockchainConfig.rpcHost || '';
|
|
|
|
this.rpcPort = this.blockchainConfig.rpcPort || '';
|
2018-07-27 18:54:25 +00:00
|
|
|
this.contractsConfig = embark.config.contractsConfig || {};
|
|
|
|
this.storageConfig = embark.config.storageConfig || {};
|
|
|
|
this.communicationConfig = embark.config.communicationConfig || {};
|
|
|
|
this.namesystemConfig = embark.config.namesystemConfig || {};
|
2018-05-28 17:53:10 +00:00
|
|
|
this.env = options.env || 'development';
|
2017-03-30 11:12:39 +00:00
|
|
|
this.plugins = options.plugins;
|
2018-07-27 18:54:25 +00:00
|
|
|
this.events = embark.events;
|
|
|
|
|
|
|
|
this.listenToCommands();
|
|
|
|
|
|
|
|
const self = this;
|
|
|
|
this.events.setCommandHandler("code-generator:embarkjs:build", (cb) => {
|
|
|
|
self.buildEmbarkJS(cb);
|
|
|
|
});
|
2017-02-20 22:12:13 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 22:48:20 +00:00
|
|
|
listenToCommands() {
|
|
|
|
let self = this;
|
|
|
|
|
2018-06-01 19:48:10 +00:00
|
|
|
this.events.setCommandHandler('provider-code', function(cb) {
|
2017-12-12 17:20:57 +00:00
|
|
|
let providerCode = self.generateProvider(false);
|
|
|
|
|
|
|
|
cb(providerCode);
|
|
|
|
});
|
|
|
|
|
2017-10-17 11:03:13 +00:00
|
|
|
// new events
|
2018-06-01 19:48:10 +00:00
|
|
|
this.events.setCommandHandler('code-vanila', function(cb) {
|
2018-06-08 11:07:27 +00:00
|
|
|
self.events.request("contracts:list", (_err, contractsList) => {
|
2018-05-22 18:49:25 +00:00
|
|
|
let vanillaABI = self.generateABI(contractsList, {useEmbarkJS: false});
|
2018-05-22 18:31:21 +00:00
|
|
|
let contractsJSON = self.generateContractsJSON(contractsList);
|
|
|
|
cb(vanillaABI, contractsJSON);
|
|
|
|
});
|
2017-10-17 11:03:13 +00:00
|
|
|
});
|
|
|
|
|
2018-06-01 19:48:10 +00:00
|
|
|
this.events.setCommandHandler('code', function(cb) {
|
2018-06-08 11:07:27 +00:00
|
|
|
self.events.request("contracts:list", (_err, contractsList) => {
|
2018-05-22 18:49:25 +00:00
|
|
|
let embarkJSABI = self.generateABI(contractsList, {useEmbarkJS: true});
|
2018-05-22 18:31:21 +00:00
|
|
|
let contractsJSON = self.generateContractsJSON(contractsList);
|
|
|
|
cb(embarkJSABI, contractsJSON);
|
|
|
|
});
|
2017-10-17 11:03:13 +00:00
|
|
|
});
|
|
|
|
|
2018-06-01 19:48:10 +00:00
|
|
|
this.events.setCommandHandler('code-contracts-vanila', function(cb) {
|
2018-06-08 11:07:27 +00:00
|
|
|
self.events.request("contracts:list", (_err, contractsList) => {
|
2018-05-22 18:49:25 +00:00
|
|
|
let vanillaContractsABI = self.generateContracts(contractsList, false, true, false);
|
2018-05-22 18:31:21 +00:00
|
|
|
let contractsJSON = self.generateContractsJSON(contractsList);
|
|
|
|
cb(vanillaContractsABI, contractsJSON);
|
|
|
|
});
|
2017-10-17 11:03:13 +00:00
|
|
|
});
|
|
|
|
|
2018-06-01 19:48:10 +00:00
|
|
|
this.events.setCommandHandler('code-vanila-deployment', function(cb) {
|
2018-06-08 11:07:27 +00:00
|
|
|
self.events.request("contracts:list", (_err, contractsList) => {
|
2018-05-22 18:49:25 +00:00
|
|
|
let vanillaABI = self.generateABI(contractsList, {useEmbarkJS: false, deployment: true});
|
2018-05-22 18:31:21 +00:00
|
|
|
let contractsJSON = self.generateContractsJSON(contractsList);
|
|
|
|
cb(vanillaABI, contractsJSON);
|
|
|
|
});
|
2017-10-17 11:03:13 +00:00
|
|
|
});
|
|
|
|
|
2018-06-01 19:48:10 +00:00
|
|
|
this.events.setCommandHandler('code-generator:web3js', function(cb) {
|
2018-05-15 21:57:56 +00:00
|
|
|
self.buildWeb3JS(cb);
|
|
|
|
});
|
|
|
|
|
2018-05-22 18:31:21 +00:00
|
|
|
self.events.setCommandHandler('code-generator:contract', (contractName, cb) => {
|
2018-05-29 21:23:07 +00:00
|
|
|
self.events.request('contracts:contract', contractName, (contract) => {
|
2018-05-22 18:31:21 +00:00
|
|
|
self.buildContractJS(contractName, self.generateContractJSON(contract, contract), cb);
|
|
|
|
});
|
2018-05-16 14:00:56 +00:00
|
|
|
});
|
|
|
|
|
2018-05-23 15:16:13 +00:00
|
|
|
self.events.setCommandHandler('code-generator:contract:vanilla', (contract, gasLimit, cb) => {
|
|
|
|
cb(self.generateContractCode(contract, gasLimit));
|
|
|
|
});
|
|
|
|
|
2018-05-22 23:33:05 +00:00
|
|
|
this.events.setCommandHandler('embark-building-placeholder', (cb) => {
|
2018-05-28 22:03:33 +00:00
|
|
|
self.buildPlaceholderPage(cb);
|
2018-05-22 23:33:05 +00:00
|
|
|
});
|
|
|
|
|
2018-08-28 13:43:52 +00:00
|
|
|
self.events.setCommandHandler('code-generator:embarkjs:provider-code', (cb) => {
|
|
|
|
cb(self.getEmbarkJsProviderCode());
|
2018-08-27 15:12:28 +00:00
|
|
|
});
|
2017-07-06 22:48:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
generateProvider(isDeployment) {
|
2017-03-30 11:12:39 +00:00
|
|
|
let self = this;
|
|
|
|
let result = "";
|
|
|
|
let providerPlugins;
|
2016-12-07 02:33:31 +00:00
|
|
|
|
2017-10-14 00:01:35 +00:00
|
|
|
result += Templates.main_context();
|
|
|
|
result += Templates.load_manager();
|
|
|
|
result += Templates.define_when_env_loaded();
|
2018-08-27 15:12:28 +00:00
|
|
|
|
2018-07-09 00:57:14 +00:00
|
|
|
if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) {
|
|
|
|
return result;
|
|
|
|
}
|
2017-06-25 02:35:27 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
if (this.plugins) {
|
|
|
|
providerPlugins = this.plugins.getPluginsFor('clientWeb3Provider');
|
|
|
|
}
|
2016-08-14 12:04:34 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
if (this.plugins && providerPlugins.length > 0) {
|
2017-06-25 02:35:27 +00:00
|
|
|
providerPlugins.forEach(function(plugin) {
|
2017-03-30 11:12:39 +00:00
|
|
|
result += plugin.generateProvider(self) + "\n";
|
|
|
|
});
|
|
|
|
} else {
|
2017-10-14 00:01:35 +00:00
|
|
|
let web3Load;
|
2017-07-06 22:48:20 +00:00
|
|
|
|
2018-07-09 00:57:14 +00:00
|
|
|
if (this.contractsConfig === {} || this.contractsConfig.enabled === false) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-10-07 19:20:51 +00:00
|
|
|
if (isDeployment) {
|
2018-07-09 00:57:14 +00:00
|
|
|
let connection = utils.buildUrlFromConfig(this.contractsConfig.deployment);
|
2017-10-14 00:01:35 +00:00
|
|
|
web3Load = Templates.define_web3_simple({url: connection, done: 'done();'});
|
2017-07-06 22:48:20 +00:00
|
|
|
} else {
|
2017-10-14 00:01:35 +00:00
|
|
|
let connectionList = "[" + this.contractsConfig.dappConnection.map((x) => '"' + x + '"').join(',') + "]";
|
2018-06-29 22:00:21 +00:00
|
|
|
let isDev = (self.env === 'development');
|
|
|
|
web3Load = Templates.web3_connector({connectionList: connectionList, done: 'done(err);', warnAboutMetamask: isDev});
|
2017-10-07 19:20:51 +00:00
|
|
|
}
|
2017-07-06 22:48:20 +00:00
|
|
|
|
2018-07-18 14:49:44 +00:00
|
|
|
result += Templates.do_when_loaded({block: web3Load, environment: this.env});
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
2016-08-14 12:04:34 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
return result;
|
2017-02-20 22:12:13 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 18:49:25 +00:00
|
|
|
generateContracts(contractsList, useEmbarkJS, isDeployment, useLoader) {
|
2017-03-30 11:12:39 +00:00
|
|
|
let self = this;
|
|
|
|
let result = "\n";
|
|
|
|
let contractsPlugins;
|
|
|
|
|
2017-10-13 09:56:42 +00:00
|
|
|
if (useLoader === false) {
|
2018-05-22 18:49:25 +00:00
|
|
|
for (let contract of contractsList) {
|
2017-10-13 09:56:42 +00:00
|
|
|
let abi = JSON.stringify(contract.abiDefinition);
|
2018-05-22 18:49:25 +00:00
|
|
|
result += Templates.vanilla_contract({className: contract.className, abi: abi, contract: contract, gasLimit: 6000000});
|
2017-10-13 09:56:42 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.plugins) {
|
|
|
|
contractsPlugins = this.plugins.getPluginsFor('contractGeneration');
|
|
|
|
}
|
2016-12-07 02:33:31 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
if (this.plugins && contractsPlugins.length > 0) {
|
|
|
|
contractsPlugins.forEach(function (plugin) {
|
2018-05-22 18:49:25 +00:00
|
|
|
result += plugin.generateContracts({contracts: contractsList});
|
2017-03-30 11:12:39 +00:00
|
|
|
});
|
|
|
|
} else {
|
2018-05-22 18:49:25 +00:00
|
|
|
for (let contract of contractsList) {
|
2017-03-30 11:12:39 +00:00
|
|
|
let abi = JSON.stringify(contract.abiDefinition);
|
|
|
|
let gasEstimates = JSON.stringify(contract.gasEstimates);
|
|
|
|
|
2017-10-14 00:01:35 +00:00
|
|
|
let block = "";
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
if (useEmbarkJS) {
|
2017-10-07 19:20:51 +00:00
|
|
|
let contractAddress = contract.deployedAddress ? ("'" + contract.deployedAddress + "'") : "undefined";
|
2018-05-22 18:49:25 +00:00
|
|
|
block += Templates.embarkjs_contract({className: contract.className, abi: abi, contract: contract, contractAddress: contractAddress, gasEstimates: gasEstimates});
|
2017-03-30 11:12:39 +00:00
|
|
|
} else {
|
2018-05-22 18:49:25 +00:00
|
|
|
block += Templates.vanilla_contract({className: contract.className, abi: abi, contract: contract, gasLimit: (isDeployment ? 6000000 : false)});
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
2017-10-14 00:01:35 +00:00
|
|
|
result += Templates.exec_when_ready({block: block});
|
|
|
|
|
2016-12-07 02:33:31 +00:00
|
|
|
}
|
2016-09-23 04:31:09 +00:00
|
|
|
}
|
2017-03-30 11:12:39 +00:00
|
|
|
|
|
|
|
return result;
|
2016-08-14 12:04:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-13 16:38:10 +00:00
|
|
|
generateContractCode(contract, gasLimit) {
|
2017-12-20 16:32:11 +00:00
|
|
|
let abi = JSON.stringify(contract.abiDefinition);
|
|
|
|
|
|
|
|
let block = "";
|
2018-01-13 16:38:10 +00:00
|
|
|
block += Templates.vanilla_contract({className: contract.className, abi: abi, contract: contract, gasLimit: gasLimit});
|
2017-12-20 16:32:11 +00:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
2018-05-28 15:42:22 +00:00
|
|
|
generateNamesInitialization(useEmbarkJS) {
|
|
|
|
if (!useEmbarkJS || this.namesystemConfig === {}) return "";
|
|
|
|
|
|
|
|
let result = "\n";
|
|
|
|
result += Templates.define_when_env_loaded();
|
|
|
|
result += this._getInitCode('names', this.namesystemConfig);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
generateStorageInitialization(useEmbarkJS) {
|
2017-12-29 21:29:32 +00:00
|
|
|
if (!useEmbarkJS || this.storageConfig === {}) return "";
|
2017-02-10 12:44:06 +00:00
|
|
|
|
2017-12-29 21:29:32 +00:00
|
|
|
let result = "\n";
|
2017-10-19 22:55:49 +00:00
|
|
|
result += Templates.define_when_env_loaded();
|
2017-12-29 21:29:32 +00:00
|
|
|
result += this._getInitCode('storage', this.storageConfig);
|
2017-02-10 12:44:06 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
return result;
|
2017-02-10 12:44:06 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
generateCommunicationInitialization(useEmbarkJS) {
|
2017-12-29 21:29:32 +00:00
|
|
|
if (!useEmbarkJS || this.communicationConfig === {}) return "";
|
2017-02-20 21:29:59 +00:00
|
|
|
|
2017-12-29 21:29:32 +00:00
|
|
|
let result = "\n";
|
2017-10-14 00:01:35 +00:00
|
|
|
result += Templates.define_when_env_loaded();
|
2017-12-29 21:29:32 +00:00
|
|
|
result += this._getInitCode('communication', this.communicationConfig);
|
2017-07-24 11:27:52 +00:00
|
|
|
|
2017-12-29 21:29:32 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
_getInitCode(codeType, config) {
|
|
|
|
let result = "";
|
2017-12-28 23:10:43 +00:00
|
|
|
let pluginsWithCode = this.plugins.getPluginsFor('initCode');
|
2017-12-29 21:29:32 +00:00
|
|
|
for (let plugin of pluginsWithCode) {
|
|
|
|
let initCodes = plugin.embarkjs_init_code[codeType] || [];
|
|
|
|
for (let initCode of initCodes) {
|
|
|
|
let [block, shouldInit] = initCode;
|
|
|
|
if (shouldInit.call(plugin, config)) {
|
|
|
|
result += Templates.exec_when_env_loaded({block: block});
|
2017-12-28 23:10:43 +00:00
|
|
|
}
|
2017-06-26 19:25:22 +00:00
|
|
|
}
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
|
|
|
return result;
|
2017-02-20 21:29:59 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 18:49:25 +00:00
|
|
|
generateABI(contractsList, options) {
|
2017-03-30 11:12:39 +00:00
|
|
|
let result = "";
|
2017-02-10 12:44:06 +00:00
|
|
|
|
2017-07-06 22:48:20 +00:00
|
|
|
result += this.generateProvider(options.deployment);
|
2018-05-22 18:49:25 +00:00
|
|
|
result += this.generateContracts(contractsList, options.useEmbarkJS, options.deployment, true);
|
2017-10-09 12:59:02 +00:00
|
|
|
result += this.generateStorageInitialization(options.useEmbarkJS);
|
|
|
|
result += this.generateCommunicationInitialization(options.useEmbarkJS);
|
2018-05-28 15:42:22 +00:00
|
|
|
result += this.generateNamesInitialization(options.useEmbarkJS);
|
2016-08-18 00:29:41 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
return result;
|
|
|
|
}
|
2017-04-04 10:37:50 +00:00
|
|
|
|
2018-05-16 16:48:17 +00:00
|
|
|
generateContractJSON(className, contract) {
|
|
|
|
let contractJSON = {};
|
|
|
|
|
|
|
|
contractJSON.contract_name = className;
|
|
|
|
contractJSON.address = contract.deployedAddress;
|
|
|
|
contractJSON.code = contract.code;
|
|
|
|
contractJSON.runtime_bytecode = contract.runtimeBytecode;
|
|
|
|
contractJSON.real_runtime_bytecode = contract.realRuntimeBytecode;
|
|
|
|
contractJSON.swarm_hash = contract.swarmHash;
|
|
|
|
contractJSON.gas_estimates = contract.gasEstimates;
|
|
|
|
contractJSON.function_hashes = contract.functionHashes;
|
|
|
|
contractJSON.abi = contract.abiDefinition;
|
|
|
|
|
|
|
|
return contractJSON;
|
|
|
|
}
|
|
|
|
|
2018-05-22 18:31:21 +00:00
|
|
|
generateContractsJSON(contractsList) {
|
2017-04-04 10:37:50 +00:00
|
|
|
let contracts = {};
|
|
|
|
|
2018-05-22 18:31:21 +00:00
|
|
|
for (let contract of contractsList) {
|
|
|
|
contracts[contract.className] = this.generateContractJSON(contract.className, contract);
|
2017-04-04 10:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return contracts;
|
|
|
|
}
|
2017-12-13 14:01:53 +00:00
|
|
|
|
2018-01-10 15:43:25 +00:00
|
|
|
buildEmbarkJS(cb) {
|
|
|
|
const self = this;
|
2018-07-16 09:05:49 +00:00
|
|
|
let embarkjsCode = "import EmbarkJS from 'embarkjs';";
|
|
|
|
embarkjsCode += "\nexport default EmbarkJS;";
|
2018-07-20 01:45:17 +00:00
|
|
|
embarkjsCode += "\nglobal.EmbarkJS = EmbarkJS";
|
2017-12-13 14:01:53 +00:00
|
|
|
let code = "";
|
|
|
|
|
2018-01-10 15:43:25 +00:00
|
|
|
async.waterfall([
|
|
|
|
function getWeb3Location(next) {
|
|
|
|
self.events.request("version:get:web3", function(web3Version) {
|
|
|
|
if (web3Version === "1.0.0-beta") {
|
2018-07-11 22:04:10 +00:00
|
|
|
return next(null, fs.embarkPath("node_modules/web3"));
|
2018-01-10 15:43:25 +00:00
|
|
|
}
|
2018-08-30 13:53:04 +00:00
|
|
|
self.events.request("version:getPackageLocation", "web3", web3Version, function(err, location) {
|
|
|
|
return next(null, fs.dappPath(location));
|
|
|
|
});
|
2018-01-10 15:43:25 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function getImports(web3Location, next) {
|
2018-04-09 20:14:07 +00:00
|
|
|
web3Location = web3Location.replace(/\\/g, '/'); // Import paths must always have forward slashes
|
2018-08-31 20:49:47 +00:00
|
|
|
code += `\nimport Web3 from '${web3Location}';\n`;
|
2018-01-10 15:43:25 +00:00
|
|
|
code += "\nimport web3 from 'Embark/web3';\n";
|
2018-08-27 15:12:28 +00:00
|
|
|
code += "\nimport IpfsApi from 'ipfs-api';\n";
|
|
|
|
|
2018-01-10 15:43:25 +00:00
|
|
|
next();
|
|
|
|
},
|
|
|
|
function getJSCode(next) {
|
|
|
|
code += "\n" + embarkjsCode + "\n";
|
2017-12-13 14:01:53 +00:00
|
|
|
|
2018-08-28 13:43:52 +00:00
|
|
|
code += self.getEmbarkJsProviderCode();
|
2018-01-10 15:43:25 +00:00
|
|
|
code += self.generateCommunicationInitialization(true);
|
|
|
|
code += self.generateStorageInitialization(true);
|
2018-05-28 15:42:22 +00:00
|
|
|
code += self.generateNamesInitialization(true);
|
2018-08-27 15:12:28 +00:00
|
|
|
|
2018-01-10 15:43:25 +00:00
|
|
|
next();
|
|
|
|
},
|
|
|
|
function writeFile(next) {
|
2018-04-02 19:30:16 +00:00
|
|
|
fs.mkdirpSync(fs.dappPath(".embark"));
|
|
|
|
fs.writeFileSync(fs.dappPath(".embark", 'embark.js'), code);
|
2018-01-10 15:43:25 +00:00
|
|
|
next();
|
|
|
|
}
|
|
|
|
], function(_err, _result) {
|
|
|
|
cb();
|
|
|
|
});
|
2017-12-13 14:01:53 +00:00
|
|
|
}
|
2018-05-15 21:57:56 +00:00
|
|
|
|
2018-08-28 13:43:52 +00:00
|
|
|
getEmbarkJsProviderCode() {
|
|
|
|
return this.plugins.getPluginsFor('embarkjsCode').reduce((code, plugin) => (
|
|
|
|
code += plugin.embarkjs_code.join('\n')
|
|
|
|
), '');
|
2018-08-27 15:12:28 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 14:00:56 +00:00
|
|
|
buildContractJS(contractName, contractJSON, cb) {
|
|
|
|
let contractCode = "";
|
|
|
|
contractCode += "import web3 from 'Embark/web3';\n";
|
|
|
|
contractCode += "import EmbarkJS from 'Embark/EmbarkJS';\n";
|
2018-08-31 20:49:47 +00:00
|
|
|
contractCode += `let ${contractName}JSONConfig = ${JSON.stringify(contractJSON)};\n`;
|
|
|
|
contractCode += `${contractName}JSONConfig.web3 = web3;\n`;
|
|
|
|
contractCode += `let ${contractName} = new EmbarkJS.Blockchain.Contract(${contractName}JSONConfig);\n`;
|
2018-05-16 14:00:56 +00:00
|
|
|
|
|
|
|
contractCode += "export default " + contractName + ";\n";
|
2018-05-16 16:48:17 +00:00
|
|
|
cb(contractCode);
|
2018-05-16 14:00:56 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 21:57:56 +00:00
|
|
|
buildWeb3JS(cb) {
|
|
|
|
const self = this;
|
|
|
|
let code = "";
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
function getWeb3Location(next) {
|
|
|
|
self.events.request("version:get:web3", function(web3Version) {
|
|
|
|
if (web3Version === "1.0.0-beta") {
|
2018-07-11 22:04:10 +00:00
|
|
|
return next(null, utils.joinPath(fs.embarkPath("node_modules/web3")));
|
2018-05-15 21:57:56 +00:00
|
|
|
}
|
2018-08-30 13:53:04 +00:00
|
|
|
self.events.request("version:getPackageLocation", "web3", web3Version, function(err, location) {
|
|
|
|
return next(null, fs.dappPath(location));
|
|
|
|
});
|
2018-05-15 21:57:56 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function getImports(web3Location, next) {
|
|
|
|
web3Location = web3Location.replace(/\\/g, '/'); // Import paths must always have forward slashes
|
2018-08-31 20:49:47 +00:00
|
|
|
code += `\nimport Web3 from '${web3Location}';\n`;
|
2018-07-20 01:45:17 +00:00
|
|
|
code += "\nglobal.Web3 = Web3;\n";
|
2018-05-15 21:57:56 +00:00
|
|
|
|
2018-07-20 01:45:17 +00:00
|
|
|
code += "\n if (typeof web3 === 'undefined') {";
|
2018-05-15 21:57:56 +00:00
|
|
|
code += "\n var web3 = new Web3();\n";
|
|
|
|
code += "\n }";
|
2018-07-20 01:45:17 +00:00
|
|
|
code += "\nglobal.web3 = web3;\n";
|
2018-05-15 21:57:56 +00:00
|
|
|
|
|
|
|
let providerCode = self.generateProvider(false);
|
|
|
|
code += providerCode;
|
2018-08-29 09:22:43 +00:00
|
|
|
code += "\nexport default web3;\n";
|
2018-05-15 22:21:00 +00:00
|
|
|
next(null, code);
|
2018-05-15 21:57:56 +00:00
|
|
|
}
|
|
|
|
], cb);
|
|
|
|
}
|
|
|
|
|
2018-05-22 23:33:05 +00:00
|
|
|
buildPlaceholderPage(cb) {
|
|
|
|
let html = Templates.embark_building_placeholder({buildingMsg: __('Embark is building, please wait...')});
|
|
|
|
cb(html);
|
|
|
|
}
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
2016-08-18 00:29:41 +00:00
|
|
|
|
2017-08-03 23:29:09 +00:00
|
|
|
module.exports = CodeGenerator;
|