Skip to content

Commit

Permalink
feat: add hook to retrieve subaccount ids by module id
Browse files Browse the repository at this point in the history
  • Loading branch information
dalechyn committed Mar 29, 2024
1 parent 05e02a1 commit b109ab3
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
51 changes: 51 additions & 0 deletions packages/core/src/actions/get-sub-account-ids-by-module-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
AccountId,
accountIdApiFormatToAccountId,
accountIdToApiFormat,
accountIdToString,
} from '@abstract-money/core'
import request from 'graphql-request'
import { gql } from '../codegen/gql'

export type GetSubAccountIdsByModuleIdFromApiParameters = {
accountId: AccountId
apiUrl: string
moduleId: string
}

export async function getSubAccountIdsByModuleIdFromApi({
accountId,
moduleId,
apiUrl,
}: GetSubAccountIdsByModuleIdFromApiParameters): Promise<AccountId[]> {
const subAccountsByModules = await request(
apiUrl,
gql(/* GraphQL */ `
query SubAccountsByModules($ids: [AccountIdWithChain!]!) {
accountsByIds(ids: $ids) {
subAccounts {
accountId {
chainName
trace
sequence
}
modules {
id
}
}
}
}
`),
{
ids: accountIdToApiFormat(accountId),
},
)

const accountData = subAccountsByModules.accountsByIds[0]
if (!accountData)
throw new Error(`account id ${accountIdToString(accountId)} does not exist`)

return accountData.subAccounts
.filter(({ modules }) => modules.some((module) => module.id === moduleId))
.map(({ accountId }) => accountIdApiFormatToAccountId(accountId))
}
1 change: 1 addition & 0 deletions packages/core/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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-version-control-address-from-api'
export * from './get-sub-account-ids-by-module-id'

export * from './account/public/get-account-base-addresses-from-api'
export * from './account/public/get-base-token'
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
@@ -1,3 +1,4 @@
import { getSubAccountIdsByModuleIdFromApi } from 'src/actions'
import { getAccountBalancesFromApi } from '../../actions/get-account-balance-from-api'
import { getAccountFactoryAddressFromApi } from '../../actions/get-account-factory-address-from-api'
import { getAccountsByOwnerFromApi } from '../../actions/get-accounts-by-owner-from-api'
Expand Down Expand Up @@ -47,6 +48,11 @@ export type ApiActions = {
typeof getAccountBalancesFromApi
>,
): ReturnType<typeof getAccountBalancesFromApi>
getSubAccountIdsByModuleIdFromApi(
parameters: ExtractAndOmitDecoratedParametersFromParameters<
typeof getSubAccountIdsByModuleIdFromApi
>,
): ReturnType<typeof getSubAccountIdsByModuleIdFromApi>
}

export function apiActions(apiUrl: string): ApiActions {
Expand Down Expand Up @@ -86,5 +92,10 @@ export function apiActions(apiUrl: string): ApiActions {
...parameters,
apiUrl,
}),
getSubAccountIdsByModuleIdFromApi: (parameters) =>
getSubAccountIdsByModuleIdFromApi({
...parameters,
apiUrl,
}),
}
}
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,3 +4,4 @@ export * from './wallet'
export * from './use-accounts'
export * from './use-ans-token-from-api'
export * from './use-account-balance-from-api'
export * from './use-sub-account-ids-by-module-from-api'
45 changes: 45 additions & 0 deletions packages/react/src/hooks/use-sub-account-ids-by-module-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 UseSubAccountIdsByModuleIdFromApiParameters = WithArgs<
Parameters<ApiClient['getSubAccountIdsByModuleIdFromApi']>[0]
> & {
query?: UseQueryParameters<
Awaited<ReturnType<ApiClient['getSubAccountIdsByModuleIdFromApi']>>,
unknown,
Awaited<ReturnType<ApiClient['getSubAccountIdsByModuleIdFromApi']>>,
readonly [
'subAccountIdsByModuleIdFromApi',
WithArgs<Parameters<ApiClient['getSubAccountIdsByModuleIdFromApi']>[0]>,
ApiClient | undefined,
]
>
}

export function useSubAccountIdsByModuleIdFromApi({
args,
query = {},
}: UseSubAccountIdsByModuleIdFromApiParameters) {
const config = useConfig()
const client = config.useApiClient()
const queryKey = React.useMemo(
() => ['subAccountIdsByModuleIdFromApi', { args }, client] as const,
[args, client],
)

const enabled = Boolean(client && args?.accountId && (query.enabled ?? true))

const queryFn = React.useCallback(() => {
if (!client || !args?.accountId) throw new Error('No client or accountid')

return client.getSubAccountIdsByModuleIdFromApi({
accountId: args.accountId,
moduleId: args.moduleId,
})
}, [client, args])

return useQuery({ queryKey, queryFn, ...query, enabled })
}

0 comments on commit b109ab3

Please sign in to comment.