From 16d3972258759b168fdf9c69f790ea4bca9f07ae Mon Sep 17 00:00:00 2001 From: Juan P Lopez Date: Fri, 9 Feb 2024 13:59:44 -0500 Subject: [PATCH] fix(trigger): early return on lnd connection issues --- .../wallets/update-single-pending-invoice.ts | 40 ++++++++++++------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/core/api/src/app/wallets/update-single-pending-invoice.ts b/core/api/src/app/wallets/update-single-pending-invoice.ts index 4bd342de09..c2b10777d3 100644 --- a/core/api/src/app/wallets/update-single-pending-invoice.ts +++ b/core/api/src/app/wallets/update-single-pending-invoice.ts @@ -96,7 +96,11 @@ const updatePendingInvoiceBeforeFinally = async ({ }) const lndService = LndService() - if (lndService instanceof Error) return lndService + if (lndService instanceof Error) { + pendingInvoiceLogger.error("Unable to initialize LndService") + recordExceptionInCurrentSpan({ error: lndService }) + return false + } const lnInvoiceLookup = await lndService.lookupInvoice({ pubkey, paymentHash }) if (lnInvoiceLookup instanceof InvoiceNotFoundError) { const processingCompletedInvoice = @@ -226,19 +230,6 @@ const lockedUpdatePendingInvoiceSteps = async ({ "invoices.finalRecipient": JSON.stringify(recipientWalletDescriptor), }) - if (!isSettledInLnd) { - const lndService = LndService() - if (lndService instanceof Error) return lndService - const invoiceSettled = await lndService.settleInvoice({ - pubkey: walletInvoiceInsideLock.pubkey, - secret: walletInvoiceInsideLock.secret, - }) - if (invoiceSettled instanceof Error) return invoiceSettled - } - - const invoicePaid = await walletInvoices.markAsPaid(paymentHash) - if (invoicePaid instanceof Error) return invoicePaid - const recipientAccount = await AccountsRepository().findById(recipientAccountId) if (recipientAccount instanceof Error) return recipientAccount const { displayCurrency: recipientDisplayCurrency } = recipientAccount @@ -280,6 +271,27 @@ const lockedUpdatePendingInvoiceSteps = async ({ displayCurrency: recipientDisplayCurrency, }) + if (!isSettledInLnd) { + const lndService = LndService() + if (lndService instanceof Error) { + logger.error("Unable to initialize LndService") + recordExceptionInCurrentSpan({ error: lndService }) + return false + } + const invoiceSettled = await lndService.settleInvoice({ + pubkey: walletInvoiceInsideLock.pubkey, + secret: walletInvoiceInsideLock.secret, + }) + if (invoiceSettled instanceof Error) { + logger.error({ paymentHash }, "Unable to settleInvoice") + recordExceptionInCurrentSpan({ error: invoiceSettled }) + return false + } + } + + const invoicePaid = await walletInvoices.markAsPaid(paymentHash) + if (invoicePaid instanceof Error) return invoicePaid + //TODO: add displayCurrency: displayPaymentAmount.currency, const journal = await LedgerFacade.recordReceiveOffChain({ description,