Skip to content

Commit ef1cc66

Browse files
committed
feat: add getV4MultiAccountTransactionsInfiniteQueryOptions
1 parent c256ae4 commit ef1cc66

2 files changed

Lines changed: 168 additions & 0 deletions

File tree

packages/core-backend/src/api/accounts/client.test.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,82 @@ describe('AccountsApiClient', () => {
397397
});
398398
expect(mockFetch).not.toHaveBeenCalled();
399399
});
400+
401+
describe('getV4MultiAccountTransactionsInfiniteQueryOptions', () => {
402+
it('returns a queryKey', () => {
403+
const result =
404+
client.accounts.getV4MultiAccountTransactionsInfiniteQueryOptions({
405+
accountAddresses: ['eip155:0:0xabc'],
406+
networks: ['eip155:1', 'eip155:137'],
407+
sortDirection: 'DESC',
408+
limit: 25,
409+
});
410+
411+
expect(result.queryKey).toStrictEqual([
412+
'accounts',
413+
'transactions',
414+
'v4MultiAccount',
415+
{
416+
accountAddresses: ['eip155:0:0xabc'],
417+
networks: ['eip155:1', 'eip155:137'],
418+
startTimestamp: undefined,
419+
endTimestamp: undefined,
420+
limit: 25,
421+
sortDirection: 'DESC',
422+
includeLogs: undefined,
423+
includeTxMetadata: undefined,
424+
maxLogsPerTx: undefined,
425+
lang: undefined,
426+
},
427+
]);
428+
});
429+
430+
it('sorts accountAddresses in the queryKey for stability', () => {
431+
const result =
432+
client.accounts.getV4MultiAccountTransactionsInfiniteQueryOptions({
433+
accountAddresses: ['eip155:0:0xzzz', 'eip155:0:0xaaa'],
434+
});
435+
436+
const keyObj = result.queryKey[3];
437+
expect(keyObj).toMatchObject({
438+
accountAddresses: ['eip155:0:0xaaa', 'eip155:0:0xzzz'],
439+
});
440+
});
441+
442+
it('sorts networks in the queryKey for stability', () => {
443+
const result =
444+
client.accounts.getV4MultiAccountTransactionsInfiniteQueryOptions({
445+
accountAddresses: ['eip155:0:0xabc'],
446+
networks: ['eip155:137', 'eip155:1'],
447+
});
448+
449+
const keyObj = result.queryKey[3];
450+
expect(keyObj).toMatchObject({
451+
networks: ['eip155:1', 'eip155:137'],
452+
});
453+
});
454+
455+
it('uses STALE_TIMES.TRANSACTIONS and GC_TIMES.DEFAULT by default', () => {
456+
const result =
457+
client.accounts.getV4MultiAccountTransactionsInfiniteQueryOptions({
458+
accountAddresses: ['eip155:0:0xabc'],
459+
});
460+
461+
expect(result.staleTime).toBe(30 * 1000);
462+
expect(result.gcTime).toBe(5 * 60 * 1000);
463+
});
464+
465+
it('allows overriding staleTime and gcTime via options', () => {
466+
const result =
467+
client.accounts.getV4MultiAccountTransactionsInfiniteQueryOptions(
468+
{ accountAddresses: ['eip155:0:0xabc'] },
469+
{ staleTime: 60_000, gcTime: 120_000 },
470+
);
471+
472+
expect(result.staleTime).toBe(60_000);
473+
expect(result.gcTime).toBe(120_000);
474+
});
475+
});
400476
});
401477

402478
describe('Relationships', () => {

packages/core-backend/src/api/accounts/client.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,98 @@ export class AccountsApiClient extends BaseApiClient {
757757
};
758758
}
759759

760+
/**
761+
* Returns TanStack Query options for v4 multi-account transactions,
762+
* designed for use with `useInfiniteQuery`.
763+
*
764+
* Unlike `getV4MultiAccountTransactionsQueryOptions`, this method:
765+
* - Excludes pagination params (cursor/after/before) from the query key for stability across pages.
766+
* - Accepts `pageParam` at query-time (via React Query) instead of baking cursor into a closure.
767+
* - Calls `this.fetch()` directly (not `queryClient.fetchQuery()`) to avoid double-caching.
768+
*
769+
* @param params - API endpoint parameters (excluding pagination cursors).
770+
* @param params.accountAddresses - Array of CAIP-10 account addresses.
771+
* @param params.networks - CAIP-2 network IDs to filter by.
772+
* @param params.startTimestamp - Start timestamp (epoch).
773+
* @param params.endTimestamp - End timestamp (epoch).
774+
* @param params.limit - Max transactions per page (default 50).
775+
* @param params.sortDirection - Sort direction (ASC/DESC).
776+
* @param params.includeLogs - Whether to include logs.
777+
* @param params.includeTxMetadata - Whether to include transaction metadata.
778+
* @param params.maxLogsPerTx - Max logs per transaction.
779+
* @param params.lang - Language for transaction category (default "en").
780+
* @param options - Fetch options including cache settings.
781+
* @returns Options object compatible with `useInfiniteQuery`.
782+
*/
783+
getV4MultiAccountTransactionsInfiniteQueryOptions(
784+
params: {
785+
accountAddresses: string[];
786+
networks?: string[];
787+
startTimestamp?: number;
788+
endTimestamp?: number;
789+
limit?: number;
790+
sortDirection?: 'ASC' | 'DESC';
791+
includeLogs?: boolean;
792+
includeTxMetadata?: boolean;
793+
maxLogsPerTx?: number;
794+
lang?: string;
795+
},
796+
options?: FetchOptions,
797+
): FetchQueryOptions<V4MultiAccountTransactionsResponse> {
798+
return {
799+
queryKey: [
800+
'accounts',
801+
'transactions',
802+
'v4MultiAccount',
803+
{
804+
accountAddresses: [...params.accountAddresses].sort(),
805+
networks: params.networks && [...params.networks].sort(),
806+
startTimestamp: params.startTimestamp,
807+
endTimestamp: params.endTimestamp,
808+
limit: params.limit,
809+
sortDirection: params.sortDirection,
810+
includeLogs: params.includeLogs,
811+
includeTxMetadata: params.includeTxMetadata,
812+
maxLogsPerTx: params.maxLogsPerTx,
813+
lang: params.lang,
814+
},
815+
] as const,
816+
queryFn: ({
817+
pageParam,
818+
signal,
819+
}: {
820+
pageParam?: string;
821+
signal?: AbortSignal;
822+
}) =>
823+
this.fetch<V4MultiAccountTransactionsResponse>(
824+
API_URLS.ACCOUNTS,
825+
'/v4/multiaccount/transactions',
826+
{
827+
signal,
828+
params: {
829+
accountAddresses: params.accountAddresses,
830+
networks: params.networks,
831+
startTimestamp: params.startTimestamp,
832+
endTimestamp: params.endTimestamp,
833+
cursor: pageParam,
834+
limit: params.limit,
835+
sortDirection: params.sortDirection,
836+
includeLogs: params.includeLogs,
837+
includeTxMetadata: params.includeTxMetadata,
838+
maxLogsPerTx: params.maxLogsPerTx,
839+
lang: params.lang,
840+
},
841+
},
842+
),
843+
getNextPageParam: ({ pageInfo }: V4MultiAccountTransactionsResponse) =>
844+
pageInfo.hasNextPage ? pageInfo.endCursor : undefined,
845+
initialPageParam: undefined as string | undefined,
846+
...getQueryOptionsOverrides(options),
847+
staleTime: options?.staleTime ?? STALE_TIMES.TRANSACTIONS,
848+
gcTime: options?.gcTime ?? GC_TIMES.DEFAULT,
849+
};
850+
}
851+
760852
/**
761853
* Get multi-account transactions (v4 endpoint).
762854
*

0 commit comments

Comments
 (0)