aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/invoices.ts4
-rw-r--r--lib/lightning.ts14
-rw-r--r--lib/payments.ts22
-rw-r--r--routes/ecash.ts4
-rw-r--r--routes/invoices.ts12
5 files changed, 36 insertions, 20 deletions
diff --git a/lib/invoices.ts b/lib/invoices.ts
index 178f793a..f6ad65e8 100644
--- a/lib/invoices.ts
+++ b/lib/invoices.ts
@@ -3,7 +3,7 @@ import { db, g, s } from "$lib/db";
import { request } from "$lib/ecash";
import ln from "$lib/ln";
import { emit } from "$lib/sockets";
-import { SATS, bip21, fail, getUser } from "$lib/utils";
+import { SATS, bip21, fail, getInvoice, getUser } from "$lib/utils";
import rpc from "@coinos/rpc";
import { v4 } from "uuid";
@@ -95,7 +95,7 @@ export const generate = async ({ invoice, user }) => {
description: memo || id,
});
- if (await g(`invoice:${r.offer_id}`)) fail("Duplicate offer exists");
+ if (await getInvoice(r.offer_id)) fail("Duplicate offer exists");
hash = r.bolt12;
text = r.bolt12;
diff --git a/lib/lightning.ts b/lib/lightning.ts
index 966a7954..326b5721 100644
--- a/lib/lightning.ts
+++ b/lib/lightning.ts
@@ -3,8 +3,8 @@ import ln from "$lib/ln";
import { err, warn } from "$lib/logging";
import { handleZap } from "$lib/nostr";
import { credit } from "$lib/payments";
-import { getPayment, getUser } from "$lib/utils";
import { PaymentType } from "$lib/types";
+import { getInvoice, getPayment, getUser } from "$lib/utils";
export async function listenForLightning() {
const inv = await ln.waitanyinvoice((await g("pay_index")) || 0);
@@ -27,8 +27,10 @@ export async function listenForLightning() {
try {
if (!preimage) return;
- const id = bolt11 ? (await g(`invoice:${bolt11}`)) : (await g(`invoice:${local_offer_id}`));
- const invoice = await g(`invoice:${id}`);
+ const id = bolt11
+ ? await getInvoice(bolt11)
+ : await getInvoice(local_offer_id);
+ const invoice = await getInvoice(id);
if (!invoice) return warn("received lightning with no invoice", bolt11);
const p = await getPayment(bolt11 || bolt12);
@@ -76,8 +78,10 @@ export async function replay(index) {
try {
if (!preimage) return;
- const id = bolt11 ? (await g(`invoice:${bolt11}`)) : (await g(`invoice:${local_offer_id}`));
- const invoice = await g(`invoice:${id}`);
+ const id = bolt11
+ ? await getInvoice(bolt11)
+ : await getInvoice(local_offer_id);
+ const invoice = await getInvoice(id);
if (!invoice) return warn("received lightning with no invoice", bolt11);
const p = await getPayment(bolt11 || bolt12);
diff --git a/lib/payments.ts b/lib/payments.ts
index 342b9641..07004fa2 100644
--- a/lib/payments.ts
+++ b/lib/payments.ts
@@ -111,9 +111,11 @@ export const debit = async ({
let creditType = type;
if (creditType === PaymentType.bolt12) creditType = PaymentType.lightning;
- let ourfee: any = [PaymentType.bitcoin, PaymentType.liquid, PaymentType.lightning].includes(
- type,
- )
+ let ourfee: any = [
+ PaymentType.bitcoin,
+ PaymentType.liquid,
+ PaymentType.lightning,
+ ].includes(type)
? Math.round((amount + fee + tip) * config.fee)
: 0;
@@ -161,7 +163,8 @@ export const debit = async ({
l(user.username, "sent", type, amount);
//emit(user.id, "payment", p);
- if (![PaymentType.lightning, PaymentType.bolt12].includes(type)) nwcNotify(p, user);
+ if (![PaymentType.lightning, PaymentType.bolt12].includes(type))
+ nwcNotify(p, user);
return p;
};
@@ -237,7 +240,8 @@ export const credit = async ({
items: undefined,
};
- if ([PaymentType.bitcoin, PaymentType.liquid].includes(type)) inv.pending += amount;
+ if ([PaymentType.bitcoin, PaymentType.liquid].includes(type))
+ inv.pending += amount;
else {
inv.received += amount;
inv.preimage = ref;
@@ -258,7 +262,11 @@ export const credit = async ({
let creditType = type;
if (creditType === PaymentType.bolt12) creditType = PaymentType.lightning;
- if ([PaymentType.bitcoin, PaymentType.liquid, PaymentType.lightning].includes(creditType))
+ if (
+ [PaymentType.bitcoin, PaymentType.liquid, PaymentType.lightning].includes(
+ creditType,
+ )
+ )
m.incrBy(`credit:${creditType}:${uid}`, Math.round(amount * config.fee));
m.set(`invoice:${inv.id}`, JSON.stringify(inv))
@@ -430,7 +438,7 @@ export const sendOnchain = async (params) => {
value,
} of tx.vout) {
total += sats(value);
- const invoice = await g(`invoice:${address}`);
+ const invoice = await getInvoice(address);
if (invoice?.aid === aid) fail("Cannot send to internal address");
if ((await node.getAddressInfo(address)).ismine) {
diff --git a/routes/ecash.ts b/routes/ecash.ts
index e4898358..25b7a255 100644
--- a/routes/ecash.ts
+++ b/routes/ecash.ts
@@ -3,7 +3,7 @@ import { check, claim, get, mint } from "$lib/ecash";
import { err, l } from "$lib/logging";
import { credit, debit } from "$lib/payments";
import { emit } from "$lib/sockets";
-import { bail, fail } from "$lib/utils";
+import { bail, fail, getInvoice } from "$lib/utils";
import { getEncodedToken } from "@cashu/cashu-ts";
import { v4 } from "uuid";
@@ -147,7 +147,7 @@ export default {
async receive(req, res) {
try {
const { id, proofs, mint, memo } = req.body;
- const { uid: ref } = await g(`invoice:${id}`);
+ const { uid: ref } = await getInvoice(id);
const amount = await claim(
getEncodedToken({
diff --git a/routes/invoices.ts b/routes/invoices.ts
index 8b71208d..0881c69f 100644
--- a/routes/invoices.ts
+++ b/routes/invoices.ts
@@ -1,5 +1,5 @@
import config from "$config";
-import { db, g } from "$lib/db";
+import { db } from "$lib/db";
import { generate } from "$lib/invoices";
import { err } from "$lib/logging";
import { bail, fields, getInvoice, getUser } from "$lib/utils";
@@ -10,8 +10,7 @@ export default {
const {
params: { id },
} = req;
- let invoice = await g(`invoice:${id}`);
- if (typeof invoice === "string") invoice = await g(`invoice:${invoice}`);
+ const invoice = await getInvoice(id);
if (invoice) {
invoice.secret = undefined;
@@ -31,7 +30,12 @@ export default {
try {
res.send(await generate({ invoice, user }));
} catch (e) {
- err("problem generating invoice", req.user?.username, body.user?.username, e.message);
+ err(
+ "problem generating invoice",
+ req.user?.username,
+ body.user?.username,
+ e.message,
+ );
bail(res, e.message);
}
},