import { CurrencyAmount , Token } from '@uniswap/sdk-core' ; import JSBI from 'jsbi' ; import { useMemo } from 'react' ; import ERC20_INTERFACE from '../../constants/abis/erc20' ; import { useAllTokens } from '../../hooks/Tokens' ; import { useActiveWeb3React } from '../../hooks' ; import { isAddress } from '../../utils' ; import { useMultipleContractSingleData } from '../multicall/hooks' ; /**
 * Returns a map of token addresses to their eventually consistent token balances for a single account.
 */ export function useTokenBalancesWithLoadingIndicator ( address ? , tokens ? ) { const validatedTokens = useMemo ( ( ) => tokens ?. filter ( ( t ? ) : t is Token => isAddress ( t ?. address ) !== false ) ?? [ ] , [ tokens ] ) ; const validatedTokenAddresses = useMemo ( ( ) => validatedTokens . map ( ( vt ) => vt . address ) , [ validatedTokens ] ) ; const balances = useMultipleContractSingleData ( validatedTokenAddresses , ERC20_INTERFACE , 'balanceOf' , [ address ] ) ; const anyLoading = useMemo ( ( ) => balances . some ( ( callState ) => callState . loading ) , [ balances ] ) ; return [ useMemo ( ( ) => address && validatedTokens . length > 0 ? validatedTokens . reduce < { [ tokenAddress ] : CurrencyAmount < Token > | undefined ; } > ( ( memo , token , i ) => { const value = balances ?. [ i ] ?. result ?. [ 0 ] ; const amount = value ? JSBI . BigInt ( value . toString ( ) ) : undefined ; if ( amount ) { memo [ token . address ] = CurrencyAmount . fromRawAmount ( token , amount ) ; } return memo ; } , { } ) : { } , [ address , validatedTokens , balances ] ) , anyLoading , ] ; } export function useTokenBalances ( address ? , tokens ? ) { return useTokenBalancesWithLoadingIndicator ( address , tokens ) [ 0 ] ; } export function useTokenBalance ( account ? , token ? ) { const tokenBalances = useTokenBalances ( account , [ token ] ) ; if ( ! token ) return undefined ; return tokenBalances [ token . address ] ; } export function useAllTokenBalances ( ) { const { account } = useActiveWeb3React ( ) ; const allTokens = useAllTokens ( ) ; const allTokensArray = useMemo ( ( ) => Object . values ( allTokens ?? { } ) , [ allTokens ] ) ; const balances = useTokenBalances ( account ?? undefined , allTokensArray ) ; return balances ?? { } ; }