Skip to content

Commit

Permalink
refactor: change 'isLnTxRecorded' to return 3 result states
Browse files Browse the repository at this point in the history
  • Loading branch information
vindard committed Jan 10, 2023
1 parent 0b69e5e commit 429613e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/app/lightning/payment-status-checker.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -11,10 +12,10 @@ export const PaymentStatusChecker = async (uncheckedPaymentRequest: string) => {
return {
paymentHash,
invoiceIsPaid: async (): Promise<boolean | RepositoryError> => {
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
},
}
}
3 changes: 2 additions & 1 deletion src/app/payments/send-lightning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
LnPaymentRequestZeroAmountRequiredError,
PriceRatio,
} from "@domain/payments"
import { LnTxRecorded } from "@domain/ledger"
import { WalletCurrency } from "@domain/shared"
import {
checkedToWalletId,
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/app/payments/update-pending-payments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CouldNotFindTransactionError,
inputAmountFromLedgerTransaction,
LedgerTransactionType,
LnTxRecorded,
UnknownLedgerError,
} from "@domain/ledger"
import { MissingPropsInTransactionForPaymentFlowError } from "@domain/payments"
Expand Down Expand Up @@ -151,7 +152,7 @@ const updatePendingPayment = wrapAsyncToRunInSpan({
return recorded
}

if (recorded) {
if (recorded === LnTxRecorded.TRUE) {
paymentLogger.info("payment has already been processed")
return true
}
Expand Down
6 changes: 6 additions & 0 deletions src/domain/ledger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down
3 changes: 2 additions & 1 deletion src/domain/ledger/index.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -295,7 +296,7 @@ interface ILedgerService {

isToHotWalletTxRecorded(txHash: OnChainTxHash): Promise<boolean | LedgerServiceError>

isLnTxRecorded(paymentHash: PaymentHash): Promise<boolean | LedgerServiceError>
isLnTxRecorded(paymentHash: PaymentHash): Promise<LnTxRecorded | LedgerServiceError>

setOnChainTxSendHash(args: SetOnChainTxSendHashArgs): Promise<true | LedgerServiceError>

Expand Down
23 changes: 20 additions & 3 deletions src/services/ledger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { toCents } from "@domain/fiat"
import {
LedgerTransactionType,
liabilitiesMainAccount,
LnTxRecorded,
toLiabilitiesWalletId,
toWalletId,
} from "@domain/ledger"
Expand Down Expand Up @@ -304,13 +305,29 @@ export const LedgerService = (): ILedgerService => {

const isLnTxRecorded = async (
paymentHash: PaymentHash,
): Promise<boolean | LedgerServiceError> => {
): Promise<LnTxRecorded | LedgerServiceError> => {
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)
}
Expand Down

0 comments on commit 429613e

Please sign in to comment.