aboutsummaryrefslogtreecommitdiffstats
path: root/lib/binance.js
blob: 78936d47136481758ef013510599a50008cd90db (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
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];
      });

      store.last = msg.c;
    } catch (e) {
      err("binance message error", e.message);
    }
  };

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

  return binance;
};