aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2019-10-06 18:32:07 -0700
committerAdam Soltys <[email protected]>2019-10-06 18:32:07 -0700
commit44b420b2bbf6227d3c35658ce65049de014f0809 (patch)
treed9d886e5db17ca5c2d5830eb87e4187f0e5d285a
parentb61477bfebe726b9200e10c1e789564a75b865b4 (diff)
downloadcoinos-server-44b420b2bbf6227d3c35658ce65049de014f0809.tar.gz
coinos-server-44b420b2bbf6227d3c35658ce65049de014f0809.zip
get fx rates from fixer api
-rw-r--r--binance.js12
-rw-r--r--ecosystem.config.js20
-rw-r--r--index.js4
-rw-r--r--kraken.js23
-rw-r--r--rates.js27
5 files changed, 50 insertions, 36 deletions
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);
+ })();
+};