Skip to content

Commit

Permalink
Retry invoices on client
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Jan 8, 2025
1 parent 6c4f61d commit ebd1349
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
6 changes: 3 additions & 3 deletions components/use-invoice.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useApolloClient, useMutation } from '@apollo/client'
import { useCallback } from 'react'
import { useCallback, useMemo } from 'react'
import { InvoiceCanceledError, InvoiceExpiredError, WalletReceiverError } from '@/wallets/errors'
import { RETRY_PAID_ACTION } from '@/fragments/paidAction'
import { INVOICE, CANCEL_INVOICE } from '@/fragments/wallet'
Expand Down Expand Up @@ -42,7 +42,7 @@ export default function useInvoice () {
return data.cancelInvoice
}, [cancelInvoice])

const retry = useCallback(async ({ id, hash, hmac }, { update }) => {
const retry = useCallback(async ({ id, hash, hmac }, { update } = {}) => {
console.log('retrying invoice:', hash)
const { data, error } = await retryPaidAction({ variables: { invoiceId: Number(id) }, update })
if (error) throw error
Expand All @@ -53,5 +53,5 @@ export default function useInvoice () {
return newInvoice
}, [retryPaidAction])

return { cancel, retry, isInvoice }
return useMemo(() => ({ cancel, retry, isInvoice }), [cancel, retry, isInvoice])
}
23 changes: 16 additions & 7 deletions wallets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { getStorageKey, getWalletByType, walletPrioritySort, canSend, isConfigur
import useVault from '@/components/vault/use-vault'
import walletDefs from '@/wallets/client'
import { generateMutation } from './graphql'
import { useWalletPayment } from './payment'
import useInvoice from '@/components/use-invoice'

const WalletsContext = createContext({
wallets: []
Expand Down Expand Up @@ -223,17 +225,25 @@ export function useWallet (name) {
export function useSendWallets () {
const { wallets } = useWallets()
// return all enabled wallets that are available and can send
return wallets
return useMemo(() => wallets
.filter(w => !w.def.isAvailable || w.def.isAvailable())
.filter(w => w.config?.enabled && canSend(w))
.filter(w => w.config?.enabled && canSend(w)), [wallets])
}

function RetryHandler ({ children }) {
const failedInvoices = useFailedInvoices()
const waitForWalletPayment = useWalletPayment()
const invoiceHelper = useInvoice()

useEffect(() => {
// TODO: retry
}, [failedInvoices])
(async () => {
for (const invoice of failedInvoices) {
// TODO: don't retry forever
const newInvoice = await invoiceHelper.retry(invoice)
waitForWalletPayment(newInvoice).catch(console.error)
}
})()
}, [failedInvoices, invoiceHelper, waitForWalletPayment])

return children
}
Expand All @@ -244,9 +254,8 @@ function useFailedInvoices () {
// TODO: use longer poll interval in prod?
const { data } = useQuery(FAILED_INVOICES, {
pollInterval: FAST_POLL_INTERVAL,
fetchPolicy: 'no-cache',
nextFetchPolicy: 'no-cache',
skip: wallets.length === 0
skip: wallets.length === 0,
notifyOnNetworkStatusChange: true
})

return data?.failedInvoices ?? []
Expand Down
2 changes: 1 addition & 1 deletion wallets/payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function useWalletPayment () {
const loggerFactory = useWalletLoggerFactory()
const invoiceHelper = useInvoice()

return useCallback(async (invoice, { waitFor, updateOnFallback }) => {
return useCallback(async (invoice, { waitFor, updateOnFallback } = {}) => {
let aggregateError = new WalletAggregateError([])
let latestInvoice = invoice

Expand Down

0 comments on commit ebd1349

Please sign in to comment.