From 44b420b2bbf6227d3c35658ce65049de014f0809 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sun, 6 Oct 2019 18:32:07 -0700 Subject: get fx rates from fixer api --- binance.js | 12 +++++++++++- ecosystem.config.js | 20 ++++++++++---------- index.js | 4 ++-- kraken.js | 23 ----------------------- rates.js | 27 +++++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 36 deletions(-) delete mode 100644 kraken.js create mode 100644 rates.js diff --git a/binance.js b/binance.js index 8edcc5da..007f7848 100644 --- a/binance.js +++ b/binance.js @@ -1,6 +1,6 @@ const WebSocket = require("ws"); -module.exports = app => { +module.exports = (app, socket) => { const binance = new WebSocket( "wss://stream.binance.com:9443/ws/btcusdt@ticker" ); @@ -10,5 +10,15 @@ module.exports = app => { app.set("bid", msg.b); app.set("ask", msg.a); app.set("last", msg.l); + + let fx = app.get("rates"); + if (!fx) return; + let rates = {}; + + Object.keys(fx).map(symbol => { + rates[symbol] = msg.a * fx[symbol]; + }); + + socket.emit("rates", rates); }; }; diff --git a/ecosystem.config.js b/ecosystem.config.js index 12a23b71..1a8f27e0 100644 --- a/ecosystem.config.js +++ b/ecosystem.config.js @@ -1,18 +1,18 @@ module.exports = { apps: [ { - name: 'coinos', - script: 'index.js', - watch: ['*.js'], - interpreter: 'node', + name: "coinos", + script: "index.js", + watch: ["./"], + interpreter: "node", env: { - COMMON_VARIABLE: 'true', - NODE_OPTIONS: '--require ./.pnp.js', + COMMON_VARIABLE: "true", + NODE_OPTIONS: "--require ./.pnp.js" }, env_production: { - NODE_ENV: 'production', - NODE_OPTIONS: '--require ./.pnp.js', + NODE_ENV: "production", + NODE_OPTIONS: "--require ./.pnp.js" } - }, + } ] -} +}; diff --git a/index.js b/index.js index e2032825..2e452404 100644 --- a/index.js +++ b/index.js @@ -39,8 +39,8 @@ const l = console.log; }) ); - require("./binance")(app); - require("./kraken")(app, socket); + require("./binance")(app, socket); + require("./rates")(app); const seen = []; const addresses = {}; diff --git a/kraken.js b/kraken.js deleted file mode 100644 index 70d7d183..00000000 --- a/kraken.js +++ /dev/null @@ -1,23 +0,0 @@ -const axios = require("axios"); -const l = console.log; - -module.exports = (app, socket) => { - let fetchRates; - (fetchRates = async () => { - try { - let res = await axios.get( - "https://api.kraken.com/0/public/Ticker?pair=XBTCAD" - ); - let ask = res.data.result.XXBTZCAD.c[0]; - let now = new Date(); - let ts = `${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`; - // l(ts, "kraken ask:", ask); - app.set("rates", { ask }); - socket.emit("rate", ask); - } catch (e) { - l(e); - } - - setTimeout(fetchRates, 3000); - })(); -}; diff --git a/rates.js b/rates.js new file mode 100644 index 00000000..d4616780 --- /dev/null +++ b/rates.js @@ -0,0 +1,27 @@ +const axios = require("axios"); +const config = require("./config"); +const l = console.log; + +module.exports = app => { + let fetchRates; + (fetchRates = async () => { + try { + let rates = {}; + let res = await axios.get( + `http://data.fixer.io/api/latest?access_key=${config.fixer}` + ); + + let fx = res.data.rates; + + Object.keys(fx).map(symbol => { + rates[symbol] = fx[symbol] / fx["USD"]; + }); + + app.set("rates", rates); + } catch (e) { + l(e); + } + + setTimeout(fetchRates, 180000); + })(); +}; -- cgit v1.2.3