aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-08-19 08:50:07 -0700
committerAdam Soltys <[email protected]>2024-08-19 08:50:07 -0700
commit00f0cda6a4c1ace9eea435e0bcc40a6004d70fdf (patch)
tree86a66ab07f760d13c695a6e8acd0bf8396d07eab
parent95e543d1060ba7f0cd5b249b4dacceb977e23df8 (diff)
downloadcoinos-server-00f0cda6a4c1ace9eea435e0bcc40a6004d70fdf.tar.gz
coinos-server-00f0cda6a4c1ace9eea435e0bcc40a6004d70fdf.zip
melt endpoint
-rw-r--r--index.js1
-rw-r--r--routes/ecash.js52
2 files changed, 52 insertions, 1 deletions
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);
+ }
+ },
};