ipod-widget-server/lib/xmrData.js
2019-07-25 10:55:22 +02:00

48 lines
1.5 KiB
JavaScript

const { EventEmitter } = require('events');
const axios = require('axios');
const log = require('loglevel');
class XMRData extends EventEmitter {
constructor() {
super();
}
get initial() {
return { shapeshift: null, xmrto: null, shapeshift_xmr: null };
}
start() {
this.fetch();
setInterval(() => {
this.fetch();
}, 30000);
}
fetch() {
Promise.all([
axios.get("https://bitpay.com/api/rates/eur").catch(err => log.error(err.config.url, err.message)),
axios.get("https://shapeshift.io/marketinfo/btc_xmr").catch(err => log.error(err.config.url, err.message)),
axios.get("https://xmr.to/api/v2/xmr2btc/order_parameter_query/").catch(err => log.error(err.config.url, err.message)),
]).then(resp => {
let bitpay = null, shapeshift = null, xmrto = null, shapeshift_xmr = null;
if (resp[0] != undefined) {
bitpay = resp[0].data.rate;
if (resp[1] != undefined) {
shapeshift_xmr = (Math.round(resp[1].data.rate * 100) / 100) + "XMR";
shapeshift = (Math.round((bitpay / resp[1].data.rate) * 100) / 100) + "€";
}
if (resp[2] != undefined) {
xmrto = (Math.round((bitpay * resp[2].data.price) * 100) / 100) + "€";
}
}
this.emit("data", { shapeshift, xmrto, shapeshift_xmr });
});
}
}
module.exports = new XMRData();