-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
explorer.js
395 lines (367 loc) · 12.3 KB
/
explorer.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
var request = require('request')
, settings = require('./settings')
, Address = require('../models/address');
var base_url = 'http://127.0.0.1:' + settings.port + '/api/';
// returns coinbase total sent as current coin supply
function coinbase_supply(cb) {
Address.findOne({a_id: 'coinbase'}, function(err, address) {
if (address) {
return cb(address.sent);
} else {
return cb();
}
});
}
module.exports = {
convert_to_satoshi: function(amount, cb) {
// fix to 8dp & convert to string
var fixed = amount.toFixed(8).toString();
// remove decimal (.) and return integer
return cb(parseInt(fixed.replace('.', '')));
},
get_hashrate: function(cb) {
if (settings.index.show_hashrate == false) return cb('-');
if (settings.nethash == 'netmhashps') {
var uri = base_url + 'getmininginfo';
request({uri: uri, json: true}, function (error, response, body) { //returned in mhash
if (body.netmhashps) {
if (settings.nethash_units == 'K') {
return cb((body.netmhashps * 1000).toFixed(4));
} else if (settings.nethash_units == 'G') {
return cb((body.netmhashps / 1000).toFixed(4));
} else if (settings.nethash_units == 'H') {
return cb((body.netmhashps * 1000000).toFixed(4));
} else if (settings.nethash_units == 'T') {
return cb((body.netmhashps / 1000000).toFixed(4));
} else if (settings.nethash_units == 'P') {
return cb((body.netmhashps / 1000000000).toFixed(4));
} else {
return cb(body.netmhashps.toFixed(4));
}
} else {
return cb('-');
}
});
} else {
var uri = base_url + 'getnetworkhashps';
request({uri: uri, json: true}, function (error, response, body) {
if (body == 'There was an error. Check your console.') {
return cb('-');
} else {
if (settings.nethash_units == 'K') {
return cb((body / 1000).toFixed(4));
} else if (settings.nethash_units == 'M'){
return cb((body / 1000000).toFixed(4));
} else if (settings.nethash_units == 'G') {
return cb((body / 1000000000).toFixed(4));
} else if (settings.nethash_units == 'T') {
return cb((body / 1000000000000).toFixed(4));
} else if (settings.nethash_units == 'P') {
return cb((body / 1000000000000000).toFixed(4));
} else {
return cb((body).toFixed(4));
}
}
});
}
},
get_difficulty: function(cb) {
var uri = base_url + 'getdifficulty';
request({uri: uri, json: true}, function (error, response, body) {
return cb(body);
});
},
get_connectioncount: function(cb) {
var uri = base_url + 'getconnectioncount';
request({uri: uri, json: true}, function (error, response, body) {
return cb(body);
});
},
get_blockcount: function(cb) {
var uri = base_url + 'getblockcount';
request({uri: uri, json: true}, function (error, response, body) {
return cb(body);
});
},
get_blockhash: function(height, cb) {
var uri = base_url + 'getblockhash?height=' + height;
request({uri: uri, json: true}, function (error, response, body) {
return cb(body);
});
},
get_block: function(hash, cb) {
var uri = base_url + 'getblock?hash=' + hash;
request({uri: uri, json: true}, function (error, response, body) {
return cb(body);
});
},
get_rawtransaction: function(hash, cb) {
var uri = base_url + 'getrawtransaction?txid=' + hash + '&decrypt=1';
request({uri: uri, json: true}, function (error, response, body) {
return cb(body);
});
},
get_maxmoney: function(cb) {
var uri = base_url + 'getmaxmoney';
request({uri: uri, json: true}, function (error, response, body) {
return cb(body);
});
},
get_maxvote: function(cb) {
var uri = base_url + 'getmaxvote';
request({uri: uri, json: true}, function (error, response, body) {
return cb(body);
});
},
get_vote: function(cb) {
var uri = base_url + 'getvote';
request({uri: uri, json: true}, function (error, response, body) {
return cb(body);
});
},
get_phase: function(cb) {
var uri = base_url + 'getphase';
request({uri: uri, json: true}, function (error, response, body) {
return cb(body);
});
},
get_reward: function(cb) {
var uri = base_url + 'getreward';
request({uri: uri, json: true}, function (error, response, body) {
return cb(body);
});
},
get_estnext: function(cb) {
var uri = base_url + 'getnextrewardestimate';
request({uri: uri, json: true}, function (error, response, body) {
return cb(body);
});
},
get_nextin: function(cb) {
var uri = base_url + 'getnextrewardwhenstr';
request({uri: uri, json: true}, function (error, response, body) {
return cb(body);
});
},
// synchonous loop used to interate through an array,
// avoid use unless absolutely neccessary
syncLoop: function(iterations, process, exit){
var index = 0,
done = false,
shouldExit = false;
var loop = {
next:function(){
if(done){
if(shouldExit && exit){
exit(); // Exit if we're done
}
return; // Stop the loop if we're done
}
// If we're not finished
if(index < iterations){
index++; // Increment our index
if (index % 100 === 0) { //clear stack
setTimeout(function() {
process(loop); // Run our process, pass in the loop
}, 1);
} else {
process(loop); // Run our process, pass in the loop
}
// Otherwise we're done
} else {
done = true; // Make sure we say we're done
if(exit) exit(); // Call the callback on exit
}
},
iteration:function(){
return index - 1; // Return the loop number we're on
},
break:function(end){
done = true; // End the loop
shouldExit = end; // Passing end as true means we still call the exit callback
}
};
loop.next();
return loop;
},
balance_supply: function(cb) {
Address.find({}, 'balance').where('balance').gt(0).exec(function(err, docs) {
var count = 0;
module.exports.syncLoop(docs.length, function (loop) {
var i = loop.iteration();
count = count + docs[i].balance;
loop.next();
}, function(){
return cb(count);
});
});
},
get_supply: function(cb) {
if ( settings.supply == 'HEAVY' ) {
var uri = base_url + 'getsupply';
request({uri: uri, json: true}, function (error, response, body) {
return cb(body);
});
} else if (settings.supply == 'GETINFO') {
var uri = base_url + 'getinfo';
request({uri: uri, json: true}, function (error, response, body) {
return cb(body.moneysupply);
});
} else if (settings.supply == 'BALANCES') {
module.exports.balance_supply(function(supply) {
return cb(supply/100000000);
});
} else if (settings.supply == 'TXOUTSET') {
var uri = base_url + 'gettxoutsetinfo';
request({uri: uri, json: true}, function (error, response, body) {
return cb(body.total_amount);
});
} else {
coinbase_supply(function(supply) {
return cb(supply/100000000);
});
}
},
is_unique: function(array, object, cb) {
var unique = true;
var index = null;
module.exports.syncLoop(array.length, function (loop) {
var i = loop.iteration();
if (array[i].addresses == object) {
unique = false;
index = i;
loop.break(true);
loop.next();
} else {
loop.next();
}
}, function(){
return cb(unique, index);
});
},
calculate_total: function(vout, cb) {
var total = 0;
module.exports.syncLoop(vout.length, function (loop) {
var i = loop.iteration();
//module.exports.convert_to_satoshi(parseFloat(vout[i].amount), function(amount_sat){
total = total + vout[i].amount;
loop.next();
//});
}, function(){
return cb(total);
});
},
prepare_vout: function(vout, txid, vin, cb) {
var arr_vout = [];
var arr_vin = [];
arr_vin = vin;
module.exports.syncLoop(vout.length, function (loop) {
var i = loop.iteration();
// make sure vout has an address
if (vout[i].scriptPubKey.type != 'nonstandard' && vout[i].scriptPubKey.type != 'nulldata') {
// check if vout address is unique, if so add it array, if not add its amount to existing index
//console.log('vout:' + i + ':' + txid);
module.exports.is_unique(arr_vout, vout[i].scriptPubKey.addresses[0], function(unique, index) {
if (unique == true) {
// unique vout
module.exports.convert_to_satoshi(parseFloat(vout[i].value), function(amount_sat){
arr_vout.push({addresses: vout[i].scriptPubKey.addresses[0], amount: amount_sat});
loop.next();
});
} else {
// already exists
module.exports.convert_to_satoshi(parseFloat(vout[i].value), function(amount_sat){
arr_vout[index].amount = arr_vout[index].amount + amount_sat;
loop.next();
});
}
});
} else {
// no address, move to next vout
loop.next();
}
}, function(){
if (vout[0].scriptPubKey.type == 'nonstandard') {
if ( arr_vin.length > 0 && arr_vout.length > 0 ) {
if (arr_vin[0].addresses == arr_vout[0].addresses) {
//PoS
arr_vout[0].amount = arr_vout[0].amount - arr_vin[0].amount;
arr_vin.shift();
return cb(arr_vout, arr_vin);
} else {
return cb(arr_vout, arr_vin);
}
} else {
return cb(arr_vout, arr_vin);
}
} else {
return cb(arr_vout, arr_vin);
}
});
},
get_input_addresses: function(input, vout, cb) {
var addresses = [];
if (input.coinbase) {
var amount = 0;
module.exports.syncLoop(vout.length, function (loop) {
var i = loop.iteration();
amount = amount + parseFloat(vout[i].value);
loop.next();
}, function(){
addresses.push({hash: 'coinbase', amount: amount});
return cb(addresses);
});
} else {
module.exports.get_rawtransaction(input.txid, function(tx){
if (tx) {
module.exports.syncLoop(tx.vout.length, function (loop) {
var i = loop.iteration();
if (tx.vout[i].n == input.vout) {
//module.exports.convert_to_satoshi(parseFloat(tx.vout[i].value), function(amount_sat){
if (tx.vout[i].scriptPubKey.addresses) {
addresses.push({hash: tx.vout[i].scriptPubKey.addresses[0], amount:tx.vout[i].value});
}
loop.break(true);
loop.next();
//});
} else {
loop.next();
}
}, function(){
return cb(addresses);
});
} else {
return cb();
}
});
}
},
prepare_vin: function(tx, cb) {
var arr_vin = [];
module.exports.syncLoop(tx.vin.length, function (loop) {
var i = loop.iteration();
module.exports.get_input_addresses(tx.vin[i], tx.vout, function(addresses){
if (addresses && addresses.length) {
//console.log('vin');
module.exports.is_unique(arr_vin, addresses[0].hash, function(unique, index) {
if (unique == true) {
module.exports.convert_to_satoshi(parseFloat(addresses[0].amount), function(amount_sat){
arr_vin.push({addresses:addresses[0].hash, amount:amount_sat});
loop.next();
});
} else {
module.exports.convert_to_satoshi(parseFloat(addresses[0].amount), function(amount_sat){
arr_vin[index].amount = arr_vin[index].amount + amount_sat;
loop.next();
});
}
});
} else {
loop.next();
}
});
}, function(){
return cb(arr_vin);
});
}
};