Skip to content

Instantly share code, notes, and snippets.

@marfillaster
Created June 13, 2015 11:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marfillaster/f88681eb0135692b8ae9 to your computer and use it in GitHub Desktop.
Save marfillaster/f88681eb0135692b8ae9 to your computer and use it in GitHub Desktop.
coins-ph-rate.js
// ==UserScript==
// @name coinsph rate info
// @namespace http://your.homepage/
// @version 0.1
// @description enter something useful
// @author Ken Marfilla
// @match http://stackoverflow.com/questions/5258989/manually-adding-a-userscript-to-google-chrome
// @include https://coins.ph/*
// @grant none
// ==/UserScript==
(function() {
function fmt (n, p) {
return parseFloat(Math.round(n * 100) / 100).toFixed(p || 2);
}
function Row(indicator, bid, ask, unit, precision) {
this.indicator = indicator;
this.bid = fmt(bid, precision) + ' ' + unit;
this.ask = fmt(ask, precision) + ' ' + unit;
}
function updateQuote() {
Promise.all([
fetch('https://api.bitcoinaverage.com/ticker/global/USD/'),
fetch('/api/v1/quote'),
fetch('https://openexchangerates.org/api/latest.json?app_id=cf7a993d922c459b812cd49d7a4f0d84')
]).then(function(responses) {
Promise.all([].map.call(responses, function(response) {
return response.json();
})).then(function(jsons) {
console.table([
new Row('coinsph', jsons[1].quote.bid, jsons[1].quote.ask, 'PHP'),
new Row('average', jsons[0].bid*jsons[2].rates.PHP, jsons[0].ask*jsons[2].rates.PHP, 'PHP'),
new Row('spread (+1%ask)',
jsons[0].bid*jsons[2].rates.PHP - jsons[1].quote.bid,
jsons[1].quote.ask - jsons[0].ask*jsons[2].rates.PHP + (0.01*jsons[1].quote.ask),
'PHP'),
new Row('coinsph', jsons[1].quote.bid/jsons[2].rates.PHP, jsons[1].quote.ask/jsons[2].rates.PHP, 'USD'),
new Row('average', jsons[0].bid, jsons[0].ask, 'USD'),
new Row('spread (+1%ask)',
jsons[0].bid - jsons[1].quote.bid/jsons[2].rates.PHP,
jsons[1].quote.ask/jsons[2].rates.PHP - jsons[0].ask + (0.01*jsons[1].quote.ask/jsons[2].rates.PHP),
'USD'),
]);
});
});
}
updateQuote();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment