@@ -6,6 +6,56 @@ import { envString } from '../../utils/envstring'
66import { useConfig } from '../useConfig'
77import { useNetwork } from '../useNetwork'
88
9+ /**
10+ * Hook that creates and manages a Sequence Indexer client for a specific chain.
11+ *
12+ * This hook maintains a cached indexer client for the specified chain ID,
13+ * initializing it with the appropriate indexer URL based on the network
14+ * and environment configuration.
15+ *
16+ * Implementation details:
17+ * - Creates a new SequenceIndexer instance for the specified chain ID if it doesn't exist
18+ * - Caches the client in a Map to avoid recreating it unnecessarily
19+ * - Uses projectAccessKey for authentication
20+ * - Constructs appropriate indexer URL based on network and environment
21+ *
22+ * @param chainId - The chain ID to create an indexer client for
23+ * @returns A SequenceIndexer instance for the specified chain
24+ *
25+ * @throws Error if an indexer client cannot be created for the specified chain ID
26+ *
27+ * @example
28+ * ```typescript
29+ * const TokenBalanceChecker = () => {
30+ * const chainId = useChainId()
31+ * const indexerClient = useIndexerClient(chainId)
32+ * const { address } = useAccount()
33+ *
34+ * const checkBalance = async () => {
35+ * // Get native token balance
36+ * const nativeBalance = await indexerClient.getNativeTokenBalance({
37+ * accountAddress: address
38+ * })
39+ *
40+ * // Get token balances
41+ * const tokenBalances = await indexerClient.getTokenBalancesSummary({
42+ * filter: {
43+ * accountAddresses: [address],
44+ * contractStatus: ContractVerificationStatus.ALL,
45+ * omitNativeBalances: true
46+ * }
47+ * })
48+ * }
49+ * }
50+ * ```
51+ *
52+ * @remarks
53+ * - The client is memoized based on projectAccessKey to prevent recreation
54+ * - The client is initialized only once per chain ID
55+ * - The environment configuration determines the indexer URL for the network
56+ * - Throws an error if the chain ID is not supported or client creation fails
57+ * - Used internally by useIndexerClients to manage multiple chain clients
58+ */
959export const useIndexerClient = ( chainId : ChainId ) => {
1060 const { env, projectAccessKey, jwt } = useConfig ( )
1161
@@ -29,6 +79,54 @@ export const useIndexerClient = (chainId: ChainId) => {
2979 return indexerClient
3080}
3181
82+ /**
83+ * Hook that creates and manages Sequence Indexer clients for multiple chains.
84+ *
85+ * This hook maintains a map of indexer clients, one for each specified chain ID.
86+ * Each client is initialized with the appropriate indexer URL based on the network
87+ * and environment configuration.
88+ *
89+ * Implementation details:
90+ * - Creates a new SequenceIndexer instance for each unique chain ID
91+ * - Caches clients in a Map to avoid recreating them unnecessarily
92+ * - Uses projectAccessKey for authentication
93+ * - Constructs appropriate indexer URLs based on network and environment
94+ *
95+ * @param chainIds - Array of chain IDs to create indexer clients for
96+ * @returns A Map where keys are chain IDs and values are the corresponding SequenceIndexer instances
97+ *
98+ * @throws Error if an indexer client cannot be created for any of the specified chain IDs
99+ *
100+ * @see {@link https://docs.sequence.xyz/sdk/web/hooks/useIndexerClients } for more detailed documentation.
101+ *
102+ * @example
103+ * ```typescript
104+ * const TransactionFetcher = () => {
105+ * // Get indexer clients for Ethereum mainnet and Polygon
106+ * const indexerClients = useIndexerClients([1, 137])
107+ *
108+ * // Use the clients to fetch data
109+ * const fetchData = async () => {
110+ * // Get Ethereum client
111+ * const ethClient = indexerClients.get(1)
112+ * // Get Polygon client
113+ * const polygonClient = indexerClients.get(137)
114+ *
115+ * // Make parallel requests
116+ * const [ethData, polygonData] = await Promise.all([
117+ * ethClient.getTransactionHistory(...),
118+ * polygonClient.getTransactionHistory(...)
119+ * ])
120+ * }
121+ * }
122+ * ```
123+ *
124+ * @remarks
125+ * - The clients are memoized based on projectAccessKey to prevent recreation
126+ * - Each client is initialized only once per chain ID
127+ * - The environment configuration determines the indexer URL for each network
128+ * - Throws an error if a chain ID is not supported or client creation fails
129+ */
32130export const useIndexerClients = ( chainIds : number [ ] ) => {
33131 const { env, projectAccessKey, jwt } = useConfig ( )
34132
0 commit comments