From 8cb89574aed5e1515891a0c7edecbfd725a4fc1b Mon Sep 17 00:00:00 2001 From: ekzyis Date: Wed, 11 Dec 2024 14:39:01 +0100 Subject: [PATCH] Fix pending forwards considered paid by client --- components/use-invoice.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/components/use-invoice.js b/components/use-invoice.js index f5af94da..dc32aacf 100644 --- a/components/use-invoice.js +++ b/components/use-invoice.js @@ -4,6 +4,8 @@ import { InvoiceCanceledError, InvoiceExpiredError, WalletReceiverError } from ' import { RETRY_PAID_ACTION } from '@/fragments/paidAction' import { INVOICE, CANCEL_INVOICE } from '@/fragments/wallet' +const PENDING_FORWARD_STATES = ['PENDING_HELD', 'FORWARDING'] + export default function useInvoice () { const client = useApolloClient() const [retryPaidAction] = useMutation(RETRY_PAID_ACTION) @@ -16,24 +18,29 @@ export default function useInvoice () { throw error } - const { cancelled, cancelledAt, actionError, expiresAt, forwardStatus } = data.invoice + const { cancelled, cancelledAt, actionState, actionError, expiresAt, forwardStatus } = data.invoice const expired = cancelledAt && new Date(expiresAt) < new Date(cancelledAt) if (expired) { throw new InvoiceExpiredError(data.invoice) } - const failed = cancelled || actionError - - if (failed && (forwardStatus && forwardStatus !== 'CONFIRMED')) { + const failedForward = forwardStatus && forwardStatus !== 'CONFIRMED' + if (failedForward) { throw new WalletReceiverError(data.invoice) } + const failed = cancelled || actionError if (failed) { throw new InvoiceCanceledError(data.invoice, actionError) } - return { invoice: data.invoice, check: that(data.invoice) } + // never let check pass if a forward is pending + // see https://github.com/stackernews/stacker.news/issues/1707 + const pendingForward = PENDING_FORWARD_STATES.includes(actionState) + const check = that(data.invoice) && !pendingForward + + return { invoice: data.invoice, check } }, [client]) const cancel = useCallback(async ({ hash, hmac }) => {