aboutsummaryrefslogtreecommitdiffstats
path: root/routes/balances.js
blob: 78567c441cd2dab6165ddff60dbbb1871715ce40 (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
const sequelize = require("sequelize");
const { Op } = sequelize;

app.get("/info", async (req, res) => {
  const assets = await lq.getBalance();
  const accounts = await db.Account.findAll({
    attributes: [
      "asset",
      [sequelize.fn("sum", sequelize.col("balance")), "total"]
    ],
    group: ["asset"],
  });

  let lnchannel; 
  let lnwallet;
  let lninfo;

  if (config.lna.clightning) {
    lninfo = await lna.getinfo();
    const funds = await lna.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 {
    lninfo = await lna.getInfo({});
    lnchannel = parseInt((await lna.channelBalance({})).balance);
    lnwallet = parseInt((await lna.walletBalance({})).total_balance);
  } 

  const info = {
    bitcoind: await bc.getNetworkInfo(),
    elementsd: await lq.getNetworkInfo(),
    accounts,
    assets,
    bitcoin: parseInt((await bc.getBalance()) * SATS),
    liquid: parseInt(assets.bitcoin * SATS),
    lnchannel,
    lnwallet,
  };

  const { bitcoin, liquid, user } = info;

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

  info.ratio = (info.total / parseInt(user)).toFixed(2);

  res.send(info);
});