diff --git a/src/app/lightning/payment-status-checker.ts b/src/app/lightning/payment-status-checker.ts index 59c76f3c42b..bc7f2c8cca3 100644 --- a/src/app/lightning/payment-status-checker.ts +++ b/src/app/lightning/payment-status-checker.ts @@ -1,5 +1,6 @@ import { RepositoryError } from "@domain/errors" import { decodeInvoice } from "@domain/bitcoin/lightning" +import { LnTxRecorded } from "@domain/ledger" import { LedgerService } from "@services/ledger" export const PaymentStatusChecker = async (uncheckedPaymentRequest: string) => { @@ -11,10 +12,10 @@ export const PaymentStatusChecker = async (uncheckedPaymentRequest: string) => { return { paymentHash, invoiceIsPaid: async (): Promise => { - const ledger = LedgerService() - const recorded = await ledger.isLnTxRecorded(paymentHash) + const recorded = await LedgerService().isLnTxRecorded(paymentHash) if (recorded instanceof Error) return recorded - return recorded + + return recorded === LnTxRecorded.TRUE }, } } diff --git a/src/app/payments/send-lightning.ts b/src/app/payments/send-lightning.ts index 432f1f13a4f..d44930d4ff8 100644 --- a/src/app/payments/send-lightning.ts +++ b/src/app/payments/send-lightning.ts @@ -17,6 +17,7 @@ import { LnPaymentRequestZeroAmountRequiredError, PriceRatio, } from "@domain/payments" +import { LnTxRecorded } from "@domain/ledger" import { WalletCurrency } from "@domain/shared" import { checkedToWalletId, @@ -367,7 +368,7 @@ const executePaymentViaIntraledger = async < const recorded = await ledgerService.isLnTxRecorded(paymentHash) if (recorded instanceof Error) return recorded - if (recorded) return PaymentSendStatus.AlreadyPaid + if (recorded === LnTxRecorded.TRUE) return PaymentSendStatus.AlreadyPaid const balance = await ledgerService.getWalletBalanceAmount(senderWallet) if (balance instanceof Error) return balance diff --git a/src/app/payments/update-pending-payments.ts b/src/app/payments/update-pending-payments.ts index 7b1b8648b4f..e5431a1df5a 100644 --- a/src/app/payments/update-pending-payments.ts +++ b/src/app/payments/update-pending-payments.ts @@ -5,6 +5,7 @@ import { CouldNotFindTransactionError, inputAmountFromLedgerTransaction, LedgerTransactionType, + LnTxRecorded, UnknownLedgerError, } from "@domain/ledger" import { MissingPropsInTransactionForPaymentFlowError } from "@domain/payments" @@ -151,7 +152,7 @@ const updatePendingPayment = wrapAsyncToRunInSpan({ return recorded } - if (recorded) { + if (recorded === LnTxRecorded.TRUE) { paymentLogger.info("payment has already been processed") return true } diff --git a/src/domain/ledger/index.ts b/src/domain/ledger/index.ts index 01fc503808d..827d1eb662e 100644 --- a/src/domain/ledger/index.ts +++ b/src/domain/ledger/index.ts @@ -53,6 +53,12 @@ export const LedgerTransactionType = { RoutingRevenue: "routing_fee", // channel-related } as const +export const LnTxRecorded = { + TRUE: "TRUE", + FALSE: "FALSE", + MIXED: "MIXED", +} as const + export const isOnChainTransaction = (type: LedgerTransactionType): boolean => type === LedgerTransactionType.OnchainIntraLedger || type === LedgerTransactionType.OnChainTradeIntraAccount || diff --git a/src/domain/ledger/index.types.d.ts b/src/domain/ledger/index.types.d.ts index 58301a35cc6..cc2c4d67924 100644 --- a/src/domain/ledger/index.types.d.ts +++ b/src/domain/ledger/index.types.d.ts @@ -11,6 +11,7 @@ type LedgerTransactionId = string & { readonly brand: unique symbol } type LedgerJournalId = string & { readonly brand: unique symbol } type LedgerTransactionTypeObject = typeof import("./index").LedgerTransactionType type LedgerTransactionTypeKey = keyof typeof import("./index").LedgerTransactionType +type LnTxRecorded = keyof typeof import("./index").LnTxRecorded type LedgerTransactionType = LedgerTransactionTypeObject[LedgerTransactionTypeKey] type LedgerJournal = { @@ -295,7 +296,7 @@ interface ILedgerService { isToHotWalletTxRecorded(txHash: OnChainTxHash): Promise - isLnTxRecorded(paymentHash: PaymentHash): Promise + isLnTxRecorded(paymentHash: PaymentHash): Promise setOnChainTxSendHash(args: SetOnChainTxSendHashArgs): Promise diff --git a/src/services/ledger/index.ts b/src/services/ledger/index.ts index 5963c602047..cd21c3aa875 100644 --- a/src/services/ledger/index.ts +++ b/src/services/ledger/index.ts @@ -8,6 +8,7 @@ import { toCents } from "@domain/fiat" import { LedgerTransactionType, liabilitiesMainAccount, + LnTxRecorded, toLiabilitiesWalletId, toWalletId, } from "@domain/ledger" @@ -304,13 +305,29 @@ export const LedgerService = (): ILedgerService => { const isLnTxRecorded = async ( paymentHash: PaymentHash, - ): Promise => { + ): Promise => { try { - const { total } = await MainBook.ledger({ + const { total: totalNotPending } = await MainBook.ledger({ pending: false, hash: paymentHash, }) - return total > 0 + const { total: totalPending } = await MainBook.ledger({ + pending: true, + hash: paymentHash, + }) + + switch (true) { + case totalNotPending > 0 && totalPending === 0: + return LnTxRecorded.TRUE + case totalNotPending === 0 && totalPending >= 0: + return LnTxRecorded.FALSE + case totalNotPending > 0 && totalPending > 0: + return LnTxRecorded.MIXED + default: + return new UnknownLedgerError( + JSON.stringify({ totalNotPending, totalPending, paymentHash }), + ) + } } catch (err) { return new UnknownLedgerError(err) }