2016-08-14 12:04:34 +00:00
|
|
|
|
2016-12-07 02:33:31 +00:00
|
|
|
var ABIGenerator = function(options) {
|
|
|
|
this.blockchainConfig = options.blockchainConfig || {};
|
2017-02-10 12:44:06 +00:00
|
|
|
this.storageConfig = options.storageConfig || {};
|
2017-02-20 22:12:13 +00:00
|
|
|
this.communicationConfig = options.communicationConfig || {};
|
2016-12-07 02:33:31 +00:00
|
|
|
this.contractsManager = options.contractsManager;
|
2017-02-06 11:42:58 +00:00
|
|
|
this.rpcHost = options.blockchainConfig && options.blockchainConfig.rpcHost;
|
|
|
|
this.rpcPort = options.blockchainConfig && options.blockchainConfig.rpcPort;
|
2017-02-18 14:09:36 +00:00
|
|
|
this.plugins = options.plugins;
|
2016-08-14 12:04:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ABIGenerator.prototype.generateProvider = function() {
|
2016-12-07 02:33:31 +00:00
|
|
|
var self = this;
|
2016-08-14 12:04:34 +00:00
|
|
|
var result = "";
|
2017-02-18 14:39:13 +00:00
|
|
|
var providerPlugins;
|
2016-08-14 12:04:34 +00:00
|
|
|
|
2017-02-20 22:12:13 +00:00
|
|
|
if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2017-02-18 14:09:36 +00:00
|
|
|
if (this.plugins) {
|
2017-02-18 14:39:13 +00:00
|
|
|
providerPlugins = this.plugins.getPluginsFor('clientWeb3Provider');
|
2017-02-18 14:09:36 +00:00
|
|
|
}
|
2016-12-07 02:33:31 +00:00
|
|
|
|
2017-02-18 14:09:36 +00:00
|
|
|
if (this.plugins && providerPlugins.length > 0) {
|
2016-12-07 02:33:31 +00:00
|
|
|
providerPlugins.forEach(function(plugin) {
|
2017-01-16 10:27:25 +00:00
|
|
|
result += plugin.generateProvider(self) + "\n";
|
2016-12-07 02:33:31 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
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];";
|
|
|
|
}
|
2016-08-14 12:04:34 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2016-09-23 04:31:09 +00:00
|
|
|
ABIGenerator.prototype.generateContracts = function(useEmbarkJS) {
|
2016-12-07 02:33:31 +00:00
|
|
|
var self = this;
|
2016-08-14 12:04:34 +00:00
|
|
|
var result = "\n";
|
2017-02-18 14:39:13 +00:00
|
|
|
var contractsPlugins;
|
2016-08-14 12:04:34 +00:00
|
|
|
|
2017-02-20 22:12:13 +00:00
|
|
|
if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2017-02-18 14:09:36 +00:00
|
|
|
if (this.plugins) {
|
2017-02-18 14:39:13 +00:00
|
|
|
contractsPlugins = this.plugins.getPluginsFor('contractGeneration');
|
2017-02-18 14:09:36 +00:00
|
|
|
}
|
2016-12-07 02:33:31 +00:00
|
|
|
|
2017-02-18 14:09:36 +00:00
|
|
|
if (this.plugins && contractsPlugins.length > 0) {
|
2016-12-07 02:33:31 +00:00
|
|
|
contractsPlugins.forEach(function(plugin) {
|
|
|
|
result += plugin.generateContracts({contracts: self.contractsManager.contracts});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
for(var className in this.contractsManager.contracts) {
|
|
|
|
var contract = this.contractsManager.contracts[className];
|
2016-08-14 12:04:34 +00:00
|
|
|
|
2016-12-07 02:33:31 +00:00
|
|
|
var abi = JSON.stringify(contract.abiDefinition);
|
|
|
|
var gasEstimates = JSON.stringify(contract.gasEstimates);
|
2016-08-14 12:04:34 +00:00
|
|
|
|
2016-12-07 02:33:31 +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 + "');";
|
|
|
|
}
|
2016-09-23 04:31:09 +00:00
|
|
|
}
|
2016-08-14 12:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2017-02-20 21:29:59 +00:00
|
|
|
ABIGenerator.prototype.generateStorageInitialization = function(useEmbarkJS) {
|
2017-02-10 12:44:06 +00:00
|
|
|
var self = this;
|
|
|
|
var result = "\n";
|
|
|
|
|
2017-02-20 22:12:13 +00:00
|
|
|
if (!useEmbarkJS || self.storageConfig === {}) return "";
|
2017-02-10 12:44:06 +00:00
|
|
|
|
|
|
|
if (self.storageConfig.provider === 'ipfs' && self.storageConfig.enabled === true) {
|
2017-02-24 00:49:48 +00:00
|
|
|
result += "\nEmbarkJS.Storage.setProvider('" + self.storageConfig.provider + "', {server: '" + self.storageConfig.host + "', port: '" + self.storageConfig.port + "'});";
|
2017-02-10 12:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2017-02-20 21:29:59 +00:00
|
|
|
ABIGenerator.prototype.generateCommunicationInitialization = function(useEmbarkJS) {
|
|
|
|
var self = this;
|
|
|
|
var result = "\n";
|
|
|
|
|
2017-02-20 22:12:13 +00:00
|
|
|
if (!useEmbarkJS || self.communicationConfig === {}) return "";
|
2017-02-20 21:29:59 +00:00
|
|
|
|
|
|
|
if (self.communicationConfig.provider === 'ipfs' && self.communicationConfig.enabled === true) {
|
2017-02-24 00:49:48 +00:00
|
|
|
result += "\nEmbarkJS.Storage.setProvider('" + self.communicationConfig.provider + "');";
|
2017-02-20 21:29:59 +00:00
|
|
|
} else if (self.communicationConfig.provider === 'orbit' && self.communicationConfig.enabled === true) {
|
2017-02-24 00:49:48 +00:00
|
|
|
result += "\nEmbarkJS.Storage.setProvider('" + self.communicationConfig.provider + "', {server: '" + self.communicationConfig.host + "', port: '" + self.communicationConfig.port + "'});";
|
2017-02-20 21:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
2017-02-10 12:44:06 +00:00
|
|
|
|
2016-09-23 04:31:09 +00:00
|
|
|
ABIGenerator.prototype.generateABI = function(options) {
|
2016-08-18 00:29:41 +00:00
|
|
|
var result = "";
|
|
|
|
|
|
|
|
result += this.generateProvider();
|
2016-09-23 04:31:09 +00:00
|
|
|
result += this.generateContracts(options.useEmbarkJS);
|
2017-02-20 21:29:59 +00:00
|
|
|
result += this.generateStorageInitialization(options.useEmbarkJS);
|
|
|
|
result += this.generateCommunicationInitialization(options.useEmbarkJS);
|
2016-08-18 00:29:41 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2016-08-14 12:04:34 +00:00
|
|
|
module.exports = ABIGenerator;
|