aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2025-07-25 23:17:04 -0700
committerAdam Soltys <[email protected]>2025-07-25 23:17:04 -0700
commit4b59d73b96987b3335f9b87a1723f6f439288493 (patch)
treeebc7ab03bccf03b4ad44872e89aad54194edbaf7
parent6b21771f95885a73f317381a33a3dbac8b6144f9 (diff)
downloadcoinos-server-4b59d73b96987b3335f9b87a1723f6f439288493.tar.gz
coinos-server-4b59d73b96987b3335f9b87a1723f6f439288493.zip
fund authorizations
-rw-r--r--routes/payments.ts83
1 files changed, 47 insertions, 36 deletions
diff --git a/routes/payments.ts b/routes/payments.ts
index c2921fcb..d8dc354b 100644
--- a/routes/payments.ts
+++ b/routes/payments.ts
@@ -2,6 +2,7 @@ import config from "$config";
import api from "$lib/api";
import { requirePin } from "$lib/auth";
import { db, g, s } from "$lib/db";
+import { generate } from "$lib/invoices";
import ln from "$lib/ln";
import { err, l, warn } from "$lib/logging";
import mqtt from "$lib/mqtt";
@@ -267,12 +268,19 @@ export default {
user,
} = req;
try {
- const { id: uid } = user;
amount = parseInt(amount);
if (amount < 0) fail("Invalid amount");
const rates = await g("rates");
+ if (!iid) {
+ const inv = await generate({
+ invoice: { amount, type: "lightning" },
+ user,
+ });
+ iid = inv.id;
+ }
+
const authorization = await g(`authorization:${id}`);
if (authorization) {
const { currency, fiat } = authorization;
@@ -280,49 +288,52 @@ export default {
const sender = await getUser(authorization.uid);
await db.del(`authorization:${id}`);
- res.send(await sendInternal({ amount, sender, recipient: user }));
- } else {
- const managers = await db.sMembers(`fund:${id}:managers`);
- if (managers.length && !managers.includes(user.id))
- fail("Unauthorized");
-
- const result: any = await db.debit(
- `fund:${id}`,
- "",
- "Insufficient funds",
- amount,
- 0,
- 0,
- 0,
- 0,
- );
- if (result.err) fail(result.err);
- if (!iid) {
- iid = v4();
- await s(`invoice:${iid}`, {
- currency: user.currency,
- id: iid,
- hash: iid,
- rate: rates[user.currency],
- uid,
- received: 0,
- });
- }
+ const { hash } = await generate({
+ invoice: { amount, type: "lightning" },
+ user: sender,
+ });
- const payment = await credit({
- aid: user.id,
- hash: iid,
+ const { id: pid } = await debit({
+ hash,
amount,
memo: id,
- ref: id,
+ user: sender,
type: PaymentType.fund,
});
- await db.lPush(`fund:${id}:payments`, payment.id);
-
- res.send({ payment });
+ await db.incrBy(`fund:${id}`, amount);
+ await db.lPush(`fund:${id}:payments`, pid);
+ l("funded fund", id);
}
+
+ const managers = await db.sMembers(`fund:${id}:managers`);
+ if (managers.length && !managers.includes(user.id)) fail("Unauthorized");
+
+ const result: any = await db.debit(
+ `fund:${id}`,
+ "",
+ "Insufficient funds",
+ amount,
+ 0,
+ 0,
+ 0,
+ 0,
+ );
+ if (result.err) fail(result.err);
+
+ const payment = await credit({
+ aid: user.id,
+ hash: iid,
+ amount,
+ memo: id,
+ ref: id,
+ type: PaymentType.fund,
+ });
+
+ await db.lPush(`fund:${id}:payments`, payment.id);
+
+ res.send(payment);
} catch (e) {
warn("problem withdrawing from fund", user.username, e.message);
bail(res, e.message);