aboutsummaryrefslogtreecommitdiffstats
path: root/routes/lightning/receive.js
blob: 6ac4c947d2ffc0198ed37e9476d1ca5d0ab51a2b (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
const asset = "LNBTC";

const handlePayment = async msg => {
  if (!msg.settled) return;

  const invoice = await db.Invoice.findOne({
    include: { model: db.User, as: "user" },
    where: {
      text: msg.payment_request
    }
  });

  if (!invoice) return;

  const { text: hash, currency, rate, tip, user } = invoice;
  const amount = parseInt(msg.value) - tip;

  const payment = await db.Payment.create({
    user_id: user.id,
    hash,
    amount,
    currency,
    rate,
    received: true,
    confirmed: true,
    asset,
    tip,
  });

  invoice.received += amount + tip;
  user.balance += amount + tip;

  await invoice.save();
  await payment.save();
  await user.save();
  payments.push(msg.payment_request);
  l.info("lightning payment received", user.username, payment.amount, payment.tip);

  emit(user.username, "payment", payment);
  emit(user.username, "user", user);
};

const invoices = lna.subscribeInvoices({});
invoices.on("data", handlePayment);

const invoicesb = lnb.subscribeInvoices({});
invoicesb.on("data", handlePayment);