aboutsummaryrefslogtreecommitdiffstats
path: root/routes/info.js
blob: f3de8907448823921b04e9aa5c860d8ef54e7b94 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import app from "$app";
import db from "$db";
import config from "$config";
import store from "$lib/store";

import bc from "$lib/bitcoin";
import lq from "$lib/liquid";
import lnd from "$lib/lnd";
import ln from "$lib/ln";

import { getChannelBalance, getChainBalance } from "lightning";
import sequelize from "@sequelize/core";
import { SATS } from "$lib/utils";

const { Op } = sequelize;

/**
 * @api {get} /info Request server information
 * @apiName GetInfo
 * @apiGroup Info
 *
 * @apiSuccess {Object} fx Exchange rates relative to USD - fx.CUR is the value of one CUR in USD.
 * @apiSuccess {String[]} networks Array of supported networks; possible values are "bitcoin", "liquid" and "lightning".
 * @apiSuccess {String} clientVersion the current git commit of the ui
 */
app.get("/info", async (req, res, next) => {
  const { clientVersion } = config;

  const info = {
    fx: store.fx,
    nodes: store.networks,
    clientVersion
  };

  res.send(info);
});

app.get("/balances", async (req, res, next) => {
  try {
    const accounts = await db.Account.findAll({
      where: {
        pubkey: null
      },
      attributes: [
        "asset",
        [sequelize.fn("sum", sequelize.col("balance")), "total"]
      ],
      group: ["asset"]
    });

    let lnchannel;
    let lnwallet;

    if (config.lna) {
      if (config.lna.clightning) {
        const funds = await ln.listfunds();
        lnchannel = parseInt(
          funds.channels.reduce((a, b) => a + b.channel_sat, 0)
        );
        lnwallet = parseInt(funds.outputs.reduce((a, b) => a + b.value, 0));
      } else {
        lnchannel = parseInt(await getChannelBalance({ lnd }).channel_balance);
        lnwallet = parseInt(await getChainBalance({ lnd }).chain_balance);
      }
    }

    let bitcoin, bitcoind;
    if (config.bitcoin) {
      bitcoin = parseInt((await bc.getBalance()) * SATS);
      bitcoind = await bc.getNetworkInfo();
    }

    let assets, liquid, elementsd;
    if (config.liquid) {
      assets = await lq.getBalance();
      liquid = parseInt(assets.bitcoin * SATS);
      elementsd = await lq.getNetworkInfo();
    }

    const info = {
      bitcoind,
      elementsd,
      accounts,
      assets,
      bitcoin,
      liquid,
      lnchannel,
      lnwallet
    };

    info.total =
      parseInt(bitcoin) +
      parseInt(liquid) +
      parseInt(lnchannel) +
      parseInt(lnwallet);

    res.send(info);
  } catch (e) {
    console.log("problem getting balances", e);
  }
});