UNPKG

@hashport/react-client

Version:
65 lines 2.59 kB
import { useHashportClient } from './useHashportClient'; import { useTokenList } from './useTokenList'; import { useQuery } from '@tanstack/react-query'; import { balanceOfAbi } from '../constants/abi'; import { useMemo } from 'react'; import { isHex } from 'viem'; import { useContractReads } from 'wagmi'; export const useTokensWithBalances = (props) => { const { evmSigner, hederaSigner, mirrorNodeClient } = useHashportClient(); const hederaId = hederaSigner.accountId; const evmAddress = evmSigner.getAddress(); const chainId = evmSigner.getChainId(); const { data: tokens, isError, isLoading } = useTokenList(props); const tokenList = useMemo(() => tokens && Array.from(tokens?.fungible.values()), [tokens]); const evmTokens = tokenList?.filter(({ id, chainId: chain }) => chain === chainId && isHex(id)); const { data: evmBalances } = useContractReads({ enabled: !!tokenList, allowFailure: true, contracts: tokenList ?.filter(({ id, chainId: chain }) => chain === chainId && isHex(id)) .map(({ id }) => ({ address: id, abi: balanceOfAbi, functionName: 'balanceOf', args: [evmAddress], })), }); const evmBalanceMap = new Map((evmTokens ?? []).map(({ id }, i) => [id, evmBalances?.[i].result])); const { data: hederaBalances } = useQuery({ staleTime: 15000, queryKey: ['mirror-node-balances', hederaId], queryFn: async () => { const { balances } = await mirrorNodeClient.getBalances({ 'account.id': hederaId, }); if (!balances[0]) throw `No balance found for ${hederaId}`; const { balance, tokens } = balances[0]; return new Map([ ['HBAR', balance], ...tokens.map(({ token_id, balance }) => [token_id, balance]), ]); }, }); const results = tokenList?.map(token => { const { id } = token; if (isHex(id)) { return { ...token, balance: evmBalanceMap?.get(id) }; } else { const balance = BigInt(hederaBalances?.get(token.id) ?? 0); return { ...token, balance }; } }); if (isLoading) { return { isLoading, isError, tokens: undefined }; } else if (isError || !results) { return { isLoading, isError: true, tokens: undefined }; } else { return { isLoading, isError, tokens: results }; } }; //# sourceMappingURL=useTokensWithBalances.js.map