Skip to content

Commit

Permalink
refactor: clean up invoiceUpdateHandler usage
Browse files Browse the repository at this point in the history
  • Loading branch information
vindard committed Sep 26, 2023
1 parent 3a5dc8a commit adadcef
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions src/servers/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,6 @@ export const onchainBlockEventHandler = async (height: number) => {
logger.info(`finish block ${height} handler with ${txNumber} transactions`)
}

export const invoiceUpdateEventHandler = async (
invoice: SubscribeToInvoiceInvoiceUpdatedEvent | GetInvoiceResult,
): Promise<boolean | ApplicationError> => {
logger.info({ invoice }, "invoiceUpdateEventHandler")
return invoice.is_held
? WalletWithSpans.updatePendingInvoiceByPaymentHash({
paymentHash: invoice.id as PaymentHash,
logger,
})
: false
}

export const publishCurrentPrices = async () => {
const currencies = await PricesWithSpans.listCurrencies()
if (currencies instanceof Error) {
Expand Down Expand Up @@ -228,6 +216,22 @@ const listenerOnchain = (lnd: AuthenticatedLnd) => {
})
}

const invoiceUpdateHandler = wrapAsyncToRunInSpan({
root: true,
namespace: "servers.trigger",
fn: async (
invoice: SubscribeToInvoiceInvoiceUpdatedEvent | GetInvoiceResult,
): Promise<boolean | ApplicationError> => {
logger.info({ invoice }, "invoiceUpdateEventHandler")
return invoice.is_held
? WalletWithSpans.updatePendingInvoiceByPaymentHash({
paymentHash: invoice.id as PaymentHash,
logger,
})
: false
},
})

const listenerHodlInvoice = ({
lnd,
paymentHash,
Expand All @@ -236,21 +240,21 @@ const listenerHodlInvoice = ({
paymentHash: PaymentHash
}) => {
const subInvoice = subscribeToInvoice({ lnd, id: paymentHash })
const invoiceUpdateHandler = wrapAsyncToRunInSpan({
root: true,
namespace: "servers.trigger",
fn: invoiceUpdateEventHandler,
})

subInvoice.on(
"invoice_updated",
async (invoice: SubscribeToInvoiceInvoiceUpdatedEvent) => {
async (
invoice: SubscribeToInvoiceInvoiceUpdatedEvent,
): Promise<boolean | ApplicationError> => {
if (invoice.is_confirmed || invoice.is_canceled) {
subInvoice.removeAllListeners()
} else {
await invoiceUpdateHandler(invoice)
return true
}

return invoiceUpdateHandler(invoice)
},
)

subInvoice.on("error", (err) => {
baseLogger.info({ err }, "error subChannels")
recordExceptionInCurrentSpan({
Expand Down

0 comments on commit adadcef

Please sign in to comment.