-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpoll.js
72 lines (69 loc) · 2.01 KB
/
poll.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var https = require('https');
function resultObject(){
this.balance = 0;
this.unconfirmed_balance = 0;
this.final_balance = 0;
this.n_tx = 0;
this.tx = [];
this.getNumberOfConfirmationsForTx = function getNumberOfConfirmationsForTx(index){
return this.tx[index].confirmations;
}
this.getTxHash = function getTxHash(index){
return this.tx[index].hash;
}
this.getTxValue = function getTxValue(index){
return this.tx[index].value;
}
}
function parseContent(content, resolve) {
content = JSON.parse(content);
if(content.final_n_tx == 0){
resolve(0);
return;
}
if(content.error){
resolve({error: content.error});
}
var returnObject = new resultObject();
returnObject.balance = content.balance;
returnObject.unconfirmed_balance = content.unconfirmed_balance;
returnObject.final_balance = content.final_balance;
returnObject.n_tx = content.final_n_tx;
var transactions = content.txs;
for(var i = 0; i < content.final_n_tx; i++){
var txObj = {};
txObj.confirmations = transactions[i].confirmations;
txObj.hash = transactions[i].hash;
// Iterate over all transaction outputs for that transaction
var txOutputs = transactions[i].outputs;
for(var j = 0; j < txOutputs.length; j++){
// Iterate over all addresses involved in that output
var addresses = txOutputs[j].addresses;
for(var k = 0; k < addresses.length; k++){
if(addresses[k] == content.address){
txObj.value = txOutputs[j].value; // In satoshis
}
}
}
returnObject.tx.push(txObj);
}
resolve(returnObject);
}
module.exports = function (requestObject) {
return new Promise(function(resolve, reject){
var reqGet = https.request(requestObject, function (res) {
var content = "";
res.on('data', function (chunk) {
content += chunk;
});
res.on('end', function(){
parseContent(content, resolve);
});
});
reqGet.end();
reqGet.on('error', function (e) {
console.error(e);
reject(e);
});
});
}