-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuseQueryHooks.ts
46 lines (40 loc) · 1.46 KB
/
useQueryHooks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { useChain } from '@interchain-kit/react';
import { createRpcQueryHooks } from 'osmo-query';
import { useRpcClient } from 'interchain-react/react-query';
import { useRpcEndpoint } from 'interchain-react/react-query'
import { defaultContext } from '@tanstack/react-query';
export const useQueryHooks = (chainName: string, extraKey?: string) => {
const { address, getRpcEndpoint } = useChain(chainName);
const rpcEndpointQuery = useRpcEndpoint({
getter: getRpcEndpoint,
options: {
context: defaultContext,
enabled: !!address,
staleTime: Infinity,
queryKeyHashFn: (queryKey) => {
const key = [...queryKey, chainName];
return JSON.stringify(extraKey ? [...key, extraKey] : key);
},
},
});
const rpcClientQuery = useRpcClient({
// rpcEndpoint: rpcEndpointQuery.data || '',
clientResolver: {
rpcEndpoint: rpcEndpointQuery.data,
},
options: {
context: defaultContext,
enabled: !!address && !!rpcEndpointQuery.data,
staleTime: Infinity,
queryKeyHashFn: (queryKey) => {
return JSON.stringify(extraKey ? [...queryKey, extraKey] : queryKey);
},
},
});
const { cosmos: cosmosQuery, osmosis: osmosisQuery } = createRpcQueryHooks({
rpc: rpcClientQuery.data,
});
const isReady = !!address && !!rpcClientQuery.data;
const isFetching = rpcEndpointQuery.isFetching || rpcClientQuery.isFetching;
return { cosmosQuery, osmosisQuery, isReady, isFetching };
};