-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbalmon.js
More file actions
39 lines (33 loc) · 1.26 KB
/
balmon.js
File metadata and controls
39 lines (33 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// balance monitoring.
// there are approximately three possible ways to do it:
// 1. scan new transactions in the blockchain looking for those which touch our addresses
// 2. get notification from some service which monitors the blockchain
// 3. batch mode, do updates periodically
//
// We will use chromanode notification mechanism for now, but at some point it becomes more efficient
// to do the blockchain monitoring ourselves.
var core = require('./core')
function BalanceMonitor (storage, connector) {
this.storage = storage
this.connector = connector
}
BalanceMonitor.prototype.updateAddressBalance = function (address, subscribe) {
var self = this
if (subscribe) {
self.connector.subscribe({event: "touchAddress", address: address})
}
return core.computeAddressBalance(address)
.then(function (balance) {
return self.storage.updateAddressBalance(address, balance)
})
}
BalanceMonitor.prototype.init = function () {
var self = this
this.connector.on('touchAddress', function (address, txid) {
self.updateAddressBalance(address)
})
this.storage.getAllAddresses().map(function (address) {
return self.updateAddressBalance(address, true)
}, {concurrency: 1})
}
module.exports = BalanceMonitor