aboutsummaryrefslogtreecommitdiffstats
path: root/lib/binance.js
blob: af8d0b15393fa0615bd18591279b1c64247b2ad4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import app from "$app";
import WebSocket from "ws";
import { err } from "./logging";
import store from "./store";

export default () => {
  const binance = new WebSocket(
    "wss://stream.binance.com:9443/ws/btcusdt@ticker"
  );

  binance.onmessage = async function(event) {
    try {
      let msg = JSON.parse(event.data);

      if (!store.fx) return;

      Object.keys(store.fx).map(symbol => {
        store.rates[symbol] = msg.c * store.fx[symbol];
      });

      // app.set("ask", msg.a);
      // app.set("bid", msg.b);
      store.last = msg.c;
    } catch (e) {
      err("binance message error", e.message);
    }
  };

  binance.onerror = async function(error) {
    err("binance socket error", error);
  };

  return binance;
};