-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
87 lines (59 loc) · 1.81 KB
/
index.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
var _ = require('lodash');
var bitcore = require('bitcore');
var p2p = require('bitcore-p2p');
var Ma = require('moving-average');
var maBTC = Ma(1 * 60 * 1000);
var maUSD = Ma(1 * 60 * 1000);
var maSatoshis = Ma(1 * 60 * 1000);
var Transaction = bitcore.Transaction;
var Unit = bitcore.Unit;
var Peer = p2p.Peer;
var Messages = new p2p.Messages();
var peer = new Peer({
host: '192.168.1.10',
port: 8333,
});
peer.on('ready', function(message) {
console.log('Starting ', peer.version, peer.subversion, peer.bestHeight);
});
var timeWindow = 5;
var satoshis = 0;
var btc = 0;
var btcAux = 0;
var usd = 0;
peer.on('tx', function(message) {
var salida = message.transaction.toJSON();
var output = _.pluck(salida.outputs, 'satoshis');
var unitPreference = Unit.BTC;
_.each(output, function(v) {
satoshis += v;
btc = Unit.fromSatoshis(v).to(unitPreference);
btcAux += btc;
usd += btc * 223.74;
return ;
});
maBTC.push(Date.now(), btcAux);
maUSD.push(Date.now(), usd);
maSatoshis.push(Date.now(), satoshis);
});
setInterval(function() {
if(maBTC.movingAverage()){
console.log('\n\tAverage:');
console.log('\t###########################\n\t# ' + maBTC.movingAverage().toFixed(2) + ' BTC/s');
console.log('\t# ' + maUSD.movingAverage().toFixed(2) + ' USD/s');
console.log('\t# ' + maSatoshis.movingAverage().toFixed(0) + ' Satoshis/s' + '\n\t###########################');
var btcPerSecond = btcAux / timeWindow;
var usdPerSecond = usd / timeWindow;
var satoshisPerSecond = satoshis / timeWindow;
btcAux = 0;
usd = 0;
satoshis = 0;
}
}, timeWindow * 1000);
peer.on('inv', function(message) {
peer.sendMessage(Messages.GetData(message.inventory));
});
peer.on('disconnect', function() {
console.log('connection closed');
});
peer.connect();