From bbdae13248476e9ff49fcefc64cf8c8e97b5f679 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Wed, 5 Mar 2025 13:35:31 -0800 Subject: use getInvoice helper --- lib/invoices.ts | 4 ++-- lib/lightning.ts | 14 +++++++++----- lib/payments.ts | 22 +++++++++++++++------- 3 files changed, 26 insertions(+), 14 deletions(-) (limited to 'lib') 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) { -- cgit v1.2.3