Skip to content

Instantly share code, notes, and snippets.

@orweinberger
Last active August 29, 2015 14:22
Show Gist options
  • Save orweinberger/5b6b276f6f6dadf686f8 to your computer and use it in GitHub Desktop.
Save orweinberger/5b6b276f6f6dadf686f8 to your computer and use it in GitHub Desktop.
Unconfirmed txs to elastic
var WebSocket = require('ws');
var request = require('request');
var socket = new WebSocket("wss://ws.blockchain.info/inv");
socket.onopen = function (event) {
console.log('connected');
socket.send(JSON.stringify({"op": "unconfirmed_sub"}));
};
var last;
socket.onmessage = function (event) {
var now = new Date().getTime();
var since_last = now - last;
last = new Date().getTime();
var sum = 0;
var e = JSON.parse(event.data);
var ins = e.x.inputs.length;
var outs = e.x.out.length;
var in_addresses = [];
var out_addresses = [];
e.x.inputs.forEach(function (d) {
in_addresses.push(d.prev_out.addr);
});
e.x.out.forEach(function (d) {
sum += d.value;
out_addresses.push(d.addr);
});
var doc = {
timestamp: new Date().toISOString(),
since_previous: since_last,
ip: e.x.relayed_by,
sum_satoshis: sum,
sum_btc: sum / 100000000,
inputs: ins,
outputs: outs,
in_addresses: in_addresses,
out_addresses: out_addresses,
hash: e.x.hash
};
request({
url: "http://localhost:9200/txs/tx/" + doc.hash,
method: "POST",
json: doc
})
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment