2018-05-18 21:15:49 +00:00
|
|
|
const Web3 = require('web3');
|
2018-05-18 20:51:03 +00:00
|
|
|
|
|
|
|
class Blockchain {
|
2018-05-18 21:15:49 +00:00
|
|
|
constructor(options) {
|
2018-05-18 20:51:03 +00:00
|
|
|
this.plugins = options.plugins;
|
|
|
|
this.logger = options.logger;
|
2018-05-18 22:31:47 +00:00
|
|
|
this.events = options.events;
|
|
|
|
this.contractsConfig = options.contractsConfig;
|
2018-05-18 21:15:49 +00:00
|
|
|
this.web3 = options.web3;
|
2018-05-18 22:31:47 +00:00
|
|
|
this.addCheck = options.addCheck;
|
2018-05-18 21:15:49 +00:00
|
|
|
|
|
|
|
if (!this.web3) {
|
|
|
|
this.initWeb3();
|
|
|
|
}
|
|
|
|
this.registerServiceCheck();
|
2018-05-18 22:31:47 +00:00
|
|
|
this.registerAccountRequests();
|
2018-05-18 21:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
initWeb3() {
|
|
|
|
this.web3 = new Web3();
|
|
|
|
if (this.contractsConfig.deployment.type === "rpc") {
|
|
|
|
let web3Endpoint = 'http://' + this.contractsConfig.deployment.host + ':' + this.contractsConfig.deployment.port;
|
|
|
|
this.web3.setProvider(new this.web3.providers.HttpProvider(web3Endpoint));
|
|
|
|
} else {
|
|
|
|
throw new Error("contracts config error: unknown deployment type " + this.contractsConfig.deployment.type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
registerServiceCheck() {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
this.addCheck('Ethereum', function (cb) {
|
|
|
|
if (self.web3.currentProvider === undefined) {
|
|
|
|
return cb({name: "No Blockchain node found", status: 'off'});
|
|
|
|
}
|
|
|
|
|
|
|
|
self.web3.eth.getAccounts(function(err, _accounts) {
|
|
|
|
if (err) {
|
|
|
|
return cb({name: "No Blockchain node found", status: 'off'});
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: web3_clientVersion method is currently not implemented in web3.js 1.0
|
|
|
|
self.web3._requestManager.send({method: 'web3_clientVersion', params: []}, (err, version) => {
|
|
|
|
if (err) {
|
|
|
|
return cb({name: "Ethereum node (version unknown)", status: 'on'});
|
|
|
|
}
|
|
|
|
if (version.indexOf("/") < 0) {
|
|
|
|
return cb({name: version, status: 'on'});
|
|
|
|
}
|
|
|
|
let nodeName = version.split("/")[0];
|
|
|
|
let versionNumber = version.split("/")[1].split("-")[0];
|
|
|
|
let name = nodeName + " " + versionNumber + " (Ethereum)";
|
|
|
|
|
|
|
|
return cb({name: name, status: 'on'});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-05-18 20:51:03 +00:00
|
|
|
}
|
2018-05-18 21:15:49 +00:00
|
|
|
|
2018-05-18 22:31:47 +00:00
|
|
|
registerAccountRequests() {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
this.events.setCommandHandler("blockchain:defaultAccount:get", function(cb) {
|
|
|
|
cb(self.defaultAccount);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.events.setCommandHandler("blockchain:defaultAccount:set", function(account, cb) {
|
|
|
|
self.setDefaultAccount(account);
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultAccount() {
|
|
|
|
return this.web3.eth.defaultAccount;
|
|
|
|
}
|
|
|
|
|
|
|
|
setDefaultAccount(account) {
|
|
|
|
this.web3.eth.defaultAccount = account;
|
|
|
|
}
|
|
|
|
|
|
|
|
getAccounts(cb) {
|
|
|
|
this.web3.eth.getAccounts(cb);
|
|
|
|
}
|
|
|
|
|
2018-05-18 22:41:04 +00:00
|
|
|
getCode(address, cb) {
|
|
|
|
this.web3.eth.getCode(address, cb);
|
|
|
|
}
|
|
|
|
|
2018-05-18 22:50:20 +00:00
|
|
|
getBlock(blockNumber, cb) {
|
|
|
|
this.web3.eth.getBlock(blockNumber, cb);
|
|
|
|
}
|
|
|
|
|
2018-05-18 23:00:36 +00:00
|
|
|
ContractObject(params) {
|
|
|
|
return new this.web3.eth.Contract(params.abi);
|
|
|
|
}
|
|
|
|
|
2018-05-19 00:26:21 +00:00
|
|
|
deployContractObject(contractObject, params) {
|
|
|
|
return contractObject.deploy({arguments: params.arguments, data: params.data});
|
|
|
|
}
|
|
|
|
|
|
|
|
estimateDeployContractGas(deployObject, cb) {
|
|
|
|
return deployObject.estimateGas().then((gasValue) => {
|
|
|
|
cb(null, gasValue);
|
|
|
|
}).catch(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
deployContractFromObject(deployContractObject, params, cb) {
|
|
|
|
deployContractObject.send({
|
|
|
|
from: params.from, gas: params.gas, gasPrice: params.gasPrice
|
|
|
|
}).on('receipt', function(receipt) {
|
|
|
|
if (receipt.contractAddress !== undefined) {
|
|
|
|
cb(null, receipt);
|
|
|
|
}
|
|
|
|
}).on('error', cb);
|
|
|
|
}
|
|
|
|
|
2018-05-18 20:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Blockchain;
|
|
|
|
|