Skip to content
Draft
Show file tree
Hide file tree
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
76 changes: 76 additions & 0 deletions packages/core-backend/src/api/accounts/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,82 @@ describe('AccountsApiClient', () => {
});
expect(mockFetch).not.toHaveBeenCalled();
});

describe('getV4MultiAccountTransactionsInfiniteQueryOptions', () => {
it('returns a queryKey', () => {
const result =
client.accounts.getV4MultiAccountTransactionsInfiniteQueryOptions({
accountAddresses: ['eip155:0:0xabc'],
networks: ['eip155:1', 'eip155:137'],
sortDirection: 'DESC',
limit: 25,
});

expect(result.queryKey).toStrictEqual([
'accounts',
'transactions',
'v4MultiAccount',
{
accountAddresses: ['eip155:0:0xabc'],
networks: ['eip155:1', 'eip155:137'],
startTimestamp: undefined,
endTimestamp: undefined,
limit: 25,
sortDirection: 'DESC',
includeLogs: undefined,
includeTxMetadata: undefined,
maxLogsPerTx: undefined,
lang: undefined,
},
]);
});

it('sorts accountAddresses in the queryKey for stability', () => {
const result =
client.accounts.getV4MultiAccountTransactionsInfiniteQueryOptions({
accountAddresses: ['eip155:0:0xzzz', 'eip155:0:0xaaa'],
});

const keyObj = result.queryKey[3];
expect(keyObj).toMatchObject({
accountAddresses: ['eip155:0:0xaaa', 'eip155:0:0xzzz'],
});
});

it('sorts networks in the queryKey for stability', () => {
const result =
client.accounts.getV4MultiAccountTransactionsInfiniteQueryOptions({
accountAddresses: ['eip155:0:0xabc'],
networks: ['eip155:137', 'eip155:1'],
});

const keyObj = result.queryKey[3];
expect(keyObj).toMatchObject({
networks: ['eip155:1', 'eip155:137'],
});
});

it('uses STALE_TIMES.TRANSACTIONS and GC_TIMES.DEFAULT by default', () => {
const result =
client.accounts.getV4MultiAccountTransactionsInfiniteQueryOptions({
accountAddresses: ['eip155:0:0xabc'],
});

expect(result.staleTime).toBe(30 * 1000);
expect(result.gcTime).toBe(5 * 60 * 1000);
});

it('allows overriding staleTime and gcTime via options', () => {
const result =
client.accounts.getV4MultiAccountTransactionsInfiniteQueryOptions(
{ accountAddresses: ['eip155:0:0xabc'] },
{ staleTime: 60_000, gcTime: 120_000 },
);

expect(result.staleTime).toBe(60_000);
expect(result.gcTime).toBe(120_000);
});
});
});

describe('Relationships', () => {
Expand Down
94 changes: 94 additions & 0 deletions packages/core-backend/src/api/accounts/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,100 @@
};
}

/**
* Returns TanStack Query options for v4 multi-account transactions,
* designed for use with `useInfiniteQuery`.
*

Check failure on line 763 in packages/core-backend/src/api/accounts/client.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (22.x)

Expected only 1 line after block description
*
* @param params - API endpoint parameters (excluding pagination cursors).
* @param params.accountAddresses - Array of CAIP-10 account addresses.
* @param params.networks - CAIP-2 network IDs to filter by.
* @param params.startTimestamp - Start timestamp (epoch).
* @param params.endTimestamp - End timestamp (epoch).
* @param params.limit - Max transactions per page (default 50).
* @param params.sortDirection - Sort direction (ASC/DESC).
* @param params.includeLogs - Whether to include logs.
* @param params.includeTxMetadata - Whether to include transaction metadata.
* @param params.maxLogsPerTx - Max logs per transaction.
* @param params.lang - Language for transaction category (default "en").
* @param options - Fetch options including cache settings.
* @returns Options object compatible with `useInfiniteQuery`.
*/
getV4MultiAccountTransactionsInfiniteQueryOptions(
params: {
accountAddresses: string[];
networks?: string[];
startTimestamp?: number;
endTimestamp?: number;
limit?: number;
sortDirection?: 'ASC' | 'DESC';
includeLogs?: boolean;
includeTxMetadata?: boolean;
maxLogsPerTx?: number;
lang?: string;
},
options?: FetchOptions,
): FetchQueryOptions<
V4MultiAccountTransactionsResponse,
Error,
V4MultiAccountTransactionsResponse,
readonly unknown[],
string | undefined
> {
return {
queryKey: [
'accounts',
'transactions',
'v4MultiAccount',
{
accountAddresses: [...params.accountAddresses].sort(),
networks: params.networks && [...params.networks].sort(),
startTimestamp: params.startTimestamp,
endTimestamp: params.endTimestamp,
limit: params.limit,
sortDirection: params.sortDirection,
includeLogs: params.includeLogs,
includeTxMetadata: params.includeTxMetadata,
maxLogsPerTx: params.maxLogsPerTx,
lang: params.lang,
},
] as const,
queryFn: ({
pageParam,
signal,
}: {
pageParam?: string;
signal?: AbortSignal;
}) =>
this.fetch<V4MultiAccountTransactionsResponse>(
API_URLS.ACCOUNTS,
'/v4/multiaccount/transactions',
{
signal,
params: {
accountAddresses: params.accountAddresses,
networks: params.networks,
startTimestamp: params.startTimestamp,
endTimestamp: params.endTimestamp,
cursor: pageParam,
limit: params.limit,
sortDirection: params.sortDirection,
includeLogs: params.includeLogs,
includeTxMetadata: params.includeTxMetadata,
maxLogsPerTx: params.maxLogsPerTx,
lang: params.lang,
},
},
),
getNextPageParam: ({ pageInfo }: V4MultiAccountTransactionsResponse) =>
pageInfo.hasNextPage ? pageInfo.endCursor : undefined,
initialPageParam: undefined as string | undefined,
...getQueryOptionsOverrides(options),
staleTime: options?.staleTime ?? STALE_TIMES.TRANSACTIONS,
gcTime: options?.gcTime ?? GC_TIMES.DEFAULT,
};
}

/**
* Get multi-account transactions (v4 endpoint).
*
Expand Down
Loading