diff --git a/.changeset/smooth-carrots-divide.md b/.changeset/smooth-carrots-divide.md new file mode 100644 index 00000000..42ec4a43 --- /dev/null +++ b/.changeset/smooth-carrots-divide.md @@ -0,0 +1,6 @@ +--- +"@abstract-money/core": patch +"@abstract-money/react": patch +--- + +Added query to fetch modules. diff --git a/packages/core/src/actions/get-modules-from-api.ts b/packages/core/src/actions/get-modules-from-api.ts new file mode 100644 index 00000000..87a9b0b1 --- /dev/null +++ b/packages/core/src/actions/get-modules-from-api.ts @@ -0,0 +1,29 @@ +import { request } from 'graphql-request' +import { gql } from '../codegen/gql' + +export type GetModulesFromApiParameters = { + apiUrl: string + chainName: string +} + +export async function getModulesFromApi({ + apiUrl, + chainName, +}: GetModulesFromApiParameters) { + const result = await request( + apiUrl, + gql(/* GraphQL */ ` + query Modules($chain: ID!, $filter: ModuleFilter) { + modules(chain: $chain, filter: $filter) { + moduleId + version + } + } + `), + { + chain: chainName, + }, + ) + + return result.modules +} diff --git a/packages/core/src/actions/index.ts b/packages/core/src/actions/index.ts index d7ad02fc..c547368a 100644 --- a/packages/core/src/actions/index.ts +++ b/packages/core/src/actions/index.ts @@ -4,6 +4,7 @@ export * from './get-accounts-by-owner-from-api' export * from './get-ans-host-address-from-api' export * from './get-ans-token-from-api' export * from './get-ans-tokens-from-api' +export * from './get-modules-from-api' export * from './get-version-control-address-from-api' export * from './get-sub-account-ids-by-module-id' diff --git a/packages/core/src/clients/decorators/api.ts b/packages/core/src/clients/decorators/api.ts index 1e08a148..1071c8be 100644 --- a/packages/core/src/clients/decorators/api.ts +++ b/packages/core/src/clients/decorators/api.ts @@ -5,6 +5,7 @@ import { getAccountsByOwnerFromApi } from '../../actions/get-accounts-by-owner-f import { getAnsHostAddressFromApi } from '../../actions/get-ans-host-address-from-api' import { getAnsTokenFromApi } from '../../actions/get-ans-token-from-api' import { getAnsTokensFromApi } from '../../actions/get-ans-tokens-from-api' +import { getModulesFromApi } from '../../actions/get-modules-from-api' import { getVersionControlAddressFromApi } from '../../actions/get-version-control-address-from-api' import { ExtractAndOmitParameters } from '../../types/parameters' @@ -38,6 +39,11 @@ export type ApiActions = { typeof getAnsTokensFromApi >, ): ReturnType + getModulesFromApi( + parameters: ExtractAndOmitDecoratedParametersFromParameters< + typeof getModulesFromApi + >, + ): ReturnType getVersionControlAddressFromApi( parameters: ExtractAndOmitDecoratedParametersFromParameters< typeof getVersionControlAddressFromApi @@ -87,6 +93,11 @@ export function apiActions(apiUrl: string): ApiActions { ...parameters, apiUrl, }), + getModulesFromApi: (parameters) => + getModulesFromApi({ + ...parameters, + apiUrl, + }), getVersionControlAddressFromApi: (parameters) => getVersionControlAddressFromApi({ ...parameters, diff --git a/packages/react/src/hooks/index.ts b/packages/react/src/hooks/index.ts index 1c2a2887..c56fa183 100644 --- a/packages/react/src/hooks/index.ts +++ b/packages/react/src/hooks/index.ts @@ -4,4 +4,5 @@ export * from './wallet' export * from './use-accounts' export * from './use-ans-token-from-api' export * from './use-account-balance-from-api' +export * from './use-modules-from-api' export * from './use-sub-account-ids-by-module-from-api' diff --git a/packages/react/src/hooks/use-modules-from-api.ts b/packages/react/src/hooks/use-modules-from-api.ts new file mode 100644 index 00000000..07752877 --- /dev/null +++ b/packages/react/src/hooks/use-modules-from-api.ts @@ -0,0 +1,45 @@ +import { ApiClient } from '@abstract-money/core/clients' +import React from 'react' +import { useConfig } from '../contexts' +import { WithArgs } from '../types/args' +import { UseQueryParameters, useQuery } from '../types/queries' + +export type UseModulesFromApiParameters = WithArgs< + Parameters[0] +> & { + query?: UseQueryParameters< + ReturnType, + unknown, + ReturnType, + readonly [ + 'modulesFromApi', + WithArgs[0]>, + ApiClient | undefined, + ] + > +} + +export function useModulesFromApi({ + args, + query = {}, +}: UseModulesFromApiParameters) { + const config = useConfig() + const client = config.useApiClient() + const queryKey = React.useMemo( + () => ['modulesFromApi', { args }, client] as const, + [args, client], + ) + + const enabled = Boolean(client && args?.chainName && (query.enabled ?? true)) + + const queryFn = React.useCallback(() => { + if (!client || !args?.chainName) + throw new Error('No client or owner or chain') + + return client.getModulesFromApi({ + chainName: args.chainName, + }) + }, [client, args]) + + return useQuery({ queryKey, queryFn, ...query, enabled }) +}