-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: hook to fetch account factory config
- Loading branch information
Showing
4 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@abstract-money/react": patch | ||
--- | ||
|
||
Added hook to fetch account factory config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' |
58 changes: 58 additions & 0 deletions
58
packages/react/src/hooks/public/use-account-factory-config-from-api.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<PublicClient['getAccountFactoryQueryClientFromApi']> | ||
>['config'] | ||
> | ||
> | ||
|
||
type QueryError = unknown | ||
type QueryData = QueryFnData | ||
type QueryKey = readonly [ | ||
'accountFactoryConfigFromApi', | ||
PublicClient | undefined, | ||
] | ||
type QueryResult = UseQueryResult<QueryData, QueryError> | ||
|
||
type QueryOptions = Omit< | ||
UseQueryOptions<QueryFnData, QueryError, QueryData, QueryKey>, | ||
'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 }) | ||
} |