@@ -20,6 +20,12 @@ export function useTokenBalance({
2020 enabled = true ,
2121} : UseTokenBalanceProps ) {
2222 const chainId = useChainId ( ) ;
23+
24+ // Add validation for tokenAddress
25+ if ( ! tokenAddress ) {
26+ throw new Error ( 'tokenAddress is required' ) ;
27+ }
28+
2329 const [ balance , setBalance ] = useState ( '0' ) ;
2430 const [ rawBalance , setRawBalance ] = useState < bigint > ( BigInt ( 0 ) ) ;
2531 const [ value , setValue ] = useState < string | number > ( 0 ) ;
@@ -28,7 +34,6 @@ export function useTokenBalance({
2834 const [ tokenSymbol , setTokenSymbol ] = useState < string > ( '' ) ;
2935 const [ tokenDecimals , setTokenDecimals ] = useState < number > ( 18 ) ;
3036
31- const isNativeToken = tokenAddress === 'native' ;
3237 const { signer } = useSigner ( ) ;
3338
3439 const {
@@ -43,104 +48,89 @@ export function useTokenBalance({
4348
4449 // Remove tokenData and add separate queries for symbol and decimals
4550 const tokenSymbolData = useReadContract ( {
46- address : isNativeToken ? undefined : ( tokenAddress as `0x${string } `) ,
51+ address : tokenAddress as `0x${string } `,
4752 abi : confidentialErc20Abi ,
4853 functionName : 'symbol' ,
4954 query : {
50- enabled : enabled && ! isNativeToken && ! ! tokenAddress ,
55+ enabled : enabled && ! ! tokenAddress ,
5156 } ,
5257 } ) ;
5358
5459 const tokenDecimalsData = useReadContract ( {
55- address : isNativeToken ? undefined : ( tokenAddress as `0x${string } `) ,
60+ address : tokenAddress as `0x${string } `,
5661 abi : confidentialErc20Abi ,
5762 functionName : 'decimals' ,
5863 query : {
59- enabled : enabled && ! isNativeToken && ! ! tokenAddress ,
60- } ,
61- } ) ;
62-
63- // Native token balance query
64- const nativeBalanceData = useBalance ( {
65- address : address as `0x${string } `,
66- query : {
67- enabled : enabled && ! ! address && isNativeToken ,
64+ enabled : enabled && ! ! tokenAddress ,
6865 } ,
6966 } ) ;
7067
7168 // ERC20 token balance query using balanceOf
7269 const tokenBalanceData = useReadContract ( {
73- address : isNativeToken ? undefined : ( tokenAddress as `0x${string } `) ,
70+ address : tokenAddress as `0x${string } `,
7471 abi : confidentialErc20Abi ,
7572 functionName : 'balanceOf' ,
7673 args : address ? [ address as `0x${string } `] : undefined ,
7774 query : {
78- enabled : enabled && ! ! address && ! isNativeToken && ! ! tokenAddress ,
75+ enabled : enabled && ! ! address && ! ! tokenAddress ,
7976 } ,
8077 } ) ;
8178
8279 useEffect ( ( ) => {
83- // For native token
84- if ( isNativeToken ) {
85- setIsLoading ( nativeBalanceData . isLoading ) ;
86-
87- if ( nativeBalanceData . error ) {
88- setError ( nativeBalanceData . error ) ;
89- } else {
90- setError ( null ) ;
91- }
92-
93- if ( nativeBalanceData . data && ! nativeBalanceData . isLoading ) {
94- const formattedBalance = formatUnits (
95- nativeBalanceData . data . value ,
96- nativeBalanceData . data . decimals ,
97- ) ;
98- setBalance ( formattedBalance ) ;
99- setRawBalance ( nativeBalanceData . data . value ) ;
100-
101- // Get the appropriate token price based on the current network
102- const nativePrice = 1940 ; // Default ETH price
80+ // For ERC20 tokens
81+ setIsLoading (
82+ tokenBalanceData . isLoading ||
83+ tokenSymbolData . isLoading ||
84+ tokenDecimalsData . isLoading ,
85+ ) ;
86+
87+ if ( tokenBalanceData . error ) {
88+ setError ( tokenBalanceData . error ) ;
89+ } else if ( tokenSymbolData . error ) {
90+ setError ( tokenSymbolData . error ) ;
91+ } else if ( tokenDecimalsData . error ) {
92+ setError ( tokenDecimalsData . error ) ;
93+ } else {
94+ setError ( null ) ;
95+ }
10396
104- setValue ( parseFloat ( formattedBalance ) * nativePrice ) ;
105- }
97+ // Update symbol and decimals when available
98+ if ( tokenSymbolData . data ) {
99+ setTokenSymbol ( tokenSymbolData . data as string ) ;
106100 }
107- // For ERC20 tokens
108- else {
109- setIsLoading (
110- tokenBalanceData . isLoading ||
111- tokenSymbolData . isLoading ||
112- tokenDecimalsData . isLoading ,
113- ) ;
114-
115- if ( tokenBalanceData . error ) {
116- setError ( tokenBalanceData . error ) ;
117- } else if ( tokenSymbolData . error ) {
118- setError ( tokenSymbolData . error ) ;
119- } else if ( tokenDecimalsData . error ) {
120- setError ( tokenDecimalsData . error ) ;
121- } else {
122- setError ( null ) ;
123- }
124101
125- // Update symbol and decimals when available
126- if ( tokenSymbolData . data ) {
127- setTokenSymbol ( tokenSymbolData . data as string ) ;
128- }
102+ if ( tokenDecimalsData . data ) {
103+ setTokenDecimals ( Number ( tokenDecimalsData . data ) ) ;
104+ }
129105
130- if ( tokenDecimalsData . data ) {
131- setTokenDecimals ( Number ( tokenDecimalsData . data ) ) ;
132- }
106+ if ( tokenBalanceData . data && ! tokenBalanceData . isLoading && tokenDecimals ) {
107+ const rawBalance = tokenBalanceData . data as bigint ;
108+ setRawBalance ( rawBalance ) ;
109+ const formattedBalance = formatUnits ( rawBalance , tokenDecimals ) ;
110+ setBalance ( formattedBalance ) ;
111+
112+ // Mock price calculation based on token symbol
113+ const mockPrice =
114+ tokenSymbol === 'LINK'
115+ ? 11.5
116+ : tokenSymbol === 'MATIC'
117+ ? 1.1
118+ : tokenSymbol === 'WETH'
119+ ? 1940
120+ : tokenSymbol === 'UNI'
121+ ? 9.8
122+ : 5 ;
123+
124+ setValue ( parseFloat ( formattedBalance ) * mockPrice ) ;
125+ }
133126
134- if (
135- tokenBalanceData . data &&
136- ! tokenBalanceData . isLoading &&
137- tokenDecimals
138- ) {
139- const rawBalance = tokenBalanceData . data as bigint ;
127+ // For Confidential tokens
128+ if ( isConfidential ) {
129+ if ( decryptedBalance || decryptedBalance === 0n ) {
130+ const rawBalance = decryptedBalance ;
140131 setRawBalance ( rawBalance ) ;
141- const formattedBalance = formatUnits ( rawBalance , tokenDecimals ) ;
132+ const formattedBalance = formatUnits ( decryptedBalance , tokenDecimals ) ;
142133 setBalance ( formattedBalance ) ;
143-
144134 // Mock price calculation based on token symbol
145135 const mockPrice =
146136 tokenSymbol === 'LINK'
@@ -149,49 +139,22 @@ export function useTokenBalance({
149139 ? 1.1
150140 : tokenSymbol === 'WETH'
151141 ? 1940
152- : tokenSymbol === 'UNI'
153- ? 9.8
154- : 5 ;
142+ : tokenSymbol === 'WETHc'
143+ ? 1940
144+ : tokenSymbol === 'UNI'
145+ ? 9.8
146+ : 5 ;
155147
156148 setValue ( parseFloat ( formattedBalance ) * mockPrice ) ;
157- }
158-
159- // For Confidential tokens
160- if ( isConfidential ) {
161- if ( decryptedBalance || decryptedBalance === 0n ) {
162- const rawBalance = decryptedBalance ;
163- setRawBalance ( rawBalance ) ;
164- const formattedBalance = formatUnits ( decryptedBalance , tokenDecimals ) ;
165- setBalance ( formattedBalance ) ;
166- // Mock price calculation based on token symbol
167- const mockPrice =
168- tokenSymbol === 'LINK'
169- ? 11.5
170- : tokenSymbol === 'MATIC'
171- ? 1.1
172- : tokenSymbol === 'WETH'
173- ? 1940
174- : tokenSymbol === 'WETHc'
175- ? 1940
176- : tokenSymbol === 'UNI'
177- ? 9.8
178- : 5 ;
179-
180- setValue ( parseFloat ( formattedBalance ) * mockPrice ) ;
181- } else {
182- setBalance ( '•••••••' ) ;
183- setValue ( '•••••••' ) ;
184- }
149+ } else {
150+ setBalance ( '•••••••' ) ;
151+ setValue ( '•••••••' ) ;
185152 }
186153 }
187154 } , [
188- isNativeToken ,
189155 isConfidential ,
190156 decryptedBalance ,
191157 chainId ,
192- nativeBalanceData . data ,
193- nativeBalanceData . isLoading ,
194- nativeBalanceData . error ,
195158 tokenBalanceData . data ,
196159 tokenBalanceData . isLoading ,
197160 tokenBalanceData . error ,
@@ -205,17 +168,6 @@ export function useTokenBalance({
205168 tokenSymbol ,
206169 ] ) ;
207170
208- // Get the appropriate native token symbol based on the chain
209- const getNativeSymbol = ( ) => {
210- return 'ETH' ; // Default for Ethereum networks (mainnet, sepolia, etc.)
211- } ;
212-
213- // Get the appropriate native token name based on the chain
214- const getNativeName = ( ) => {
215- if ( chainId === sepolia . id ) return 'Sepolia ETH' ;
216- return 'Sepolia ETH' ; // Default
217- } ;
218-
219171 const decrypt = async ( ) => {
220172 if ( isConfidential ) {
221173 await decryptBalance ( rawBalance , tokenAddress as `0x${string } `) ;
@@ -232,8 +184,8 @@ export function useTokenBalance({
232184 isLoading,
233185 isDecrypting,
234186 error,
235- symbol : isNativeToken ? getNativeSymbol ( ) : tokenSymbol ,
236- name : isNativeToken ? getNativeName ( ) : tokenSymbol ,
237- decimals : isNativeToken ? 18 : tokenDecimals ,
187+ symbol : tokenSymbol ,
188+ name : tokenSymbol ,
189+ decimals : tokenDecimals ,
238190 } ;
239191}
0 commit comments