2017-12-13 14:01:53 +00:00
|
|
|
let fs = require('../core/fs.js');
|
|
|
|
var utils = require('../utils/utils.js');
|
|
|
|
|
2017-10-14 10:12:54 +00:00
|
|
|
require('ejs');
|
2017-10-14 00:01:35 +00:00
|
|
|
const Templates = {
|
|
|
|
utils: require('./code_templates/utils.js.ejs'),
|
|
|
|
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'),
|
|
|
|
exec_when_env_loaded: require('./code_templates/exec-when-env-loaded.js.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 {
|
2017-03-30 11:12:39 +00:00
|
|
|
constructor(options) {
|
|
|
|
this.blockchainConfig = options.blockchainConfig || {};
|
2017-07-06 22:48:20 +00:00
|
|
|
this.contractsConfig = options.contractsConfig || {};
|
2017-03-30 11:12:39 +00:00
|
|
|
this.storageConfig = options.storageConfig || {};
|
|
|
|
this.communicationConfig = options.communicationConfig || {};
|
|
|
|
this.contractsManager = options.contractsManager;
|
|
|
|
this.plugins = options.plugins;
|
2017-07-06 22:48:20 +00:00
|
|
|
this.events = options.events;
|
2017-02-20 22:12:13 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 22:48:20 +00:00
|
|
|
listenToCommands() {
|
|
|
|
let self = this;
|
|
|
|
|
2017-12-12 17:20:57 +00:00
|
|
|
this.events.setCommandHandlerOnce('provider-code', function(cb) {
|
|
|
|
let providerCode = self.generateProvider(false);
|
|
|
|
|
|
|
|
cb(providerCode);
|
|
|
|
});
|
|
|
|
|
2017-10-17 11:03:13 +00:00
|
|
|
// deprecated events; to remove in embark 2.7.0
|
2017-10-21 18:21:18 +00:00
|
|
|
this.events.setCommandHandlerOnce('abi-vanila', function(cb) {
|
2017-07-06 22:48:20 +00:00
|
|
|
let vanillaABI = self.generateABI({useEmbarkJS: false});
|
|
|
|
let contractsJSON = self.generateContractsJSON();
|
|
|
|
|
|
|
|
cb(vanillaABI, contractsJSON);
|
|
|
|
});
|
|
|
|
|
2017-10-21 18:21:18 +00:00
|
|
|
this.events.setCommandHandlerOnce('abi', function(cb) {
|
2017-07-06 22:48:20 +00:00
|
|
|
let embarkJSABI = self.generateABI({useEmbarkJS: true});
|
|
|
|
let contractsJSON = self.generateContractsJSON();
|
|
|
|
|
|
|
|
cb(embarkJSABI, contractsJSON);
|
|
|
|
});
|
|
|
|
|
2017-10-21 18:21:18 +00:00
|
|
|
this.events.setCommandHandlerOnce('abi-contracts-vanila', function(cb) {
|
2017-10-13 09:56:42 +00:00
|
|
|
let vanillaContractsABI = self.generateContracts(false, true, false);
|
2017-07-06 22:48:20 +00:00
|
|
|
let contractsJSON = self.generateContractsJSON();
|
|
|
|
|
|
|
|
cb(vanillaContractsABI, contractsJSON);
|
|
|
|
});
|
|
|
|
|
2017-10-21 18:21:18 +00:00
|
|
|
this.events.setCommandHandlerOnce('abi-vanila-deployment', function(cb) {
|
2017-07-06 22:48:20 +00:00
|
|
|
let vanillaABI = self.generateABI({useEmbarkJS: false, deployment: true});
|
|
|
|
let contractsJSON = self.generateContractsJSON();
|
|
|
|
|
|
|
|
cb(vanillaABI, contractsJSON);
|
|
|
|
});
|
2017-10-17 11:03:13 +00:00
|
|
|
|
|
|
|
// new events
|
2017-10-21 18:21:18 +00:00
|
|
|
this.events.setCommandHandlerOnce('code-vanila', function(cb) {
|
2017-10-17 11:03:13 +00:00
|
|
|
let vanillaABI = self.generateABI({useEmbarkJS: false});
|
|
|
|
let contractsJSON = self.generateContractsJSON();
|
|
|
|
|
|
|
|
cb(vanillaABI, contractsJSON);
|
|
|
|
});
|
|
|
|
|
2017-10-21 18:21:18 +00:00
|
|
|
this.events.setCommandHandlerOnce('code', function(cb) {
|
2017-10-17 11:03:13 +00:00
|
|
|
let embarkJSABI = self.generateABI({useEmbarkJS: true});
|
|
|
|
let contractsJSON = self.generateContractsJSON();
|
|
|
|
|
|
|
|
cb(embarkJSABI, contractsJSON);
|
|
|
|
});
|
|
|
|
|
2017-10-21 18:21:18 +00:00
|
|
|
this.events.setCommandHandlerOnce('code-contracts-vanila', function(cb) {
|
2017-10-17 11:03:13 +00:00
|
|
|
let vanillaContractsABI = self.generateContracts(false, true, false);
|
|
|
|
let contractsJSON = self.generateContractsJSON();
|
|
|
|
|
|
|
|
cb(vanillaContractsABI, contractsJSON);
|
|
|
|
});
|
|
|
|
|
2017-10-21 18:21:18 +00:00
|
|
|
this.events.setCommandHandlerOnce('code-vanila-deployment', function(cb) {
|
2017-10-17 11:03:13 +00:00
|
|
|
let vanillaABI = self.generateABI({useEmbarkJS: false, deployment: true});
|
|
|
|
let contractsJSON = self.generateContractsJSON();
|
|
|
|
|
|
|
|
cb(vanillaABI, contractsJSON);
|
|
|
|
});
|
|
|
|
|
2017-07-06 22:48:20 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 20:50:42 +00:00
|
|
|
generateContext() {
|
|
|
|
let result = "";
|
|
|
|
result += Templates.main_context();
|
|
|
|
result += Templates.load_manager();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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-07-06 22:48:20 +00:00
|
|
|
// TODO: check contractsConfig for enabled
|
2017-03-30 11:12:39 +00:00
|
|
|
if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) {
|
|
|
|
return "";
|
|
|
|
}
|
2016-08-14 12:04:34 +00:00
|
|
|
|
2017-10-14 00:01:35 +00:00
|
|
|
result += Templates.utils();
|
2017-10-07 23:53:57 +00:00
|
|
|
|
2017-10-14 00:01:35 +00:00
|
|
|
result += Templates.main_context();
|
|
|
|
result += Templates.load_manager();
|
|
|
|
result += Templates.define_when_env_loaded();
|
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-07-06 22:48:20 +00:00
|
|
|
|
2017-10-14 00:01:35 +00:00
|
|
|
let web3Load;
|
2017-07-06 22:48:20 +00:00
|
|
|
|
2017-10-07 19:20:51 +00:00
|
|
|
if (isDeployment) {
|
2017-07-06 22:48:20 +00:00
|
|
|
let connection = "http://" + this.contractsConfig.deployment.host + ":" + this.contractsConfig.deployment.port;
|
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(',') + "]";
|
|
|
|
web3Load = Templates.web3_connector({connectionList: connectionList, done: 'done();'});
|
2017-10-07 19:20:51 +00:00
|
|
|
}
|
2017-07-06 22:48:20 +00:00
|
|
|
|
2017-10-14 00:01:35 +00:00
|
|
|
result += Templates.do_when_loaded({block: web3Load});
|
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
|
|
|
}
|
|
|
|
|
2017-10-13 09:56:42 +00:00
|
|
|
generateContracts(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) {
|
|
|
|
for (let className in this.contractsManager.contracts) {
|
|
|
|
let contract = this.contractsManager.contracts[className];
|
|
|
|
let abi = JSON.stringify(contract.abiDefinition);
|
2017-10-14 00:01:35 +00:00
|
|
|
result += Templates.vanilla_contract({className: className, abi: abi, contract: contract});
|
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) {
|
|
|
|
result += plugin.generateContracts({contracts: self.contractsManager.contracts});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
for (let className in this.contractsManager.contracts) {
|
|
|
|
let contract = this.contractsManager.contracts[className];
|
|
|
|
|
|
|
|
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";
|
2017-10-14 00:01:35 +00:00
|
|
|
block += Templates.embarkjs_contract({className: className, abi: abi, contract: contract, contractAddress: contractAddress, gasEstimates: gasEstimates});
|
2017-03-30 11:12:39 +00:00
|
|
|
} else {
|
2017-10-14 00:01:35 +00:00
|
|
|
block += Templates.vanilla_contract({className: className, abi: abi, contract: contract});
|
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
|
|
|
}
|
|
|
|
|
2017-12-20 16:32:11 +00:00
|
|
|
generateContractCode(contract) {
|
|
|
|
let abi = JSON.stringify(contract.abiDefinition);
|
|
|
|
|
|
|
|
let block = "";
|
|
|
|
block += Templates.vanilla_contract({className: contract.className, abi: abi, contract: contract});
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
generateStorageInitialization(useEmbarkJS) {
|
|
|
|
let self = this;
|
|
|
|
let result = "\n";
|
2016-08-14 12:04:34 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
if (!useEmbarkJS || self.storageConfig === {}) return "";
|
2017-02-10 12:44:06 +00:00
|
|
|
|
2017-10-19 22:55:49 +00:00
|
|
|
result += Templates.define_when_env_loaded();
|
|
|
|
|
2017-12-28 22:42:25 +00:00
|
|
|
let pluginsWithCode = this.plugins.getPluginsFor('initCode');
|
|
|
|
if (pluginsWithCode.length > 0) {
|
|
|
|
for (let plugin of pluginsWithCode) {
|
2017-12-28 23:10:43 +00:00
|
|
|
let initCodes = plugin.embarkjs_init_code.storage || [];
|
2017-12-28 22:42:25 +00:00
|
|
|
for (let initCode of initCodes) {
|
|
|
|
let [block, shouldInit] = initCode;
|
|
|
|
if (shouldInit.call(plugin, self.storageConfig)) {
|
|
|
|
result += Templates.exec_when_env_loaded({block: block});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
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) {
|
|
|
|
let self = this;
|
|
|
|
let result = "\n";
|
2017-02-10 12:44:06 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
if (!useEmbarkJS || self.communicationConfig === {}) return "";
|
2017-02-20 21:29:59 +00:00
|
|
|
|
2017-10-14 00:01:35 +00:00
|
|
|
// TODO: don't repeat this twice; should have 'requirements' generator first
|
|
|
|
result += Templates.define_when_env_loaded();
|
2017-07-24 11:27:52 +00:00
|
|
|
|
2017-12-28 23:10:43 +00:00
|
|
|
let pluginsWithCode = this.plugins.getPluginsFor('initCode');
|
|
|
|
if (pluginsWithCode.length > 0) {
|
|
|
|
for (let plugin of pluginsWithCode) {
|
|
|
|
let initCodes = plugin.embarkjs_init_code.communication || [];
|
|
|
|
for (let initCode of initCodes) {
|
|
|
|
let [block, shouldInit] = initCode;
|
|
|
|
if (shouldInit.call(plugin, self.communicationConfig)) {
|
|
|
|
result += Templates.exec_when_env_loaded({block: block});
|
|
|
|
}
|
|
|
|
}
|
2017-06-26 19:25:22 +00:00
|
|
|
}
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
2017-12-28 23:10:43 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
return result;
|
2017-02-20 21:29:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
generateABI(options) {
|
|
|
|
let result = "";
|
2017-02-10 12:44:06 +00:00
|
|
|
|
2017-07-06 22:48:20 +00:00
|
|
|
result += this.generateProvider(options.deployment);
|
2017-10-13 09:56:42 +00:00
|
|
|
result += this.generateContracts(options.useEmbarkJS, options.deployment, true);
|
2017-10-09 12:59:02 +00:00
|
|
|
result += this.generateStorageInitialization(options.useEmbarkJS);
|
|
|
|
result += this.generateCommunicationInitialization(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
|
|
|
|
|
|
|
generateContractsJSON() {
|
|
|
|
let contracts = {};
|
|
|
|
|
|
|
|
for (let className in this.contractsManager.contracts) {
|
|
|
|
let contract = this.contractsManager.contracts[className];
|
|
|
|
let contractJSON = {};
|
|
|
|
|
|
|
|
contractJSON.contract_name = className;
|
2017-07-02 02:04:29 +00:00
|
|
|
contractJSON.address = contract.deployedAddress;
|
2017-04-04 10:37:50 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
contracts[className] = contractJSON;
|
|
|
|
}
|
|
|
|
|
|
|
|
return contracts;
|
|
|
|
}
|
2017-12-13 14:01:53 +00:00
|
|
|
|
|
|
|
buildEmbarkJS() {
|
|
|
|
let embarkjsCode = fs.readFileSync(fs.embarkPath('js/embark.js')).toString();
|
|
|
|
let code = "";
|
|
|
|
|
|
|
|
code += "\nimport Web3 from '" + utils.joinPath(fs.embarkPath("js/web3-1.0.min.js")) + "'\n";
|
|
|
|
code += "\nimport web3 from 'Embark/web3';\n";
|
|
|
|
|
|
|
|
code += "\n" + embarkjsCode + "\n";
|
|
|
|
|
2017-12-28 17:16:50 +00:00
|
|
|
let pluginsWithCode = this.plugins.getPluginsFor('embarkjsCode');
|
|
|
|
if (pluginsWithCode.length > 0) {
|
|
|
|
for (let plugin of pluginsWithCode) {
|
|
|
|
code += plugin.embarkjs_code.join('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-28 02:04:12 +00:00
|
|
|
//code += "\n" + fs.readFileSync(fs.embarkPath('js/embarkjs/orbit.js')).toString();
|
2017-12-28 00:10:05 +00:00
|
|
|
|
2017-12-13 14:01:53 +00:00
|
|
|
code += this.generateCommunicationInitialization(true);
|
|
|
|
code += this.generateStorageInitialization(true);
|
|
|
|
|
|
|
|
let filePath = utils.joinPath(fs.dappPath(), ".embark", 'embark.js');
|
|
|
|
fs.mkdirpSync(utils.joinPath(fs.dappPath(), ".embark"));
|
|
|
|
fs.writeFileSync(filePath, code);
|
|
|
|
}
|
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;
|