From 00f0cda6a4c1ace9eea435e0bcc40a6004d70fdf Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Mon, 19 Aug 2024 08:50:07 -0700 Subject: melt endpoint --- index.js | 1 + routes/ecash.js | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index d280ee41..919ee5e5 100644 --- a/index.js +++ b/index.js @@ -120,6 +120,7 @@ app.post("/unlimit", admin, users.unlimit); app.post("/claim", auth, ecash.claim); app.post("/mint", auth, ecash.mint); +app.post("/melt", auth, ecash.melt); let host = process.env.HOST || "0.0.0.0"; let port = process.env.PORT || 3119; diff --git a/routes/ecash.js b/routes/ecash.js index e1877440..eb69798f 100644 --- a/routes/ecash.js +++ b/routes/ecash.js @@ -1,8 +1,10 @@ -import { g, s } from "$lib/db"; +import { g, s, db } from "$lib/db"; import { claim, mint } from "$lib/ecash"; import { bail } from "$lib/utils"; import { debit, credit, types } from "$lib/payments"; import { v4 } from "uuid"; +import { l, err, warn } from "$lib/logging"; +import { emit } from "$lib/sockets"; import store from "$lib/store"; let { ecash: type } = types; @@ -46,4 +48,52 @@ export default { bail(res, e.message); } }, + + async melt({ body: { amount, bolt11: hash, preimage }, user }, res) { + try { + amount = Math.round(amount / 1000); + let ref = preimage; + let { lightning: type } = types; + if (user.username !== "mint") fail("unauthorized"); + let { id: uid, currency, username } = user; + let ourfee = await db.debit( + `balance:${uid}`, + `credit:${type}:${uid}`, + amount || 0, + 0, + 0, + 0, + ); + + let rate = store.rates[currency]; + + if (ourfee.err) fail(ourfee.err); + + let id = v4(); + let 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); + } + }, }; -- cgit v1.2.3