Browse Source

Merge pull request #309 from isocolsky/block_event

Listen for new blocks and raise notification
activeAddress
Matias Alejo Garcia 10 years ago
parent
commit
f9a33c3c93
  1. 33
      lib/blockchainmonitor.js
  2. 3
      lib/wsapp.js

33
lib/blockchainmonitor.js

@ -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();
});

3
lib/wsapp.js

@ -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) {

Loading…
Cancel
Save