aboutsummaryrefslogtreecommitdiffstats
path: root/routes/payments.js
blob: 416bc20a115d55fb6d8c0460c22b1b4357edaddc (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import app from "$app";
import db from "$db";
import config from "$config";
import store from "$lib/store";

import { auth, adminAuth, optionalAuth } from "$lib/passport";
import fs from "fs";
import { join } from "path";
import { Op } from "@sequelize/core";
import send from "./send";
import { l, warn } from "$lib/logging";
import { fail } from "$lib/utils";

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

import btcRoutes from "./bitcoin/index";
import lnRoutes from "./lightning/index";
import lqRoutes from "./liquid/index";

import "./bitcoin/receive";
import "./lightning/receive";
import "./liquid/receive";

app.post("/send", auth, send);
app.post("/sendToTokenHolders", auth, async (req, res, next) => {
  let { asset, amount } = req.body;

  let accounts = await db.Account.findAll({
    where: {
      asset,
      "$user.username$": { [Op.ne]: "gh" }
    },

    include: [{ model: db.User, as: "user" }]
  });

  let totalTokens = accounts.reduce((a, b) => a + b.balance, 0);

  let totalSats = Math.floor(amount / totalTokens);

  if (totalSats < 1) throw new Error("amount is too low to distribute");

  for (let i = 0; i < accounts.length; i++) {
    let account = accounts[i];
    console.log(account.user.username, account.balance);
  }
  accounts.map(({ balance, user: { username } }) => ({ username, balance }));

  res.send({ success: "it worked" });
});

app.get("/except", adminAuth, (req, res) => {
  let s = fs.createWriteStream("exceptions", { flags: "a" });
  store.unaccounted.map(tx => s.write(tx.txid + "\n"));
  l("updated exceptions");
  res.send("updated exceptions");
});

app.post("/lightning/parse", lnRoutes.parse);
app.post("/lightning/channel", lnRoutes.channel);
app.post("/lightning/query", auth, lnRoutes.query);
app.post("/lightning/send", auth, lnRoutes.send);

if (config.lnurl) {
  let { channelRequest } = await import("$routes/lightning/channelRequest");
  app.post("/lightning/channelRequest", channelRequest);
}

app.post("/bitcoin/broadcast", optionalAuth, btcRoutes.broadcast);
app.get("/bitcoin/generate", auth, btcRoutes.generate);
app.post("/bitcoin/sweep", auth, btcRoutes.sweep);
app.post("/bitcoin/fee", auth, btcRoutes.fee);
app.post("/bitcoin/send", auth, btcRoutes.send);

setTimeout(async () => {
  try {
    const address = await bc.getNewAddress();
    const { hdkeypath } = await bc.getAddressInfo(address);
    const parts = hdkeypath.split("/");
    store.bcAddressIndex = parts[parts.length - 1].replace("'", "");
  } catch (e) {
    console.error(e);
  }
}, 50);

app.post("/liquid/broadcast", optionalAuth, lqRoutes.broadcast);
app.get("/liquid/generate", auth, lqRoutes.generate);
app.post("/liquid/fee", auth, lqRoutes.fee);
app.post("/liquid/send", auth, lqRoutes.send);

setTimeout(async () => {
  try {
    const address = await lq.getNewAddress();
    const { hdkeypath } = await lq.getAddressInfo(address);
    const parts = hdkeypath.split("/");
    store.lqAddressIndex = parts[parts.length - 1].slice(0, -1);
  } catch (e) {
    warn("Problem getting liquid address index", e.message);
  }
}, 50);

app.get("/payments", auth, async (req, res) => {
  let { start, end, limit, offset, v2 } = req.query;

  if (limit) limit = parseInt(limit);
  if (offset) offset = parseInt(offset);

  let where = {
    account_id: req.user.account_id,
  };

  if (start || end) where.createdAt = {};
  if (start) where.createdAt[Op.gte] = new Date(parseInt(start));
  if (end) where.createdAt[Op.lte] = new Date(parseInt(end));

  if (!req.user.account_id) return res.send([]);

  let total = await db.Payment.count({ where });

  let payments = await db.Payment.findAll({
    where,
    order: [["id", "DESC"]],
    include: {
      model: db.Account,
      as: "account"
    },
    include: {
      attributes: ["username", "profile"],
      model: db.User,
      as: "with"
    },
    limit,
    offset
  });

  if (v2) {
    res.send({ transactions: payments, total });
  } else {
    res.send(payments);
  }
});

app.get("/payment/:redeemcode", async (req, res) => {
  try {
    const { redeemcode } = req.params;
    let payment = await db.Payment.findOne({
      where: {
        redeemcode
      },
      include: {
        model: db.Account,
        as: "account"
      }
    });

    if (!payment) fail("invalid code");

    res.send(payment);
  } catch (e) {
    res.code(500).send(e.message);
  }
});