aboutsummaryrefslogtreecommitdiffstats
path: root/routes/ecash.ts
blob: 35d9f9506aa5b30f97deb43c1147479483292221 (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
import { db, g, s } from "$lib/db";
import { check, claim, get, mint } from "$lib/ecash";
import { err, l } from "$lib/logging";
import { credit, debit, types } from "$lib/payments";
import { emit } from "$lib/sockets";
import { bail, fail } from "$lib/utils";
import { v4 } from "uuid";
const { ecash: type } = types;

Error.stackTraceLimit = 100; // Set this to the desired limit

const sendCash = async ({ amount, user }) => {
  const id = v4();
  const hash = v4();

  const p = await debit({ hash, amount, user, type });
  const token = await mint(parseInt(amount));
  p.memo = id;
  await s(`payment:${p.id}`, p);
  s(`cash:${id}`, token);

  return { id, token };
};

export default {
  async save(req, res) {
    const {
      body: { token },
    } = req;
    try {
      const id = v4();
      await s(`cash:${id}`, token);
      res.send({ id });
    } catch (e) {
      err(e.message);
      bail(res, e.message);
    }
  },

  async get(req, res) {
    const {
      params: { id, version },
    } = req;
    try {
      const token = await get(id, version);
      const status = await check(token);
      res.send({ token, status });
    } catch (e) {
      err(e.message);
      bail(res, e.message);
    }
  },

  async claim(req, res) {
    const {
      body: { token },
      user,
    } = req;
    try {
      const amount = await claim(token);

      let memo;
      const hash = v4();
      const { currency, id: uid } = user;
      const rates = await g("rates");
      await s(`invoice:${hash}`, {
        currency,
        id: hash,
        hash,
        rate: rates[currency],
        uid,
        received: 0,
      });

      await credit({ hash, amount, memo, ref: user.id, type });

      res.send({ ok: true });
    } catch (e) {
      err(e.message);
      bail(res, e.message);
    }
  },

  async mint(req, res) {
    const { body, user } = req;
    const amount = parseInt(body.amount);
    res.send(await sendCash({ amount, user }));
  },

  async melt(req, res) {
    let {
      body: { amount, bolt11: hash, preimage },
      user,
    } = req;
    try {
      amount = Math.round(amount / 1000);
      const ref = preimage;
      const { lightning: type } = types;
      if (user.username !== "mint") fail("unauthorized");
      const { id: uid, currency } = user;
      const ourfee = await db.debit(
        `balance:${uid}`,
        `credit:${type}:${uid}`,
        amount || 0,
        0,
        0,
        0,
      );

      const rates = await g("rates");
      const rate = rates[currency];

      if (ourfee.err) fail(ourfee.err);

      const id = v4();
      const p = {
        id,
        amount: -amount,
        hash,
        ourfee,
        uid,
        confirmed: true,
        rate,
        currency,
        type,
        ref,
        created: Date.now(),
      };

      await s(`payment:${hash}`, id);
      await s(`payment:${id}`, p);
      await db.lPush(`${uid}:payments`, id);

      l(user.username, "sent", type, amount);
      emit(user.id, "payment", p);

      res.send(p);
    } catch (e) {
      bail(res, e.message);
    }
  },
};