Skip to content

Commit

Permalink
feat: hook to fetch account factory config
Browse files Browse the repository at this point in the history
  • Loading branch information
dalechyn committed Feb 21, 2024
1 parent 20b2ac6 commit bd6928a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/forty-bugs-join.md
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ describe('getInstantiate2AccountAddress', () => {
const checksum =
'3f6fb5db7e9be94c6699c495535fd55884ac72e2babbcd90b5b41a41cce179ee'
const accountId = stringToAccountId('osmosis-48', 'osmosis')
console.log('accountId', accountId)

const result = await getInstantiate2Address(
moduleFactoryAddress,
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/hooks/public/index.ts
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'
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 })
}

0 comments on commit bd6928a

Please sign in to comment.