1- import { erc20Abi , ethAddress , formatUnits , type Address } from 'viem'
1+ import { erc20Abi , ethAddress , formatUnits , isAddressEqual , type Address } from 'viem'
22import { useConfig } from 'wagmi'
33import { useBalance , useReadContracts } from 'wagmi'
44import type { FieldsOf } from '@ui-kit/lib'
@@ -10,7 +10,6 @@ import type { GetBalanceReturnType } from '@wagmi/core'
1010import { getBalanceQueryOptions , readContractsQueryOptions } from '@wagmi/core/query'
1111
1212type TokenQuery = { tokenAddress : Address }
13- type TokenSymbolQuery = { tokenSymbol : string }
1413
1514/** Convert user collateral from GetBalanceReturnType to number */
1615const convertBalance = ( { value, decimals } : Partial < GetBalanceReturnType > ) =>
@@ -30,20 +29,12 @@ const getERC20QueryContracts = ({ chainId, userAddress, tokenAddress }: ChainQue
3029 { chainId, address : tokenAddress , abi : erc20Abi , functionName : 'decimals' } ,
3130 ] as const
3231
33- /** Function that gets the native currency symbol for a given chain */
34- const getNativeCurrencySymbol = ( config : Config , chainId : number ) =>
35- config . chains . find ( ( chain ) => chain . id === chainId ) ?. nativeCurrency ?. symbol
36-
37- /** Function that checks if a given token address and symbol are the chain's native currency symbol */
38- const isNative = ( config : Config , { chainId, tokenAddress, tokenSymbol } : ChainQuery & TokenQuery & TokenSymbolQuery ) =>
39- tokenAddress === ethAddress || tokenSymbol === getNativeCurrencySymbol ( config , chainId )
32+ /** In the Curve ecosystem all native chain gas tokens are the 0xeee...eee address */
33+ const isNative = ( { tokenAddress } : TokenQuery ) => isAddressEqual ( tokenAddress , ethAddress )
4034
4135/** Imperatively fetch token balance */
42- export const fetchTokenBalance = async (
43- config : Config ,
44- query : ChainQuery & UserQuery & TokenQuery & TokenSymbolQuery ,
45- ) =>
46- isNative ( config , query )
36+ export const fetchTokenBalance = async ( config : Config , query : ChainQuery & UserQuery & TokenQuery ) =>
37+ isNative ( query )
4738 ? await queryClient
4839 . fetchQuery ( getNativeBalanceQueryOptions ( config , query ) )
4940 . then ( ( balance ) => convertBalance ( { value : balance . value , decimals : balance . decimals } ) )
@@ -57,16 +48,11 @@ export const fetchTokenBalance = async (
5748 . then ( ( balance ) => convertBalance ( { value : balance [ 0 ] , decimals : balance [ 1 ] } ) )
5849
5950/** Hook to fetch the token balance */
60- export function useTokenBalance ( {
61- chainId,
62- userAddress,
63- tokenAddress,
64- tokenSymbol,
65- } : FieldsOf < ChainQuery & UserQuery & TokenQuery & TokenSymbolQuery > ) {
51+ export function useTokenBalance ( { chainId, userAddress, tokenAddress } : FieldsOf < ChainQuery & UserQuery & TokenQuery > ) {
6652 const config = useConfig ( )
6753
68- const isEnabled = chainId != null && userAddress != null && tokenAddress != null && tokenSymbol != null
69- const isNativeToken = isEnabled && isNative ( config , { chainId , tokenAddress, tokenSymbol } )
54+ const isEnabled = chainId != null && userAddress != null && tokenAddress != null
55+ const isNativeToken = isEnabled && isNative ( { tokenAddress } )
7056
7157 const nativeBalance = useBalance ( {
7258 ...( isEnabled ? getNativeBalanceQueryOptions ( config , { chainId, userAddress } ) : { } ) ,
0 commit comments