Skip to content

Commit

Permalink
Switch transaction provider back to blockchain.info, as Toshi.io has …
Browse files Browse the repository at this point in the history
…been unreliable
  • Loading branch information
MaxLaumeister committed Jun 12, 2016
1 parent 52d2706 commit f57e589
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 126 deletions.
6 changes: 3 additions & 3 deletions bitlisten.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bitlisten.min.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<link rel="stylesheet" type="text/css" href="ie.css">
<![endif]-->

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="//js.pusher.com/2.2/pusher.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="https://js.pusher.com/2.2/pusher.min.js"></script>
<script src="bitlisten.min.js" type="text/javascript"></script>

<script>
Expand Down
116 changes: 24 additions & 92 deletions src/socket.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var satoshi = 100000000;

var DELAY_CAP = 1000;

var DELAY_CAP = 5000;
var lastBlockHeight = 0;

var provider_name = "blockchain.info";

/** @constructor */
function TransactionSocket() {

Expand All @@ -15,60 +15,60 @@ TransactionSocket.init = function() {
TransactionSocket.connection.close();

if ('WebSocket' in window) {
var connection = new ReconnectingWebSocket('wss://bitcoin.toshi.io');
var connection = new ReconnectingWebSocket('wss://ws.blockchain.info/inv');
TransactionSocket.connection = connection;

StatusBox.reconnecting("blockchain");

connection.onopen = function() {
console.log('Toshi.io: Connection open!');
console.log('Blockchain.info: Connection open!');
StatusBox.connected("blockchain");
var newTransactions = {
"subscribe" : "transactions"
"op" : "unconfirmed_sub"
};
var newBlocks = {
"subscribe" : "blocks"
"op" : "blocks_sub"
};
connection.send(JSON.stringify(newTransactions));
connection.send(JSON.stringify(newBlocks));
connection.send(JSON.stringify({
"fetch" : "latest_transaction"
"op" : "ping_tx"
}));
// Display the latest transaction so the user sees something.
};

connection.onclose = function() {
console.log('Toshi.io: Connection closed');
console.log('Blockchain.info: Connection closed');
if ($("#blockchainCheckBox").prop("checked"))
StatusBox.reconnecting("blockchain");
else
StatusBox.closed("blockchain");
};

connection.onerror = function(error) {
console.log('Toshi.io: Connection Error: ' + error);
console.log('Blockchain.info: Connection Error: ' + error);
};

connection.onmessage = function(e) {
var response = JSON.parse(e.data);
var data = JSON.parse(e.data);

// New Transaction
if (response.subscription == "transactions" || response.fetched == "latest_transaction") {
if (data.op == "utx") {
var transacted = 0;

for (var i = 0; i < response.data.outputs.length; i++) {
transacted += response.data.outputs[i].amount;
for (var i = 0; i < data.x.out.length; i++) {
transacted += data.x.out[i].value;
}

var bitcoins = transacted / satoshi;
//console.log("Transaction: " + bitcoins + " BTC");

var donation = false;
var soundDonation = false;
var outputs = response.data.outputs;
var soundDonation = false;
var outputs = data.x.out;
for (var j = 0; j < outputs.length; j++) {
if ((outputs[j].addresses[0]) == DONATION_ADDRESS) {
bitcoins = response.data.outputs[j].amount / satoshi;
if ((outputs[j].addr) == DONATION_ADDRESS) {
bitcoins = data.x.out[j].value / satoshi;
new Transaction(bitcoins, true);
return;
}
Expand All @@ -78,11 +78,11 @@ TransactionSocket.init = function() {
new Transaction(bitcoins);
}, Math.random() * DELAY_CAP);

} else if (response.subscription == "blocks" || response.fetched == "latest_block") {
var blockHeight = response.data.height;
var transactions = response.data.transactions_count;
var volumeSent = response.data.total_out;
var blockSize = response.data.size;
} else if (data.op == "block") {
var blockHeight = data.x.height;
var transactions = data.x.nTx;
var volumeSent = data.x.estimatedBTCSent;
var blockSize = data.x.size;
// Filter out the orphaned blocks.
if (blockHeight > lastBlockHeight) {
lastBlockHeight = blockHeight;
Expand All @@ -101,74 +101,6 @@ TransactionSocket.init = function() {

TransactionSocket.close = function() {
if (TransactionSocket.connection)
TransactionSocket.connection.close();
TransactionSocket.connection.disconnect();
StatusBox.closed("blockchain");
};

/** @constructor */
function TradeSocket() {

}

TradeSocket.init = function() {
var channel_id = "dbf1dee9-4f2e-4a08-8cb7-748919a71b21"; // Channel id for BTC trades

// Terminate previous connection, if any
if (TradeSocket.connection)
TradeSocket.connection.close();

var connection = PUBNUB.init({
publish_key : 'demo',
subscribe_key : 'sub-c-50d56e1e-2fd9-11e3-a041-02ee2ddab7fe',
ssl : true
});
TradeSocket.connection = connection;

connection.close = function() {
connection.unsubscribe({channel : channel_id});
connection.onclose();
};

connection.onmessage = function(message) {
//console.log(message);
if (message.trade) {
//console.log("Trade: " + message.trade.amount_int / satoshi + " BTC | " + (message.trade.price * message.trade.amount_int / satoshi) + " " + message.trade.price_currency);
// 0.57 BTC | 42.75 USD

var bitcoins = message.trade.amount_int / satoshi;
var currency = (message.trade.price * message.trade.amount_int / satoshi);
var currencyName = message.trade.price_currency;

setTimeout(function() {
new Transaction(bitcoins, false, currency, currencyName);
}, Math.random() * DELAY_CAP);
}
};

connection.onopen = function() {
console.log('Mt.Gox: Connection open!');
StatusBox.connected("mtgox");
};

connection.onclose = function() {
console.log('Mt.Gox: Connection closed');
if ($("#mtgoxCheckBox").prop("checked"))
StatusBox.reconnecting("mtgox");
else
StatusBox.closed("mtgox");
};

connection.subscribe({
channel : channel_id,
message : connection.onmessage,
connect : connection.onopen,
disconnect : connection.onclose,
reconnect : connection.onopen
});
};

TradeSocket.close = function() {
if (TradeSocket.connection)
TradeSocket.connection.close();
StatusBox.closed("mtgox");
};
10 changes: 6 additions & 4 deletions src/statusbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var CONNECTING = "Connecting...";
var NO_SUPPORT = "No browser support.";
var CLOSED = "Click to connect.";

var provider_name = "blockchain.info";

function StatusBox() {

}
Expand All @@ -29,28 +31,28 @@ StatusBox.init = function(debugmode) {
// "type" can be either "blockchain" or "mtgox"
StatusBox.connected = function(type) {
if (type == "blockchain")
StatusBox.blockchain.html('Transactions (Toshi.io): <span style="color: green;">' + CONNECTED + '</span>');
StatusBox.blockchain.html('Transactions (' + provider_name + '): <span style="color: green;">' + CONNECTED + '</span>');
if (type == "mtgox")
StatusBox.mtgox.html('Mt.Gox Trades: <span style="color: green;">' + CONNECTED + '</span>');
};

StatusBox.reconnecting = function(type) {
if (type == "blockchain")
StatusBox.blockchain.html('Transactions (Toshi.io): <span style="color: yellow;">' + CONNECTING + '</span>');
StatusBox.blockchain.html('Transactions (' + provider_name + '): <span style="color: yellow;">' + CONNECTING + '</span>');
if (type == "mtgox")
StatusBox.mtgox.html('Mt.Gox Trades: <span style="color: yellow;">' + CONNECTING + '</span>');
};

StatusBox.nosupport = function(type) {
if (type == "blockchain")
StatusBox.blockchain.html('Transactions (Toshi.io): <span style="color: red;">' + NO_SUPPORT + '</span>');
StatusBox.blockchain.html('Transactions (' + provider_name + '): <span style="color: red;">' + NO_SUPPORT + '</span>');
if (type == "mtgox")
StatusBox.mtgox.html('Mt.Gox Trades: <span style="color: red;">' + NO_SUPPORT + '</span>');
};

StatusBox.closed = function(type) {
if (type == "blockchain")
StatusBox.blockchain.html('Transactions (Toshi.io): <span style="color: gray;">' + CLOSED + '</span>');
StatusBox.blockchain.html('Transactions (' + provider_name + '): <span style="color: gray;">' + CLOSED + '</span>');
if (type == "mtgox")
StatusBox.mtgox.html('Mt.Gox Trades: <span style="color: gray;">' + CLOSED + '</span>');
};
50 changes: 26 additions & 24 deletions src/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,34 @@ function Transaction(bitcoins, highlight, currency, currencyName) {
this.addText('<br />' + currency.toFixed(2) + ' ' + currencyName);
}
this.initPosition();
}

// Sound
var maxBitcoins = 1000;
var minVolume = 0.3;
var maxVolume = 0.7;
var volume = bitcoins / (maxBitcoins / (maxVolume - minVolume)) + minVolume;
if (volume > maxVolume)
volume = maxVolume;

// Sound
var maxBitcoins = 1000;
var minVolume = 0.3;
var maxVolume = 0.7;
var volume = bitcoins / (maxBitcoins / (maxVolume - minVolume)) + minVolume;
if (volume > maxVolume)
volume = maxVolume;

var maxPitch = 100.0;
// We need to use a log that makes it so that maxBitcoins reaches the maximum pitch.
// Well, the opposite of the maximum pitch. Anyway. So we solve:
// maxPitch = log(maxBitcoins + logUsed) / log(logUsed)
// For maxPitch = 100 (for 100%) and maxBitcoins = 1000, that gives us...
var logUsed = 1.0715307808111486871978099;
// So we find the smallest value between log(bitcoins + logUsed) / log(logUsed) and our max pitch...
var pitch = Math.min(maxPitch, Math.log(bitcoins + logUsed) / Math.log(logUsed));
// ...we invert it so that a bigger transaction = a deeper noise...
pitch = maxPitch - pitch;
// ...and we play the sound!
if(globalScalePitch) {
Sound.playPitchAtVolume(volume, pitch);
} else {
Sound.playRandomAtVolume(volume);
var maxPitch = 100.0;
// We need to use a log that makes it so that maxBitcoins reaches the maximum pitch.
// Well, the opposite of the maximum pitch. Anyway. So we solve:
// maxPitch = log(maxBitcoins + logUsed) / log(logUsed)
// For maxPitch = 100 (for 100%) and maxBitcoins = 1000, that gives us...
var logUsed = 1.0715307808111486871978099;
// So we find the smallest value between log(bitcoins + logUsed) / log(logUsed) and our max pitch...
var pitch = Math.min(maxPitch, Math.log(bitcoins + logUsed) / Math.log(logUsed));
// ...we invert it so that a bigger transaction = a deeper noise...
pitch = maxPitch - pitch;
// ...and we play the sound!
if(globalScalePitch) {
Sound.playPitchAtVolume(volume, pitch);
} else {
Sound.playRandomAtVolume(volume);
}

}

}

extend(Floatable, Transaction);

0 comments on commit f57e589

Please sign in to comment.