embark/lib/contracts/code_generator.js

338 lines
13 KiB
JavaScript
Raw Normal View History

class CodeGenerator {
2017-03-30 11:12:39 +00:00
constructor(options) {
this.blockchainConfig = options.blockchainConfig || {};
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;
this.events = options.events;
}
listenToCommands() {
let self = this;
this.events.setCommandHandler('abi-vanila', function(cb) {
let vanillaABI = self.generateABI({useEmbarkJS: false});
let contractsJSON = self.generateContractsJSON();
cb(vanillaABI, contractsJSON);
});
this.events.setCommandHandler('abi', function(cb) {
let embarkJSABI = self.generateABI({useEmbarkJS: true});
let contractsJSON = self.generateContractsJSON();
cb(embarkJSABI, contractsJSON);
});
this.events.setCommandHandler('abi-contracts-vanila', function(cb) {
2017-10-13 09:56:42 +00:00
let vanillaContractsABI = self.generateContracts(false, true, false);
let contractsJSON = self.generateContractsJSON();
cb(vanillaContractsABI, contractsJSON);
});
this.events.setCommandHandler('abi-vanila-deployment', function(cb) {
let vanillaABI = self.generateABI({useEmbarkJS: false, deployment: true});
let contractsJSON = self.generateContractsJSON();
cb(vanillaABI, contractsJSON);
});
}
generateProvider(isDeployment) {
2017-03-30 11:12:39 +00:00
let self = this;
let result = "";
let providerPlugins;
// 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-07 19:20:51 +00:00
result += "\nfunction __reduce(arr, memo, iteratee, cb) {";
result += "\n if (typeof cb !== 'function') {";
result += "\n if (typeof memo === 'function' && typeof iteratee === 'function') {";
result += "\n cb = iteratee;";
result += "\n iteratee = memo;";
result += "\n memo = [];";
result += "\n } else {";
result += "\n throw new TypeError('expected callback to be a function');";
result += "\n }";
result += "\n }";
result += "\n";
result += "\n if (!Array.isArray(arr)) {";
result += "\n cb(new TypeError('expected an array'));";
result += "\n return;";
result += "\n }";
result += "\n";
result += "\n if (typeof iteratee !== 'function') {";
result += "\n cb(new TypeError('expected iteratee to be a function'));";
result += "\n return;";
result += "\n }";
result += "\n";
result += "\n (function next(i, acc) {";
result += "\n if (i === arr.length) {";
result += "\n cb(null, acc);";
result += "\n return;";
result += "\n }";
result += "\n";
result += "\n iteratee(acc, arr[i], function(err, val) {";
result += "\n if (err) {";
result += "\n cb(err);";
result += "\n return;";
result += "\n }";
result += "\n next(i + 1, val);";
result += "\n });";
result += "\n })(0, memo);";
result += "\n};";
2017-10-07 23:53:57 +00:00
let mainContext = "";
if (isDeployment) {
mainContext = "__mainContext.";
result += "\nvar __mainContext = __mainContext || this;";
} else {
result += "\nvar __mainContext = __mainContext || this;";
mainContext = "__mainContext.";
}
result += "\n" + mainContext + "__LoadManager = function() { this.list = []; this.done = false; }";
result += "\n" + mainContext + "__LoadManager.prototype.execWhenReady = function(cb) { if (this.done) { cb(); } else { this.list.push(cb) } }";
result += "\n" + mainContext + "__LoadManager.prototype.doFirst = function(todo) { var self = this; todo(function() { self.done = true; self.list.map((x) => x.apply()) }) }";
result += "\n" + mainContext + "__loadManagerInstance = new " + mainContext + "__LoadManager();";
2017-10-07 20:51:56 +00:00
2017-06-25 02:35:27 +00:00
result += "\nvar whenEnvIsLoaded = function(cb) {";
result += "\n if (typeof document !== 'undefined' && document !== null) {";
result += "\n document.addEventListener('DOMContentLoaded', cb);";
2017-06-25 02:35:27 +00:00
result += "\n } else {";
result += "\n cb();";
result += "\n }";
result += "\n}";
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-07 23:53:57 +00:00
result += "\nwhenEnvIsLoaded(function(){" + mainContext + "__loadManagerInstance.doFirst(function(done) {\n";
2017-10-07 19:20:51 +00:00
result += "\nif (typeof window !== 'undefined') { window.web3 = undefined; }";
2017-10-07 19:20:51 +00:00
if (isDeployment) {
let connection = "http://" + this.contractsConfig.deployment.host + ":" + this.contractsConfig.deployment.port;
result += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("' + connection + '"));';
2017-10-07 23:53:57 +00:00
result += '\n\tdone();';
} else {
2017-10-07 19:20:51 +00:00
let connectionCode = "";
connectionCode += "\n__reduce([";
connectionCode += this.contractsConfig.dappConnection.map((x) => '"' + x + '"').join(',');
connectionCode += "], function(prev, value, next) {";
connectionCode += "\nif (prev === false) {";
connectionCode += "\n return next(null, false);";
connectionCode += "\n}";
2017-10-07 19:20:51 +00:00
connectionCode += "\n if (value === '$WEB3' && (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined')) {";
connectionCode += '\n\tweb3 = new Web3(web3.currentProvider);';
connectionCode += "\n } else if (value !== '$WEB3' && (typeof Web3 !== 'undefined' && ((typeof web3 === 'undefined') || (typeof web3 !== 'undefined' && (!web3.isConnected || (web3.isConnected && !web3.isConnected())))))) {";
2017-10-09 13:11:37 +00:00
connectionCode += "\n\tweb3 = new Web3(new Web3.providers.HttpProvider(value));";
2017-10-07 19:20:51 +00:00
connectionCode += "\n}";
connectionCode += "\nelse if (value === '$WEB3') {";
connectionCode += "\n\treturn next(null, '');";
connectionCode += "\n}";
connectionCode += "\nweb3.eth.getAccounts(function(err, account) { if(err) { next(null, true) } else { next(null, false) }})";
connectionCode += "\n}, function(err, _result) {";
2017-10-08 01:02:05 +00:00
connectionCode += "\nweb3.eth.getAccounts(function(err, accounts) {;";
connectionCode += "\nweb3.eth.defaultAccount = accounts[0];";
2017-10-07 20:51:56 +00:00
connectionCode += '\ndone();';
2017-10-07 19:20:51 +00:00
connectionCode += "\n});";
2017-10-08 01:02:05 +00:00
connectionCode += "\n});";
2017-10-07 19:20:51 +00:00
result += connectionCode;
}
2017-10-07 23:53:57 +00:00
result += '\n})})';
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-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) {
let result;
for (let className in this.contractsManager.contracts) {
let contract = this.contractsManager.contracts[className];
let abi = JSON.stringify(contract.abiDefinition);
result += "\n" + className + "Abi = " + abi + ";";
result += "\n" + className + "Contract = web3.eth.contract(" + className + "Abi);";
result += "\n" + className + " = " + className + "Contract.at('" + contract.deployedAddress + "');";
}
return result;
}
2017-10-07 23:53:57 +00:00
let mainContext = "";
if (isDeployment) {
mainContext = "__mainContext.";
} else {
mainContext = "__mainContext.";
}
2017-03-30 11:12:39 +00:00
if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) {
return "";
}
if (this.plugins) {
contractsPlugins = this.plugins.getPluginsFor('contractGeneration');
}
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-06-25 02:35:27 +00:00
// TODO: refactor this
2017-10-07 20:51:56 +00:00
//result += "\nif (whenEnvIsLoaded === undefined) {";
//result += "\n var whenEnvIsLoaded = function(cb) {";
//result += "\n if (typeof document !== 'undefined' && document !== null) {";
//result += "\n document.addEventListener('DOMContentLoaded', cb);";
//result += "\n } else {";
//result += "\n cb();";
//result += "\n }";
//result += "\n }";
//result += "\n}";
2017-10-07 23:53:57 +00:00
result += "\n" + mainContext + "__loadManagerInstance.execWhenReady(function() {";
2017-10-07 19:20:51 +00:00
result += "\nif (typeof window !== 'undefined') { window." + className + " = undefined; }";
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-07 23:53:57 +00:00
result += "\n" + mainContext + "" + className + " = new EmbarkJS.Contract({abi: " + abi + ", address: " + contractAddress + ", code: '" + contract.code + "', gasEstimates: " + gasEstimates + "});";
2017-03-30 11:12:39 +00:00
} else {
result += "\n" + className + "Abi = " + abi + ";";
result += "\n" + className + "Contract = web3.eth.contract(" + className + "Abi);";
result += "\n" + className + " = " + className + "Contract.at('" + contract.deployedAddress + "');";
}
2017-06-25 02:35:27 +00:00
result += '\n});';
}
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-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-03-30 11:12:39 +00:00
if (self.storageConfig.provider === 'ipfs' && self.storageConfig.enabled === true) {
2017-07-23 12:15:40 +00:00
// TODO: make this more readable
result += "\nvar whenEnvIsLoaded = function(cb) {";
result += "\n if (typeof document !== 'undefined' && document !== null) {";
result += "\n document.addEventListener('DOMContentLoaded', cb);";
result += "\n } else {";
result += "\n cb();";
result += "\n }";
result += "\n}";
result += "\nwhenEnvIsLoaded(function() {\n";
2017-07-23 12:15:40 +00:00
result += "\nEmbarkJS.Storage.setProvider('" + self.storageConfig.provider + "', {server: '" + self.storageConfig.host + "', port: '" + self.storageConfig.port + "', getUrl: '" + self.storageConfig.getUrl + "'});";
result += '\n})';
2017-03-30 11:12:39 +00:00
}
2017-03-30 11:12:39 +00:00
return result;
}
2017-03-30 11:12:39 +00:00
generateCommunicationInitialization(useEmbarkJS) {
let self = this;
let result = "\n";
2017-03-30 11:12:39 +00:00
if (!useEmbarkJS || self.communicationConfig === {}) return "";
result += "\nvar whenEnvIsLoaded = function(cb) {";
result += "\n if (typeof document !== 'undefined' && document !== null) {";
result += "\n document.addEventListener('DOMContentLoaded', cb);";
result += "\n } else {";
result += "\n cb();";
result += "\n }";
result += "\n}";
2017-03-30 11:12:39 +00:00
if (self.communicationConfig.provider === 'whisper' && self.communicationConfig.enabled === true) {
result += "\nwhenEnvIsLoaded(function() {\n";
2017-03-30 11:12:39 +00:00
result += "\nEmbarkJS.Messages.setProvider('" + self.communicationConfig.provider + "');";
result += '\n})';
2017-03-30 11:12:39 +00:00
} else if (self.communicationConfig.provider === 'orbit' && self.communicationConfig.enabled === true) {
result += "\nwhenEnvIsLoaded(function() {\n";
2017-06-26 19:25:22 +00:00
if (self.communicationConfig.host === undefined && self.communicationConfig.port === undefined) {
result += "\nEmbarkJS.Messages.setProvider('" + self.communicationConfig.provider + "');";
} else {
result += "\nEmbarkJS.Messages.setProvider('" + self.communicationConfig.provider + "', {server: '" + self.communicationConfig.host + "', port: '" + self.communicationConfig.port + "'});";
}
result += '\n})';
2017-03-30 11:12:39 +00:00
}
2017-03-30 11:12:39 +00:00
return result;
}
2017-03-30 11:12:39 +00:00
generateABI(options) {
let result = "";
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 = {};
let abi = JSON.stringify(contract.abiDefinition);
let gasEstimates = JSON.stringify(contract.gasEstimates);
contractJSON.contract_name = className;
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-03-30 11:12:39 +00:00
}
2016-08-18 00:29:41 +00:00
module.exports = CodeGenerator;