From 841e5434fe3b53bc35b0c5e22e7589b47aac5dc7 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Mon, 8 May 2023 16:59:05 -0700 Subject: withdraw endpoint --- index.js | 1 + routes/lnurl.js | 3 +++ routes/payments.js | 35 +++++++++++++++++++++++++---------- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 6e001025..a799ab30 100644 --- a/index.js +++ b/index.js @@ -53,6 +53,7 @@ app.get("/payments", auth, payments.list); app.get("/payments/:hash", auth, payments.get); app.post("/parse", payments.parse); app.get("/pot/:name", payments.pot); +app.get("/pot/:name/withdraw", payments.withdraw); app.post("/take", auth, payments.take); app.post("/buy", auth, payments.buy); app.post("/print", auth, payments.print); diff --git a/routes/lnurl.js b/routes/lnurl.js index 14f848a4..146ac199 100644 --- a/routes/lnurl.js +++ b/routes/lnurl.js @@ -89,6 +89,9 @@ export default { } }, + async lnurlw({ params: { k1, pr } }, res) { + }, + async lnurl({ params: { id }, query: { amount } }, res) { let pr = await g(`lnurlp:${id}`); diff --git a/routes/payments.js b/routes/payments.js index 282a8682..874ed6e1 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -192,8 +192,21 @@ export default { res.send({ amount, payments }); }, - async take({ body: { name, amount }, user }, res) { + async withdraw({ params: { name } }, res) { + let maxWithdrawable = await g(`pot:${name}`); + res.send({ + tag: "withdrawRequest", + callback: `${URL}/lnurlw/${name}`, + k1: name, + defaultDescription: `Withdraw from coinos pot ${name}`, + minWithdrawable: 0, + maxWithdrawable + }); + }, + + async take({ body: { name, amount, hash }, user }, res) { amount = parseInt(amount); + await t(`pot:${name}`, async (balance, db) => { if (balance < amount) fail("Insufficient funds"); await db @@ -202,16 +215,18 @@ export default { .exec(); }); - let hash = v4(); - let { currency } = user; - await s(`invoice:${hash}`, { - currency, - rate: store.rates[currency], - uid: user.id, - received: 0 - }); + if (!hash) { + hash = v4(); + let { currency } = user; + await s(`invoice:${hash}`, { + currency, + rate: store.rates[currency], + uid: user.id, + received: 0 + }); + } - let payment = await credit(hash, amount, "", name, types.pot); + let payment = await credit(hash, amount, name, name, types.pot); await db.lPush(`pot:${name}:payments`, hash); res.send({ payment }); -- cgit v1.2.3