aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-12-24 00:47:08 -0800
committerAdam Soltys <[email protected]>2024-12-24 00:47:12 -0800
commit4a9d5e1f40be289970f3dec2006852b160ae8e41 (patch)
treef6feb393c91e79048a7ad6ca4c69381d1a1e29f6
parent47236bc1ac522aa14e280e1a9664b3bd90e761db (diff)
downloadcoinos-server-4a9d5e1f40be289970f3dec2006852b160ae8e41.tar.gz
coinos-server-4a9d5e1f40be289970f3dec2006852b160ae8e41.zip
lnurlw
-rw-r--r--index.ts1
-rw-r--r--routes/lnurl.ts51
-rw-r--r--routes/payments.ts28
3 files changed, 64 insertions, 16 deletions
diff --git a/index.ts b/index.ts
index 8af31a85..36dd4c9f 100644
--- a/index.ts
+++ b/index.ts
@@ -77,6 +77,7 @@ app.get("/decode", lnurl.decode);
app.get("/lnurl/verify/:id", lnurl.verify);
app.get("/lnurlp/:username", lnurl.lnurlp);
app.get("/lnurl/:id", lnurl.lnurl);
+app.get("/lnurlw", lnurl.withdraw);
app.post("/freeze", payments.freeze);
diff --git a/routes/lnurl.ts b/routes/lnurl.ts
index 11b81273..8aeeac6f 100644
--- a/routes/lnurl.ts
+++ b/routes/lnurl.ts
@@ -1,9 +1,11 @@
-import { g, s } from "$lib/db";
+import { db, g, s } from "$lib/db";
import { generate } from "$lib/invoices";
+import ln from "$lib/ln";
import { err, warn } from "$lib/logging";
import { serverPubkey } from "$lib/nostr";
+import { credit } from "$lib/payments";
import { types } from "$lib/payments";
-import { bail, fail, getUser } from "$lib/utils";
+import { bail, fail, getInvoice, getUser } from "$lib/utils";
import { bech32 } from "bech32";
import got from "got";
import { v4 } from "uuid";
@@ -137,8 +139,7 @@ export default {
const {
params: { id },
} = req;
- const inv = await g(`invoice:${id}`);
-
+ const inv = await getInvoice(id);
if (!inv) return res.send({ status: "ERROR", reason: "Not found" });
const { hash, received, amount, preimage } = inv;
@@ -146,4 +147,46 @@ export default {
res.send({ pr: hash, status: "OK", settled, preimage: preimage || null });
},
+
+ async withdraw(req, res) {
+ try {
+ const { pr, k1: id } = req.query;
+ const invoice = await getInvoice(pr);
+ const { amount_msat } = await ln.decode(pr);
+ const amount = Math.ceil(amount_msat / 1000);
+ await db.debit(`fund:${id}`, "", amount, 0, 0, 0);
+ let p;
+ if (invoice) {
+ const { hash } = invoice;
+ p = await credit({ hash, amount, memo: id, ref: id, type: types.fund });
+ } else {
+ await ln.xpay({
+ invstring: pr.replace(/\s/g, "").toLowerCase(),
+ maxfee: 0,
+ retry_for: 10,
+ });
+ const rates = await g("rates");
+ p = {
+ id: v4(),
+ amount: -amount,
+ hash: pr,
+ confirmed: true,
+ rate: rates.USD,
+ currency: "USD",
+ type: types.fund,
+ ref: id,
+ created: Date.now(),
+ };
+ await s(`payment:${p.id}`, p);
+ await s(`payment:${pr}`, p);
+ }
+
+ await db.lPush(`fund:${id}:payments`, p.id);
+ res.send({ status: "OK" });
+ } catch (e) {
+ console.log(e);
+ warn("lnurlw failed", e.message);
+ res.send({ status: "ERROR", reason: "Withdrawal failed" });
+ }
+ },
};
diff --git a/routes/payments.ts b/routes/payments.ts
index 1f919cf4..665e0014 100644
--- a/routes/payments.ts
+++ b/routes/payments.ts
@@ -30,6 +30,7 @@ import got from "got";
import { v4 } from "uuid";
import ln from "$lib/ln";
+const { URL } = process.env;
export default {
async info(_, res) {
@@ -232,14 +233,14 @@ export default {
const {
params: { name },
} = req;
- const maxWithdrawable = await g(`fund:${name}`);
+ const balance = await g(`fund:${name}`);
res.send({
tag: "withdrawRequest",
- callback: `${URL}/lnurlw/${name}`,
+ callback: `${URL}/api/lnurlw`,
k1: name,
defaultDescription: `Withdraw from coinos fund ${name}`,
- minWithdrawable: 0,
- maxWithdrawable,
+ minWithdrawable: balance * 1000,
+ maxWithdrawable: balance * 1000,
});
},
@@ -249,39 +250,42 @@ export default {
user,
} = req;
try {
+ const { id: uid } = user;
amount = parseInt(amount);
if (amount < 0) fail("Invalid amount");
- await t(`fund:${id}`, async (balance, db) => {
- if (balance < amount) fail("Insufficient funds");
- await db.multi().decrBy(`fund:${id}`, amount).exec();
- });
+ await db.debit(`fund:${id}`, "", amount, 0, 0, 0);
+
+ const rates = await g("rates");
+ const { currency } = user;
+ const rate = rates[currency];
if (!iid) {
iid = v4();
- const rates = await g("rates");
- const { currency } = user;
await s(`invoice:${iid}`, {
currency,
id: iid,
hash: iid,
- rate: rates[currency],
- uid: user.id,
+ rate,
+ uid,
received: 0,
});
}
const payment = await credit({
+ aid: user.id,
hash: iid,
amount,
memo: id,
ref: id,
type: types.fund,
});
+
await db.lPush(`fund:${id}:payments`, payment.id);
res.send({ payment });
} catch (e) {
+ console.log(e);
warn("problem withdrawing from fund", e.message);
bail(res, e.message);
}