embark/lib/contracts/abi.js

167 lines
5.9 KiB
JavaScript
Raw Normal View History

2017-03-30 11:12:39 +00:00
class ABIGenerator {
constructor(options) {
this.blockchainConfig = options.blockchainConfig || {};
this.storageConfig = options.storageConfig || {};
this.communicationConfig = options.communicationConfig || {};
this.contractsManager = options.contractsManager;
this.rpcHost = options.blockchainConfig && options.blockchainConfig.rpcHost;
this.rpcPort = options.blockchainConfig && options.blockchainConfig.rpcPort;
this.plugins = options.plugins;
}
2017-03-30 11:12:39 +00:00
generateProvider() {
let self = this;
let result = "";
let providerPlugins;
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-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-06-25 02:35:27 +00:00
result += "\nwhenEnvIsLoaded(function() {";
2017-03-30 11:12:39 +00:00
result += "\nif (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {";
result += '\n\tweb3 = new Web3(web3.currentProvider);';
result += "\n} else if (typeof Web3 !== 'undefined') {";
result += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("http://' + this.rpcHost + ':' + this.rpcPort + '"));';
result += '\n}';
result += "\nweb3.eth.defaultAccount = web3.eth.accounts[0];";
2017-06-25 02:35:27 +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-03-30 11:12:39 +00:00
generateContracts(useEmbarkJS) {
let self = this;
let result = "\n";
let contractsPlugins;
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
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 }";
2017-06-25 02:35:27 +00:00
result += "\n }";
result += "\n}";
result += "\nwhenEnvIsLoaded(function() {";
2017-03-30 11:12:39 +00:00
if (useEmbarkJS) {
result += "\n" + className + " = new EmbarkJS.Contract({abi: " + abi + ", address: '" + contract.deployedAddress + "', code: '" + contract.code + "', gasEstimates: " + gasEstimates + "});";
} 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) {
result += "\nEmbarkJS.Storage.setProvider('" + self.storageConfig.provider + "', {server: '" + self.storageConfig.host + "', port: '" + self.storageConfig.port + "'});";
}
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 "";
2017-03-30 11:12:39 +00:00
if (self.communicationConfig.provider === 'whisper' && self.communicationConfig.enabled === true) {
result += "\nEmbarkJS.Messages.setProvider('" + self.communicationConfig.provider + "');";
} else if (self.communicationConfig.provider === 'orbit' && self.communicationConfig.enabled === true) {
result += "\nEmbarkJS.Messages.setProvider('" + self.communicationConfig.provider + "', {server: '" + self.communicationConfig.host + "', port: '" + self.communicationConfig.port + "'});";
}
2017-03-30 11:12:39 +00:00
return result;
}
2017-03-30 11:12:39 +00:00
generateABI(options) {
let result = "";
2017-03-30 11:12:39 +00:00
result += this.generateProvider();
result += this.generateContracts(options.useEmbarkJS);
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.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
2016-08-14 12:04:34 +00:00
module.exports = ABIGenerator;