listen for new blocks and raise notification

This commit is contained in:
Ivan Socolsky 2015-08-04 16:10:48 -03:00
parent 362d1a42bb
commit 14a08f2e0f
2 changed files with 27 additions and 9 deletions

View File

@ -83,6 +83,7 @@ BlockchainMonitor.prototype._initExplorer = function(explorer) {
log.error('Error connecting to ' + explorer.getConnectionInfo());
});
socket.on('tx', _.bind(self._handleIncommingTx, self));
socket.on('block', _.bind(self._handleNewBlock, self));
};
BlockchainMonitor.prototype._handleIncommingTx = function(data) {
@ -112,26 +113,42 @@ BlockchainMonitor.prototype._handleIncommingTx = function(data) {
var walletId = address.walletId;
log.info('Incoming tx for wallet ' + walletId + ' [' + out.amount + 'sat -> ' + out.address + ']');
self._createNotification(walletId, data.txid, out.address, out.amount, next);
var notification = Notification.create({
type: 'NewIncomingTx',
data: {
txid: data.txid,
address: out.address,
amount: out.amount,
},
walletId: walletId,
});
self._storeAndBroadcastNotification(notification, next);
});
}, function(err) {
return;
});
};
BlockchainMonitor.prototype._createNotification = function(walletId, txid, address, amount, cb) {
BlockchainMonitor.prototype._handleNewBlock = function(hash) {
var self = this;
log.info('New block: ', hash);
var notification = Notification.create({
type: 'NewIncomingTx',
type: 'NewBlock',
data: {
txid: txid,
address: address,
amount: amount,
hash: hash,
},
walletId: walletId,
});
self.storage.storeNotification(walletId, notification, function() {
self._storeAndBroadcastNotification(notification, function(err) {
return;
});
};
BlockchainMonitor.prototype._storeAndBroadcastNotification = function(notification, cb) {
var self = this;
self.storage.storeNotification(notification.walletId, notification, function() {
self.messageBroker.send(notification)
return cb();
});

View File

@ -20,7 +20,8 @@ WsApp.prototype._unauthorized = function(socket) {
};
WsApp.prototype._handleNotification = function(notification) {
this.io.to(notification.walletId).emit('notification', notification);
var room = notification.walletId ? this.io.to(notification.walletId) : this.io;
room.emit('notification', notification);
};
WsApp.prototype.start = function(server, opts, cb) {