Created
June 13, 2015 11:03
-
-
Save marfillaster/f88681eb0135692b8ae9 to your computer and use it in GitHub Desktop.
coins-ph-rate.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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