Skip to content

Instantly share code, notes, and snippets.

@codedot
Last active August 29, 2015 14:01
Show Gist options
  • Save codedot/1f08a9e8ceafda847605 to your computer and use it in GitHub Desktop.
Save codedot/1f08a9e8ceafda847605 to your computer and use it in GitHub Desktop.
Bitstamp vs. Ripple spreads listener
var ripple = require("ripple-lib");
var Pusher = require("pusher-client");
var west = {
host: "s-west.ripple.com",
port: 443,
secure: true
};
var east = {
host: "s-east.ripple.com",
port: 443,
secure: true
};
var options = {
fee_cushion: 1.5,
servers: [
east,
west
],
trusted: false
};
var remote = new ripple.Remote(options);
var native = new Pusher("de504dc5763aeef9ff52");
function start()
{
remote.on("ledger_closed", update);
}
function update()
{
var base = {
currency: "USD",
issuer: "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B"
};
var counter = {
currency: "BTC",
issuer: "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B"
};
var bid = {
gets: base,
pays: counter
};
var ask = {
gets: counter,
pays: base
};
remote.request_book_offers(bid, showbid);
remote.request_book_offers(ask, showask);
}
function gettime()
{
var date = new Date();
return date.getTime().toFixed(0);
}
function showrip(error, response, type)
{
var time = gettime();
var offer, src, dst, price;
if (error)
return;
offer = response.offers.shift();
src = offer.TakerPays.value;
dst = offer.TakerGets.value;
offer = ("Ask" == type ? src / dst : dst / src);
offer = offer.toFixed(2);
console.log(time, type, offer, "Ripple");
}
function showbid(error, response)
{
showrip(error, response, "Bid");
}
function showask(error, response)
{
showrip(error, response, "Ask");
}
function showtop(data)
{
var time = gettime();
var bid = data.bids.shift().shift();
var ask = data.asks.shift().shift();
console.log(time, "Bid", bid, "Bitstamp");
console.log(time, "Ask", ask, "Bitstamp");
}
function finish()
{
process.exit();
}
setTimeout(finish, 3e5);
remote.connect(start);
native.subscribe("order_book").bind("data", showtop);
PUSHER = node_modules/pusher-client/package.json
RIPPLE = node_modules/ripple-lib/package.json
all: $(PUSHER) $(RIPPLE)
node arb | tee -a top.log
$(PUSHER):
npm install pusher-client
$(RIPPLE):
npm install ripple-lib
loop:
-while true; do \
$(MAKE); \
sleep 1; \
done
daemon: stop
nohup $(MAKE) loop >/dev/null & echo $$! >daemon.pid
sleep 1
stop:
-if [ -f daemon.pid ]; then \
kill "`cat daemon.pid`"; \
rm -f daemon.pid; \
fi
clean: stop
-rm -fr node_modules *.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment