diff --git a/.changeset/forty-bugs-join.md b/.changeset/forty-bugs-join.md new file mode 100644 index 00000000..4b7ee98f --- /dev/null +++ b/.changeset/forty-bugs-join.md @@ -0,0 +1,5 @@ +--- +"@abstract-money/react": patch +--- + +Added hook to fetch account factory config diff --git a/packages/core/src/utils/account-factory/get-instantiate2-address.test.ts b/packages/core/src/utils/account-factory/get-instantiate2-address.test.ts index 9e6e86bb..f94f2795 100644 --- a/packages/core/src/utils/account-factory/get-instantiate2-address.test.ts +++ b/packages/core/src/utils/account-factory/get-instantiate2-address.test.ts @@ -10,7 +10,6 @@ describe('getInstantiate2AccountAddress', () => { const checksum = '3f6fb5db7e9be94c6699c495535fd55884ac72e2babbcd90b5b41a41cce179ee' const accountId = stringToAccountId('osmosis-48', 'osmosis') - console.log('accountId', accountId) const result = await getInstantiate2Address( moduleFactoryAddress, diff --git a/packages/react/src/hooks/public/index.ts b/packages/react/src/hooks/public/index.ts index 166813d7..f080195b 100644 --- a/packages/react/src/hooks/public/index.ts +++ b/packages/react/src/hooks/public/index.ts @@ -1,2 +1,3 @@ export * from './use-cosm-wasm-client' export * from './use-account-factory-query-client-from-api' +export * from './use-account-factory-config-from-api' diff --git a/packages/react/src/hooks/public/use-account-factory-config-from-api.ts b/packages/react/src/hooks/public/use-account-factory-config-from-api.ts new file mode 100644 index 00000000..ed4448b6 --- /dev/null +++ b/packages/react/src/hooks/public/use-account-factory-config-from-api.ts @@ -0,0 +1,58 @@ +import { PublicClient } from '@abstract-money/core' +import { + UseQueryOptions, + UseQueryResult, + useQuery, +} from '@tanstack/react-query' +import React from 'react' +import { useConfig } from '../../contexts' + +type QueryFnData = Awaited< + ReturnType< + Awaited< + ReturnType + >['config'] + > +> + +type QueryError = unknown +type QueryData = QueryFnData +type QueryKey = readonly [ + 'accountFactoryConfigFromApi', + PublicClient | undefined, +] +type QueryResult = UseQueryResult + +type QueryOptions = Omit< + UseQueryOptions, + 'queryFn' +> +export function useAccountFactoryConfigFromApi( + chainName?: string, + options: QueryOptions = { enabled: true }, +): QueryResult { + const config = useConfig() + const publicClient = config.usePublicClient({ + chainName, + }) + const queryKey = React.useMemo( + () => ['accountFactoryConfigFromApi', publicClient] as const, + [publicClient], + ) + + const enabled = React.useMemo( + () => Boolean(publicClient && options?.enabled), + [options?.enabled, publicClient], + ) + + const queryFn = React.useCallback(async () => { + if (!publicClient) throw new Error('No client') + + const accountFactoryQueryClient = + await publicClient.getAccountFactoryQueryClientFromApi({ args: {} }) + const config = await accountFactoryQueryClient.config() + return config + }, [publicClient]) + + return useQuery(queryKey, queryFn, { ...options, enabled }) +}