From 6101cbf5db17faa2c32d81fc28a3971fb3b98c12 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 22 Nov 2016 12:01:38 -0800 Subject: Switch to blockcypher API from blockchain --- public/js/calculator.coffee | 16 ++++++++-------- public/js/calculator.js | 28 +++++++++++++++------------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/public/js/calculator.coffee b/public/js/calculator.coffee index fccc0c5e..7d185f24 100644 --- a/public/js/calculator.coffee +++ b/public/js/calculator.coffee @@ -169,13 +169,13 @@ listen = -> setTimeout(listen, 12000) else fail(SOCKET_FAIL) if g.attempts > 3 - g.blockchain = new WebSocket("wss://ws.blockchain.info/inv") + g.blockchain = new WebSocket("wss://socket.blockcypher.com/v1/btc/main") g.blockchain.onopen = -> if g.blockchain.readyState is 1 g.attempts = 0 clear(SOCKET_FAIL) - g.blockchain.send('{"op":"addr_sub", "addr":"' + g.user.address + '"}') + g.blockchain.send('{"event":"unconfirmed-tx", "address":"' + g.user.address + '"}') else setTimeout(g.blockchain.onopen, 12000 * g.attempts) @@ -190,17 +190,17 @@ listen = -> listen() g.blockchain.onmessage = (e) -> - results = eval('(' + e.data + ')') + tx = JSON.parse(e.data) + amount = 0 - txid = results.x.hash + txid = tx.hash return if txid == g.last g.last = txid - $.each(results.x.out, (i, v) -> - if (v.addr == g.user.address) - amount += v.value / 100000000 - ) + for output in tx.outputs + if (output.addresses[0] == g.user.address) + amount += output.value / 100000000 logTransaction(txid, amount) diff --git a/public/js/calculator.js b/public/js/calculator.js index ab2123ca..3a564034 100644 --- a/public/js/calculator.js +++ b/public/js/calculator.js @@ -173,12 +173,12 @@ if (g.attempts > 3) { fail(SOCKET_FAIL); } - g.blockchain = new WebSocket("wss://ws.blockchain.info/inv"); + g.blockchain = new WebSocket("wss://socket.blockcypher.com/v1/btc/main"); g.blockchain.onopen = function() { if (g.blockchain.readyState === 1) { g.attempts = 0; clear(SOCKET_FAIL); - return g.blockchain.send('{"op":"addr_sub", "addr":"' + g.user.address + '"}'); + return g.blockchain.send('{"event":"unconfirmed-tx", "address":"' + g.user.address + '"}'); } else { return setTimeout(g.blockchain.onopen, 12000 * g.attempts); } @@ -194,19 +194,21 @@ return listen(); }; return g.blockchain.onmessage = function(e) { - var amount, results, txid; - results = eval('(' + e.data + ')'); + var amount, j, len, output, ref, tx, txid; + tx = JSON.parse(e.data); amount = 0; - txid = results.x.hash; + txid = tx.hash; if (txid === g.last) { return; } g.last = txid; - $.each(results.x.out, function(i, v) { - if (v.addr === g.user.address) { - return amount += v.value / 100000000; + ref = tx.outputs; + for (j = 0, len = ref.length; j < len; j++) { + output = ref[j]; + if (output.addresses[0] === g.user.address) { + amount += output.value / 100000000; } - }); + } return logTransaction(txid, amount); }; } @@ -271,17 +273,17 @@ }; Array.prototype.uniq = function() { - var j, key, output, ref, results1, value; + var j, key, output, ref, results, value; output = {}; for (key = j = 0, ref = this.length; 0 <= ref ? j < ref : j > ref; key = 0 <= ref ? ++j : --j) { output[this[key]] = this[key]; } - results1 = []; + results = []; for (key in output) { value = output[key]; - results1.push(value); + results.push(value); } - return results1; + return results; }; multiplier = function() { -- cgit v1.2.3