-
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: add hook to retrieve subaccount ids by module id
- Loading branch information
Showing
5 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
packages/core/src/actions/get-sub-account-ids-by-module-id.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,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)) | ||
} |
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
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
45 changes: 45 additions & 0 deletions
45
packages/react/src/hooks/use-sub-account-ids-by-module-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,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 }) | ||
} |