aboutsummaryrefslogtreecommitdiffstats
path: root/lib/sendInternal.js
blob: f801c9cd7abb1c358f01c5b79f1f961cedac063b (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
import config from "$config";
import { emit } from "$lib/sockets";
import store from "$lib/store";
import { callWebhook } from "$lib/webhooks";
import { v4 } from "uuid";
import { notify } from "$lib/notifications";
import { l, err } from "$lib/logging";
import { fail } from "$lib/utils";
import ln from "$lib/ln";
import { g, s, rd } from "$lib/redis";

const { HOSTNAME: hostname } = process.env;

export default async (
  { amount, address, payreq, unconfidential, memo, username, tip },
  user
) => {
  amount = parseInt(amount);

  if (!amount || amount < 0) fail("Amount must be greater than zero");

  let fee = 0;
  let recipient;

  let t = async () => {
    await rd.watch(`user:${user_id}`);
    user = await g(`user:${user_id}`);
    if (user.balance < amount) throw new Error("Insufficient funds");

    user.balance -= amount;
    let m = await rd.multi();
    await s(`user:${user.id}`, user);

    if (username && (recipient = await g(`user:${username}`))) {
      let { payment_hash: h } = await ln.decode(payreq);
      let invoice = payreq && (await g(`invoice:${h}`));

      let t = async () => {
        await rd.watch(`user:${recipient.id}`);
        recipient = await g(`user:${recipient.id}`);
        recipient.balance += amount;
        let m = await rd.multi();
        await s(`user:${recipient.id}`, recipient);
        if (!(await m.exec())) t();
      };
      await t();

      if (!(await m.exec())) t();
    }
    await t();

    let p2 = {
      id: v4(),
      amount,
      user_id: recipient.id,
      rate: store.rates[recipient.currency],
      currency: recipient.currency,
      memo,
      type: "internal",
      with_id: user.id,
      created_at: Date.now()
    };

    if (invoice) {
      let { tip } = invoice;
      params.invoice_id = invoice.id;
      if (tip) {
        params.tip = tip;
        params.amount -= tip;
      }

      invoice.received += amount;
      await s(`invoice:${h}`, invoice);
    }

    if (invoice) p2.invoice = invoice;
    await s(`payment:${p2.id}`, p2);
    await rd.lPush(`${recipient.id}:payments`, p2.id);

    emit(recipient.username, "payment", p2);

    l("received internal", recipient.username, amount);
    notify(recipient, `Received ${amount} sats`);
    callWebhook(invoice, p2);
  };

  let p1 = {
    id: v4(),
    amount: -amount,
    memo,
    user_id: user.id,
    rate: store.rates[user.currency],
    currency: user.currency,
    with_id: recipient && recipient.id,
    hash: `#${v4().substr(0, 6)} ${
      username ? `Payment to ${username}` : "Internal Transfer"
    }`,
    type: "internal",
    created_at: Date.now()
  };

  if (!username) {
    l("creating redeemable payment");
    params.redeemcode = v4();
    params.hash = `${hostname}/redeem/${params.redeemcode}`;
  }

  await s(`payment:${p1.id}`, p1);
  await rd.lPush(`${user.id}:payments`, p1.id);

  l("sent internal", user.username, -p1.amount);

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

  return p1;
};