embark-area-51/lib/abi.js

53 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-08-14 12:04:34 +00:00
var ABIGenerator = function(blockchainConfig, contractsManager) {
this.blockchainConfig = blockchainConfig;
2016-08-14 12:04:34 +00:00
this.contractsManager = contractsManager;
this.rpcHost = blockchainConfig.rpcHost;
this.rpcPort = blockchainConfig.rpcPort;
2016-08-14 12:04:34 +00:00
};
ABIGenerator.prototype.generateProvider = function() {
var result = "";
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];";
return result;
};
2016-09-23 04:31:09 +00:00
ABIGenerator.prototype.generateContracts = function(useEmbarkJS) {
2016-08-14 12:04:34 +00:00
var result = "\n";
for(var className in this.contractsManager.contracts) {
var contract = this.contractsManager.contracts[className];
var abi = JSON.stringify(contract.abiDefinition);
var gasEstimates = JSON.stringify(contract.gasEstimates);
2016-08-14 12:04:34 +00:00
2016-09-23 04:31:09 +00:00
if (useEmbarkJS) {
result += "\n" + className + " = new EmbarkJS.Contract({abi: " + abi + ", address: '" + contract.deployedAddress + "', code: '" + contract.code + "', gasEstimates: " + gasEstimates + "});";
2016-09-23 04:31:09 +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 + "');";
}
2016-08-14 12:04:34 +00:00
}
return result;
};
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);
2016-08-18 00:29:41 +00:00
return result;
};
2016-08-14 12:04:34 +00:00
module.exports = ABIGenerator;