-
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.
- Loading branch information
Showing
7 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
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,6 @@ | ||
--- | ||
"@abstract-money/core": patch | ||
"@abstract-money/react": patch | ||
--- | ||
|
||
useAccountBalanceFromApi hook |
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,34 @@ | ||
import request from 'graphql-request' | ||
import { tokenFromApi } from 'src/utils/tokens/token-from-api' | ||
import { tokenToAsset } from '..' | ||
import { gql } from '../codegen/gql' | ||
import { WithArgs } from '../types/with-args' | ||
import { AccountId, accountIdToApiFormat } from '../utils/account-id' | ||
|
||
export type GetAccountBalancesFromApi = WithArgs<{ | ||
apiUrl: string | ||
accountId: AccountId | ||
}> | ||
|
||
export async function getAccountBalancesFromApi({ | ||
args: { apiUrl, accountId }, | ||
}: GetAccountBalancesFromApi) { | ||
const result = await request( | ||
apiUrl, | ||
gql(/* GraphQL */ ` | ||
query Balances($ids: [AccountIdWithChain!]!) { | ||
accountsByIds(ids: $ids) { | ||
balances { | ||
amount | ||
type | ||
denom | ||
} | ||
} | ||
} | ||
`), | ||
{ ids: [accountIdToApiFormat(accountId)] }, | ||
) | ||
return result.accountsByIds[0]?.balances.map(({ amount, type, denom }) => | ||
tokenToAsset(tokenFromApi({ type, denom }), amount), | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { AssetType, BalancesQuery } from '../../codegen/gql/graphql' | ||
import { Token } from './token' | ||
|
||
export function tokenFromApi( | ||
token: Pick< | ||
BalancesQuery['accountsByIds'][number]['balances'][number], | ||
'type' | 'denom' | ||
>, | ||
): Token { | ||
if (token.type === AssetType.Cw1155) | ||
throw new Error('CW1155 tokens are not supported') | ||
|
||
if (token.type === AssetType.Cw20) { | ||
return { | ||
type: 'cw20', | ||
address: token.denom, | ||
} satisfies Token | ||
} | ||
|
||
return { | ||
type: 'native', | ||
denom: token.denom, | ||
} satisfies Token | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { ApiClient } from '@abstract-money/core' | ||
import { UseQueryOptions, useQuery } from '@tanstack/react-query' | ||
import React from 'react' | ||
import { useConfig } from '../contexts' | ||
import { WithArgs } from '../types/utils' | ||
|
||
export function useAccountBalancesFromAPI( | ||
{ | ||
args, | ||
}: WithArgs<Parameters<ApiClient['getAccountBalancesFromApi']>[0]['args']>, | ||
{ | ||
enabled: enabled_ = true, | ||
...rest | ||
}: UseQueryOptions< | ||
Awaited<ReturnType<ApiClient['getAccountBalancesFromApi']>> | undefined, | ||
unknown, | ||
Awaited<ReturnType<ApiClient['getAccountBalancesFromApi']>> | undefined, | ||
readonly [ | ||
'accountBalancesFromApi', | ||
WithArgs<Parameters<ApiClient['getAccountBalancesFromApi']>[0]['args']>, | ||
ApiClient | undefined, | ||
] | ||
> = {}, | ||
) { | ||
const config = useConfig() | ||
const client = config.useApiClient() | ||
const queryKey = React.useMemo( | ||
() => ['accountBalancesFromApi', { args }, client] as const, | ||
[args, client], | ||
) | ||
|
||
const enabled = React.useMemo( | ||
() => Boolean(client && args?.accountId && enabled_), | ||
[enabled_, client, args], | ||
) | ||
|
||
const queryFn = React.useCallback(() => { | ||
if (!client || !args?.accountId) throw new Error('No client or accountid') | ||
|
||
return client.getAccountBalancesFromApi({ | ||
args: { accountId: args.accountId }, | ||
}) | ||
}, [client, args]) | ||
|
||
return useQuery(queryKey, queryFn, { enabled, ...rest }) | ||
} |