|
| 1 | +import { Commitment } from '@solana/web3.js' |
| 2 | +import { useQuery } from '@tanstack/react-query' |
| 3 | +import BN from 'bn.js' |
| 4 | +import { useDispatch, useSelector } from 'react-redux' |
| 5 | + |
| 6 | +import { useAudiusQueryContext } from '~/audius-query' |
| 7 | +import { Status } from '~/models/Status' |
| 8 | +import { BNUSDC, StringUSDC } from '~/models/Wallet' |
| 9 | +import { getUserbankAccountInfo } from '~/services/index' |
| 10 | +import { getRecoveryStatus } from '~/store/buy-usdc/selectors' |
| 11 | +import { setUSDCBalance } from '~/store/wallet/slice' |
| 12 | + |
| 13 | +import { useGetCurrentUser } from '../index' |
| 14 | + |
| 15 | +import { QUERY_KEYS } from './queryKeys' |
| 16 | +import { QueryOptions, type QueryKey } from './types' |
| 17 | + |
| 18 | +export const getUSDCBalanceQueryKey = ( |
| 19 | + ethAddress: string | null, |
| 20 | + commitment: Commitment |
| 21 | +) => |
| 22 | + [ |
| 23 | + QUERY_KEYS.usdcBalance, |
| 24 | + ethAddress, |
| 25 | + commitment |
| 26 | + ] as unknown as QueryKey<BNUSDC | null> |
| 27 | + |
| 28 | +/** |
| 29 | + * Hook to get the USDC balance for the current user. |
| 30 | + * Uses TanStack Query for data fetching and caching. |
| 31 | + * |
| 32 | + * @param options Options for the query and polling |
| 33 | + * @returns Object with status, data, refresh, and cancelPolling |
| 34 | + */ |
| 35 | +export const useUSDCBalance = ({ |
| 36 | + isPolling, |
| 37 | + pollingInterval = 1000, |
| 38 | + commitment = 'processed', |
| 39 | + ...queryOptions |
| 40 | +}: { |
| 41 | + isPolling?: boolean |
| 42 | + pollingInterval?: number |
| 43 | + commitment?: Commitment |
| 44 | +} & QueryOptions = {}) => { |
| 45 | + const { audiusSdk } = useAudiusQueryContext() |
| 46 | + const { data: user } = useGetCurrentUser({}) |
| 47 | + const ethAddress = user?.wallet ?? null |
| 48 | + const dispatch = useDispatch() |
| 49 | + const recoveryStatus = useSelector(getRecoveryStatus) |
| 50 | + |
| 51 | + const result = useQuery({ |
| 52 | + queryKey: getUSDCBalanceQueryKey(ethAddress, commitment), |
| 53 | + queryFn: async () => { |
| 54 | + const sdk = await audiusSdk() |
| 55 | + if (!ethAddress) { |
| 56 | + return null |
| 57 | + } |
| 58 | + |
| 59 | + try { |
| 60 | + const account = await getUserbankAccountInfo( |
| 61 | + sdk, |
| 62 | + { |
| 63 | + ethAddress, |
| 64 | + mint: 'USDC' |
| 65 | + }, |
| 66 | + commitment |
| 67 | + ) |
| 68 | + const balance = (account?.amount ?? new BN(0)) as BNUSDC |
| 69 | + |
| 70 | + // Still update Redux for compatibility with existing code |
| 71 | + dispatch(setUSDCBalance({ amount: balance.toString() as StringUSDC })) |
| 72 | + |
| 73 | + return balance |
| 74 | + } catch (e) { |
| 75 | + console.error('Error fetching USDC balance:', e) |
| 76 | + throw e |
| 77 | + } |
| 78 | + }, |
| 79 | + enabled: !!ethAddress, |
| 80 | + ...queryOptions |
| 81 | + }) |
| 82 | + |
| 83 | + // Map TanStack Query states to the Status enum for API compatibility |
| 84 | + let status = Status.IDLE |
| 85 | + if (result.isLoading) { |
| 86 | + status = Status.LOADING |
| 87 | + } else if (result.isError) { |
| 88 | + status = Status.ERROR |
| 89 | + } else if (result.isSuccess) { |
| 90 | + status = Status.SUCCESS |
| 91 | + } |
| 92 | + |
| 93 | + // For compatibility with existing code |
| 94 | + const data = result.data ?? null |
| 95 | + |
| 96 | + // If we're actively recovering, then we will be in loading state regardless |
| 97 | + if (recoveryStatus === Status.LOADING) { |
| 98 | + status = Status.LOADING |
| 99 | + } |
| 100 | + |
| 101 | + return { |
| 102 | + status, |
| 103 | + data, |
| 104 | + refresh: result.refetch |
| 105 | + } |
| 106 | +} |
0 commit comments