diff options
| author | Adam Soltys <[email protected]> | 2023-01-16 23:40:25 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2023-01-16 23:40:25 -0800 |
| commit | 91e847126c832efa25ab0bf8d6f3b7e537378fa7 (patch) | |
| tree | 4a42b6e3bec6eb0f640ed44533b54e6d3d6a5e0e /lib/rates.js | |
| parent | 6bafea9bffe24d2227d269812a73a9a513d92ddf (diff) | |
| download | coinos-server-91e847126c832efa25ab0bf8d6f3b7e537378fa7.tar.gz coinos-server-91e847126c832efa25ab0bf8d6f3b7e537378fa7.zip | |
shuffle things around
Diffstat (limited to 'lib/rates.js')
| -rw-r--r-- | lib/rates.js | 87 |
1 files changed, 39 insertions, 48 deletions
diff --git a/lib/rates.js b/lib/rates.js index 8064a117..c7d6918c 100644 --- a/lib/rates.js +++ b/lib/rates.js @@ -1,69 +1,60 @@ -import app from "$app"; import got from "got"; import config from "$config"; -import fs from "fs"; -import binance from "./binance"; import { broadcast } from "./sockets"; import store from "./store"; import { err, warn } from "$lib/logging"; +import { g, s } from "$lib/db"; +import WebSocket from "ws"; -let b = binance(); -setInterval(() => { - b.terminate(); - b = binance(); -}, 360000); +let ws; +let connect = () => { + if (ws) ws.terminate(); + ws = new WebSocket("wss://stream.binance.com:9443/ws/btcusdt@ticker"); -let fetchRates = async () => { - let date, fx; + ws.onmessage = async function(event) { + try { + let msg = JSON.parse(event.data); - try { - ({ date, fx } = JSON.parse(fs.readFileSync("data/fx"))); - } catch (e) { - warn("Failed to read fx file"); - } + if (!store.fx) return; - let today = JSON.stringify(new Date()) - .replace(/T.*/, "") - .replace(/"/, ""); + Object.keys(store.fx).map(symbol => { + store.rates[symbol] = msg.c * store.fx[symbol]; + }); + + store.last = msg.c; + } catch (e) { + err("binance message error", e.message); + } + }; + + ws.onerror = async function(error) { + err("binance socket error", error.message); + }; + + setTimeout(connect, 360000); + return ws; +}; - if (date !== today) { +export let sendRates = () => broadcast("rate", store.last); +export let getRates = async () => { + connect(); + + let date, fx; + let rates = await g("rates"); + if (rates) ({ date, fx } = rates); + + if (Date.now() - date > 24 * 60 * 60 * 1000) { try { let { rates: fx } = await got( `https://api.exchangerate.host/latest?base=USD&v={(new Date()).toISOString().split('T')[0]}` ).json(); - fs.writeFileSync("data/fx", JSON.stringify({ date, fx })); + await s("rates", { date, fx }); } catch (e) { - try { - let { rates: fx } = await got( - `http://data.fixer.io/api/latest?access_key=${config.fixer}` - ).json(); - - let USD = fx["USD"]; - Object.keys(fx).map(k => { - fx[k] = parseFloat((fx[k] / fx["USD"]).toFixed(6)); - }); - - fs.writeFileSync("data/fx", JSON.stringify({ date, fx })); - } catch (e) { - err("error fetching rates", e.message); - } + err("error fetching rates", e.message); } } store.fx = fx; - setTimeout(fetchRates, 7200000); + setTimeout(getRates, 7200000); }; -fetchRates(); - -app.get("/rate", async (req, res, next) => { - res.send(store.last); -}); - -app.get("/rates", async (req, res, next) => { - res.send(store.rates); -}); - -setInterval(() => { - broadcast("rate", store.last); -}, 1000); |
