mirror of
https://github.com/status-im/embark-area-51.git
synced 2025-02-02 10:25:21 +00:00
move web3 init to its own module
This commit is contained in:
parent
28ef2ed067
commit
4d0ebc03ad
@ -1,9 +1,60 @@
|
|||||||
|
const Web3 = require('web3');
|
||||||
|
|
||||||
class Blockchain {
|
class Blockchain {
|
||||||
constructor(optiosn) {
|
constructor(options) {
|
||||||
this.plugins = options.plugins;
|
this.plugins = options.plugins;
|
||||||
this.logger = options.logger;
|
this.logger = options.logger;
|
||||||
|
this.config = options.config;
|
||||||
|
this.contractsConfig = this.config.contractsConfig;
|
||||||
|
this.web3 = options.web3;
|
||||||
|
|
||||||
|
if (!this.web3) {
|
||||||
|
this.initWeb3();
|
||||||
|
}
|
||||||
|
this.registerServiceCheck();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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'});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Blockchain;
|
module.exports = Blockchain;
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
const Web3 = require('web3');
|
const async = require('async');
|
||||||
|
|
||||||
const Events = require('./events.js');
|
const Events = require('./events.js');
|
||||||
const Logger = require('./logger.js');
|
const Logger = require('./logger.js');
|
||||||
const Config = require('./config.js');
|
const Config = require('./config.js');
|
||||||
|
const Blockchain = require('../contracts/blockchain.js');
|
||||||
const ContractsManager = require('../contracts/contracts.js');
|
const ContractsManager = require('../contracts/contracts.js');
|
||||||
const DeployManager = require('../contracts/deploy_manager.js');
|
const DeployManager = require('../contracts/deploy_manager.js');
|
||||||
const CodeGenerator = require('../contracts/code_generator.js');
|
const CodeGenerator = require('../contracts/code_generator.js');
|
||||||
const ServicesMonitor = require('./services_monitor.js');
|
const ServicesMonitor = require('./services_monitor.js');
|
||||||
|
const Pipeline = require('../pipeline/pipeline.js');
|
||||||
const Watch = require('../pipeline/watch.js');
|
const Watch = require('../pipeline/watch.js');
|
||||||
const LibraryManager = require('../versions/library_manager.js');
|
const LibraryManager = require('../versions/library_manager.js');
|
||||||
const Pipeline = require('../pipeline/pipeline.js');
|
|
||||||
const async = require('async');
|
|
||||||
const Provider = require('../contracts/provider');
|
|
||||||
|
|
||||||
class Engine {
|
class Engine {
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
@ -273,69 +273,20 @@ class Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
web3Service(options) {
|
web3Service(options) {
|
||||||
let self = this;
|
this.blockchain = new Blockchain({
|
||||||
this.web3 = options.web3;
|
contractsConfig: this.config.contractsConfig,
|
||||||
let provider;
|
addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor),
|
||||||
if (this.web3 === undefined) {
|
events: this.events,
|
||||||
this.web3 = new Web3();
|
logger: this.logger,
|
||||||
if (this.config.contractsConfig.deployment.type === "rpc") {
|
options: options.web3 // TODO: might not be necessary
|
||||||
const web3Endpoint = 'http://' + this.config.contractsConfig.deployment.host + ':' + this.config.contractsConfig.deployment.port;
|
});
|
||||||
const providerOptions = {
|
|
||||||
web3: this.web3,
|
|
||||||
accountsConfig: this.config.contractsConfig.deployment.accounts,
|
|
||||||
logger: this.logger,
|
|
||||||
isDev: this.isDev,
|
|
||||||
web3Endpoint
|
|
||||||
};
|
|
||||||
provider = new Provider(providerOptions);
|
|
||||||
} else {
|
|
||||||
throw new Error("contracts config error: unknown deployment type " + this.config.contractsConfig.deployment.type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async.waterfall([
|
this.web3 = this.blockchain.web3;
|
||||||
function (next) {
|
|
||||||
if (!provider) {
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
provider.startWeb3Provider(next);
|
|
||||||
}
|
|
||||||
], function (err) {
|
|
||||||
if (err) {
|
|
||||||
console.error(err);
|
|
||||||
}
|
|
||||||
self.servicesMonitor.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) {
|
this.registerModule('whisper', {
|
||||||
if (err) {
|
addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor),
|
||||||
return cb({name: __("No Blockchain node found"), status: 'off'});
|
communicationConfig: this.config.communicationConfig,
|
||||||
}
|
web3: this.web3
|
||||||
|
|
||||||
// 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'});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
self.registerModule('whisper', {
|
|
||||||
addCheck: self.servicesMonitor.addCheck.bind(self.servicesMonitor),
|
|
||||||
communicationConfig: self.config.communicationConfig,
|
|
||||||
web3: self.web3
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user