refactor bcmonitor
This commit is contained in:
parent
cf8106eb3f
commit
86525ce6ef
|
@ -2,14 +2,19 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
var _ = require('lodash');
|
||||
var log = require('npmlog');
|
||||
log.debug = log.verbose;
|
||||
|
||||
var config = require('../config');
|
||||
var BlockchainExplorer = require('../lib/blockchainmonitor');
|
||||
var BlockchainMonitor = require('../lib/blockchainmonitor');
|
||||
|
||||
BlockchainExplorer.start(config, function(err) {
|
||||
var bcm = new BlockchainMonitor();
|
||||
bcm.start(config, function(err) {
|
||||
if (err) throw err;
|
||||
|
||||
console.log('Blockchain monitor started');
|
||||
_.each(bcm.explorers, function(explorer) {
|
||||
console.log('\t' + explorer.network.name + ': ' + explorer.url);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -12,39 +12,41 @@ var MessageBroker = require('./messagebroker');
|
|||
|
||||
var Notification = require('./model/notification');
|
||||
|
||||
var storage, messageBroker;
|
||||
|
||||
function BlockchainMonitor() {};
|
||||
|
||||
BlockchainMonitor.start = function(opts, cb) {
|
||||
BlockchainMonitor.prototype.start = function(opts, cb) {
|
||||
opts = opts || {};
|
||||
$.checkArgument(opts.blockchainExplorerOpts);
|
||||
$.checkArgument(opts.storageOpts);
|
||||
|
||||
var self = this;
|
||||
|
||||
async.parallel([
|
||||
|
||||
function(done) {
|
||||
_.each(['livenet', 'testnet'], function(network) {
|
||||
self.explorers = _.map(['livenet', 'testnet'], function(network) {
|
||||
var config = opts.blockchainExplorerOpts[network] || {};
|
||||
BlockchainMonitor._initExplorer(config.provider, network, config.url);
|
||||
return self._initExplorer(config.provider, network, config.url);
|
||||
});
|
||||
done();
|
||||
},
|
||||
function(done) {
|
||||
storage = new Storage();
|
||||
storage.connect(opts.storageOpts, done);
|
||||
self.storage = new Storage();
|
||||
self.storage.connect(opts.storageOpts, done);
|
||||
},
|
||||
function(done) {
|
||||
messageBroker = new MessageBroker(opts.messageBrokerOpts);
|
||||
self.messageBroker = new MessageBroker(opts.messageBrokerOpts);
|
||||
done();
|
||||
},
|
||||
], cb);
|
||||
|
||||
};
|
||||
|
||||
BlockchainMonitor._initExplorer = function(provider, network, url) {
|
||||
BlockchainMonitor.prototype._initExplorer = function(provider, network, url) {
|
||||
$.checkArgument(provider == 'insight', 'Blockchain monitor ' + provider + ' not supported');
|
||||
|
||||
var self = this;
|
||||
|
||||
var explorer = new BlockchainExplorer({
|
||||
provider: provider,
|
||||
network: network,
|
||||
|
@ -53,10 +55,14 @@ BlockchainMonitor._initExplorer = function(provider, network, url) {
|
|||
|
||||
var socket = explorer.initSocket();
|
||||
socket.emit('subscribe', 'inv');
|
||||
socket.on('tx', BlockchainMonitor._handleIncommingTx);
|
||||
socket.on('tx', _.bind(self._handleIncommingTx, self));
|
||||
|
||||
return explorer;
|
||||
};
|
||||
|
||||
BlockchainMonitor._handleIncommingTx = function(data) {
|
||||
BlockchainMonitor.prototype._handleIncommingTx = function(data) {
|
||||
var self = this;
|
||||
|
||||
if (!data || !data.vout) return;
|
||||
|
||||
var outs = _.compact(_.map(data.vout, function(v) {
|
||||
|
@ -71,20 +77,22 @@ BlockchainMonitor._handleIncommingTx = function(data) {
|
|||
if (_.isEmpty(outs)) return;
|
||||
|
||||
async.each(outs, function(out, next) {
|
||||
storage.fetchAddress(out.address, function(err, address) {
|
||||
self.storage.fetchAddress(out.address, function(err, address) {
|
||||
if (err || !address) return next(err);
|
||||
if (address.isChange) return next();
|
||||
|
||||
var walletId = address.walletId;
|
||||
log.info('Incoming tx for wallet ' + walletId + ' (' + out.address + ' -> ' + out.amount + ')');
|
||||
BlockchainMonitor._createNotification(walletId, data.txid, out.address, out.amount, next);
|
||||
log.info('Incoming tx for wallet ' + walletId + ' [' + out.amount + 'sat -> ' + out.address + ']');
|
||||
self._createNotification(walletId, data.txid, out.address, out.amount, next);
|
||||
});
|
||||
}, function(err) {
|
||||
return;
|
||||
});
|
||||
};
|
||||
|
||||
BlockchainMonitor._createNotification = function(walletId, txid, address, amount, cb) {
|
||||
BlockchainMonitor.prototype._createNotification = function(walletId, txid, address, amount, cb) {
|
||||
var self = this;
|
||||
|
||||
var n = Notification.create({
|
||||
type: 'NewIncomingTx',
|
||||
data: {
|
||||
|
@ -94,8 +102,8 @@ BlockchainMonitor._createNotification = function(walletId, txid, address, amount
|
|||
},
|
||||
walletId: walletId,
|
||||
});
|
||||
storage.storeNotification(walletId, n, function() {
|
||||
messageBroker.send(n)
|
||||
self.storage.storeNotification(walletId, n, function() {
|
||||
self.messageBroker.send(n)
|
||||
return cb();
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue