export const reactQuery = `import { getRpcClient } from './extern' import { useQuery, UseQueryOptions, } from '@tanstack/react-query'; import { HttpEndpoint, ProtobufRpcClient } from '@cosmjs/stargate'; import { Tendermint34Client } from '@cosmjs/tendermint-rpc'; export interface ReactQueryParams { options?: UseQueryOptions; } export interface UseRpcClientQuery extends ReactQueryParams { rpcEndpoint: string | HttpEndpoint; } export interface UseRpcEndpointQuery extends ReactQueryParams { getter: () => Promise; } export const useRpcEndpoint = ({ getter, options, }: UseRpcEndpointQuery) => { return useQuery(['rpcEndpoint', getter], async () => { return await getter(); }, options); }; export const useRpcClient = ({ rpcEndpoint, options, }: UseRpcClientQuery) => { return useQuery(['rpcClient', rpcEndpoint], async () => { return await getRpcClient(rpcEndpoint); }, options); }; interface UseTendermintClient extends ReactQueryParams { rpcEndpoint: string | HttpEndpoint; } /** * Hook that uses react-query to cache a connected tendermint client. */ export const useTendermintClient = ({ rpcEndpoint, options, }: UseTendermintClient) => { const { data: client } = useQuery( ['client', 'tendermint', rpcEndpoint], () => Tendermint34Client.connect(rpcEndpoint), { // allow overriding onError: (e) => { throw new Error(\`Failed to connect to \${rpcEndpoint}\` + '\\n' + e) }, ...options, } ) return { client } }; `;