Skip to content

Commit

Permalink
Merge pull request #101 from AbstractSDK/feat/modules-query
Browse files Browse the repository at this point in the history
feat: modules query
  • Loading branch information
dalechyn authored Apr 11, 2024
2 parents adcfa58 + 6b763ee commit 9edc325
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/smooth-carrots-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@abstract-money/core": patch
"@abstract-money/react": patch
---

Added query to fetch modules.
29 changes: 29 additions & 0 deletions packages/core/src/actions/get-modules-from-api.ts
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions packages/core/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/clients/decorators/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -38,6 +39,11 @@ export type ApiActions = {
typeof getAnsTokensFromApi
>,
): ReturnType<typeof getAnsTokensFromApi>
getModulesFromApi(
parameters: ExtractAndOmitDecoratedParametersFromParameters<
typeof getModulesFromApi
>,
): ReturnType<typeof getModulesFromApi>
getVersionControlAddressFromApi(
parameters: ExtractAndOmitDecoratedParametersFromParameters<
typeof getVersionControlAddressFromApi
Expand Down Expand Up @@ -87,6 +93,11 @@ export function apiActions(apiUrl: string): ApiActions {
...parameters,
apiUrl,
}),
getModulesFromApi: (parameters) =>
getModulesFromApi({
...parameters,
apiUrl,
}),
getVersionControlAddressFromApi: (parameters) =>
getVersionControlAddressFromApi({
...parameters,
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
45 changes: 45 additions & 0 deletions packages/react/src/hooks/use-modules-from-api.ts
Original file line number Diff line number Diff line change
@@ -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<ApiClient['getModulesFromApi']>[0]
> & {
query?: UseQueryParameters<
ReturnType<ApiClient['getModulesFromApi']>,
unknown,
ReturnType<ApiClient['getModulesFromApi']>,
readonly [
'modulesFromApi',
WithArgs<Parameters<ApiClient['getModulesFromApi']>[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 })
}

0 comments on commit 9edc325

Please sign in to comment.