aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2025-03-05 13:35:31 -0800
committerAdam Soltys <[email protected]>2025-03-05 15:06:48 -0800
commitbbdae13248476e9ff49fcefc64cf8c8e97b5f679 (patch)
treecb3696e680f7417f5988375043341e495b7cad3d /lib
parent118386e0691a0c80a0d8c19ab40bd3abf6859b84 (diff)
downloadcoinos-server-bbdae13248476e9ff49fcefc64cf8c8e97b5f679.tar.gz
coinos-server-bbdae13248476e9ff49fcefc64cf8c8e97b5f679.zip
use getInvoice helper
Diffstat (limited to 'lib')
-rw-r--r--lib/invoices.ts4
-rw-r--r--lib/lightning.ts14
-rw-r--r--lib/payments.ts22
3 files changed, 26 insertions, 14 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) {