aboutsummaryrefslogtreecommitdiffstats
path: root/lib/rates.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-08-26 21:17:43 -0700
committerAdam Soltys <[email protected]>2024-08-26 21:17:43 -0700
commitfe62cadd6fb4a7e49f7d59401fc74ae0411e32a7 (patch)
treeaa8f34e1692bd2d638a2c996eeadb58671d07aea /lib/rates.js
parent00e43ddc0bf3d6dd719fc9295d156567fa2fecfb (diff)
downloadcoinos-server-fe62cadd6fb4a7e49f7d59401fc74ae0411e32a7.tar.gz
coinos-server-fe62cadd6fb4a7e49f7d59401fc74ae0411e32a7.zip
typescript
Diffstat (limited to 'lib/rates.js')
-rw-r--r--lib/rates.js71
1 files changed, 0 insertions, 71 deletions
diff --git a/lib/rates.js b/lib/rates.js
deleted file mode 100644
index fe942453..00000000
--- a/lib/rates.js
+++ /dev/null
@@ -1,71 +0,0 @@
-import got from "got";
-import config from "$config";
-import store from "$lib/store";
-import { err, warn } from "$lib/logging";
-import { g, s } from "$lib/db";
-import { sleep } from "$lib/utils";
-import WebSocket from "ws";
-
-let last;
-let ws;
-let connect = async () => {
- if (ws && ws.readyState === 1 && Date.now() - last < 5000) return;
- if (ws) ws.terminate() && (await sleep(Math.round(Math.random() * 1000)));
-
- ws = new WebSocket("wss://stream.binance.com:9443/ws/btcusdt@ticker");
-
- ws.onmessage = async function (event) {
- try {
- let msg = JSON.parse(event.data);
- let rates = await g("rates");
- let fx = await g("fx");
- if (fx) ({ fx } = fx);
-
- Object.keys(fx).map((symbol) => {
- rates[symbol] = msg.c * fx[symbol];
- });
-
- s("rate", msg.c);
- s("rates", rates);
- last = Date.now();
- } catch (e) {
- console.log(e);
- err("binance message error", e.message);
- }
- };
-
- ws.onerror = async function (error) {
- err("binance socket error", error.message);
- };
-
- return ws;
-};
-
-export let getFx = async () => {
- connect();
-
- let date = 0,
- fx = await g("fx");
- if (fx) ({ date, fx } = fx);
-
- if (Date.now() - date > 24 * 60 * 60 * 1000) {
- date = Date.now();
- try {
- let r = await got(
- `http://data.fixer.io/api/latest?access_key=${config.fixer}`,
- ).json();
- let { rates: fx } = r;
- let USD = fx["USD"];
-
- Object.keys(fx).map((k) => {
- fx[k] = fx[k] / USD;
- });
-
- await s("fx", { date, fx });
- } catch (e) {
- err("error fetching rates", e.message);
- }
- }
-
- setTimeout(getFx, 30000);
-};