aboutsummaryrefslogtreecommitdiffstats
path: root/lightningPayments.js
blob: 9b3eef789532ccb14e3b30b9f76d5ced59498601 (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
module.exports = (app, db, lna, lnb, emit, payments) => {
  const handlePayment = async msg => {
    if (!msg.settled) return;

    let payment = await db.Payment.findOne({
      include: { model: db.User, as: "user" },
      where: {
        hash: msg.payment_request
      }
    });

    if (!payment) return;

    const { user } = payment;

    payment.received = true;
    user.balance += parseInt(msg.value);
    payment.rate = app.get("rates")[user.currency];
    payment.confirmed = true;
    payment.currency = user.currency;

    await payment.save();
    await user.save();
    payments.push(msg.payment_request);

    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);
};