aboutsummaryrefslogtreecommitdiffstats
path: root/lib/webhooks.js
blob: 4741932abc544aff24186dea075bdb97bc9acfd3 (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
35
36
37
import config from "$config";
import wretch from "./wretch";
import { l } from "$lib/logging";

export const callWebhook = async (invoice, payment) => {
  try {
    if (!invoice || !payment) return;

    let { address, received, text, webhook } = invoice;

    if (webhook) {
      let { amount, confirmed, hash, memo, account } = payment;
      let { asset } = account;
      let secret = config.webhooks[webhook];

      l("calling webhook", webhook, amount, hash, address, text);
      let res = await wretch()
        .headers({ "x-webhook": secret })
        .url(webhook)
        .post({
          address,
          amount,
          asset,
          confirmed,
          hash,
          memo,
          received,
          text,
          webhook
        })
        .res();
      return res;
    }
  } catch (e) {
    console.log("problem calling webhook", e);
  }
};
="w"> || {}; if (!fx) return; Object.keys(fx).map((symbol) => { rates[symbol] = msg.c * fx[symbol]; }); rates.IRT = ( (await got("https://api.nobitex.ir/v2/orderbook/BTCIRT").json()) as any ).lastTradePrice; rate = msg.c; s("rate", rate); s("rates", rates); last = Date.now(); } catch (e) { console.log(e); err("binance message error", e.message); } }; ws.onerror = async (error) => { err("binance socket error", error.message); }; return ws; }; export const getFx = async () => { connect(); let date = 0; let fx = await g("fx"); if (fx) ({ date, fx } = fx); if (Date.now() - date > 24 * 60 * 60 * 1000) { date = Date.now(); try { if (config.fixer) { ({ rates: fx } = (await got( `http://data.fixer.io/api/latest?access_key=${config.fixer}&base=USD`, ).json()) as any); } else { ({ fx } = (await got("https://coinos.io/api/fx").json()) as any); } const 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); };