Skip to content

Commit

Permalink
refactor: swap WalletInvoiceChecker into trigger handler
Browse files Browse the repository at this point in the history
  • Loading branch information
vindard committed Sep 26, 2023
1 parent 6c6fd43 commit e0aba44
Showing 1 changed file with 13 additions and 31 deletions.
44 changes: 13 additions & 31 deletions src/servers/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,12 @@ import * as Wallets from "@app/wallets"

import { TxDecoder } from "@domain/bitcoin/onchain"
import { CacheKeys } from "@domain/cache"
import {
CouldNotFindWalletFromOnChainAddressError,
CouldNotFindWalletInvoiceError,
} from "@domain/errors"
import { CouldNotFindWalletFromOnChainAddressError } from "@domain/errors"
import { SwapTriggerError } from "@domain/swap/errors"
import { checkedToDisplayCurrency } from "@domain/fiat"
import {
DEFAULT_EXPIRATIONS,
invoiceExpirationForCurrency,
} from "@domain/bitcoin/lightning/invoice-expiration"
import { DEFAULT_EXPIRATIONS } from "@domain/bitcoin/lightning/invoice-expiration"
import { ErrorLevel, paymentAmountFromNumber, WalletCurrency } from "@domain/shared"
import { WalletInvoiceChecker } from "@domain/wallet-invoices"

import { BriaSubscriber } from "@services/bria"
import { RedisCacheService } from "@services/cache"
Expand Down Expand Up @@ -284,38 +279,25 @@ const listenerExistingHodlInvoices = async ({
if (invoices instanceof Error) return invoices

for (const lnInvoice of invoices) {
if (lnInvoice.isSettled || lnInvoice.isCanceled) {
continue
}

if (lnInvoice.isHeld) {
const walletInvoice = await WalletInvoicesRepository().findByPaymentHash(
lnInvoice.paymentHash,
)
const declineArgs = {
pubkey,
paymentHash: lnInvoice.paymentHash,
logger: baseLogger,
}
if (walletInvoice instanceof CouldNotFindWalletInvoiceError) {
Wallets.declineHeldInvoice(declineArgs)
if (WalletInvoiceChecker(walletInvoice).shouldDecline()) {
Wallets.declineHeldInvoice({
pubkey,
paymentHash: lnInvoice.paymentHash,
logger: baseLogger,
})
continue
}
if (walletInvoice instanceof Error) {
continue
}

const expiresIn = invoiceExpirationForCurrency(
walletInvoice.recipientWalletDescriptor.currency,
walletInvoice.createdAt,
)
if (
walletInvoice.recipientWalletDescriptor.currency === WalletCurrency.Usd &&
expiresIn.getTime() < Date.now()
) {
Wallets.declineHeldInvoice(declineArgs)
continue
}
}

if (lnInvoice.isSettled || lnInvoice.isCanceled) {
continue
}

listenerHodlInvoice({ lnd, paymentHash: lnInvoice.paymentHash })
Expand Down

0 comments on commit e0aba44

Please sign in to comment.