Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions app/features/receive/cashu-receive-quote-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ type TrackMintQuotesWithWebSocketProps = {
quotesByMint: Record<string, CashuReceiveQuote[]>;
getCashuAccount: (accountId: string) => Promise<CashuAccount>;
onUpdate: (mintQuoteResponse: MintQuoteResponse) => void;
onSubscribeFailed?: (mintUrl: string, error: unknown) => void;
};

/**
Expand All @@ -395,6 +396,7 @@ const useTrackMintQuotesWithWebSocket = ({
quotesByMint,
getCashuAccount,
onUpdate,
onSubscribeFailed,
}: TrackMintQuotesWithWebSocketProps) => {
const [subscriptionManager] = useState(
() => new MintQuoteSubscriptionManager(),
Expand All @@ -404,12 +406,13 @@ const useTrackMintQuotesWithWebSocket = ({
const { mutate: subscribe } = useMutation({
mutationFn: (props: Parameters<typeof subscriptionManager.subscribe>[0]) =>
subscriptionManager.subscribe(props),
retry: 5,
retry: 2,
onError: (error, variables) => {
console.error('Error subscribing to mint quote updates', {
mintUrl: variables.mintUrl,
cause: error,
});
onSubscribeFailed?.(variables.mintUrl, error);
},
});

Expand Down Expand Up @@ -472,9 +475,14 @@ const useTrackMintQuotesWithWebSocket = ({
const usePartitionQuotesByStateCheckType = ({
quotes,
accountsCache,
mintsToPoll,
}: {
quotes: CashuReceiveQuote[];
accountsCache: ReturnType<typeof useAccountsCache>;
/**
* Mints that should be polled whether they support web sockets or not.
*/
mintsToPoll?: Set<string>;
}) => {
const getCashuAccount = useCallback(
(accountId: string) => {
Expand All @@ -499,7 +507,9 @@ const usePartitionQuotesByStateCheckType = ({
account.currency,
);

if (mintSupportsWebSockets) {
const forcePolling = mintsToPoll?.has(account.mintUrl) ?? false;

if (mintSupportsWebSockets && !forcePolling) {
const quotesForMint = quotesToSubscribeTo[account.mintUrl] ?? [];
quotesToSubscribeTo[account.mintUrl] = quotesForMint.concat(quote);
} else {
Expand All @@ -508,7 +518,7 @@ const usePartitionQuotesByStateCheckType = ({
});

return { quotesToSubscribeTo, quotesToPoll };
}, [quotes, getCashuAccount]);
}, [quotes, getCashuAccount, mintsToPoll]);
};

type OnMintQuoteStateChangeProps = {
Expand All @@ -533,6 +543,9 @@ const useOnMintQuoteStateChange = ({
const accountsCache = useAccountsCache();
const pendingQuotesCache = usePendingCashuReceiveQuotesCache();
const getCashuAccount = useGetLatestCashuAccount();
const [mintsToPoll, setMintsToPoll] = useState<Set<string>>(
() => new Set<string>(),
);

const processMintQuote = useCallback(
async (mintQuote: MintQuoteResponse) => {
Expand Down Expand Up @@ -577,12 +590,19 @@ const useOnMintQuoteStateChange = ({
usePartitionQuotesByStateCheckType({
quotes,
accountsCache,
mintsToPoll,
});

useTrackMintQuotesWithWebSocket({
quotesByMint: quotesToSubscribeTo,
getCashuAccount,
onUpdate: processMintQuote,
onSubscribeFailed: (mintUrl) =>
setMintsToPoll((prev) => {
const next = new Set(prev);
next.add(mintUrl);
return next;
}),
});

useTrackMintQuotesWithPolling({
Expand Down