listen for new blocks and raise notification
This commit is contained in:
parent
362d1a42bb
commit
14a08f2e0f
|
@ -83,6 +83,7 @@ BlockchainMonitor.prototype._initExplorer = function(explorer) {
|
||||||
log.error('Error connecting to ' + explorer.getConnectionInfo());
|
log.error('Error connecting to ' + explorer.getConnectionInfo());
|
||||||
});
|
});
|
||||||
socket.on('tx', _.bind(self._handleIncommingTx, self));
|
socket.on('tx', _.bind(self._handleIncommingTx, self));
|
||||||
|
socket.on('block', _.bind(self._handleNewBlock, self));
|
||||||
};
|
};
|
||||||
|
|
||||||
BlockchainMonitor.prototype._handleIncommingTx = function(data) {
|
BlockchainMonitor.prototype._handleIncommingTx = function(data) {
|
||||||
|
@ -112,26 +113,42 @@ BlockchainMonitor.prototype._handleIncommingTx = function(data) {
|
||||||
|
|
||||||
var walletId = address.walletId;
|
var walletId = address.walletId;
|
||||||
log.info('Incoming tx for wallet ' + walletId + ' [' + out.amount + 'sat -> ' + out.address + ']');
|
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) {
|
}, function(err) {
|
||||||
return;
|
return;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
BlockchainMonitor.prototype._createNotification = function(walletId, txid, address, amount, cb) {
|
BlockchainMonitor.prototype._handleNewBlock = function(hash) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
|
log.info('New block: ', hash);
|
||||||
var notification = Notification.create({
|
var notification = Notification.create({
|
||||||
type: 'NewIncomingTx',
|
type: 'NewBlock',
|
||||||
data: {
|
data: {
|
||||||
txid: txid,
|
hash: hash,
|
||||||
address: address,
|
|
||||||
amount: amount,
|
|
||||||
},
|
},
|
||||||
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)
|
self.messageBroker.send(notification)
|
||||||
return cb();
|
return cb();
|
||||||
});
|
});
|
||||||
|
|
|
@ -20,7 +20,8 @@ WsApp.prototype._unauthorized = function(socket) {
|
||||||
};
|
};
|
||||||
|
|
||||||
WsApp.prototype._handleNotification = function(notification) {
|
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) {
|
WsApp.prototype.start = function(server, opts, cb) {
|
||||||
|
|
Loading…
Reference in New Issue