1
1
import React , { useMemo } from "react" ;
2
2
import styled from "styled-components" ;
3
3
import Skeleton from "react-loading-skeleton" ;
4
- import { useAccount , useReadContract } from "wagmi" ;
4
+ import { useAccount , useBalance , useReadContract } from "wagmi" ;
5
5
import { IToken } from "context/NewTransactionContext" ;
6
6
import { isUndefined } from "utils/index" ;
7
7
import { getFormattedBalance } from "utils/getFormattedBalance" ;
@@ -23,20 +23,27 @@ interface IBalance {
23
23
24
24
const Balance : React . FC < IBalance > = ( { token } ) => {
25
25
const { address } = useAccount ( ) ;
26
+ const isNativeTransaction = token ?. address === 'native' ;
26
27
27
- const { data : balanceData } = useReadContract ( {
28
- address : token ?. address as `0x${string } `,
28
+ const { data : nativeBalance } = useBalance ( {
29
+ address : isNativeTransaction ? address as `0x${string } ` : undefined ,
30
+ } ) ;
31
+
32
+ const { data : tokenBalance } = useReadContract ( {
33
+ address : ! isNativeTransaction ? token ?. address as `0x${string } ` : undefined ,
29
34
abi : erc20Abi ,
30
35
functionName : 'balanceOf' ,
31
36
args : [ address as `0x${string } `] ,
32
37
} ) ;
33
38
34
- const formattedBalance = useMemo ( ( ) => getFormattedBalance ( balanceData , token ) , [ balanceData , token ] ) ;
39
+ const formattedBalance = useMemo ( ( ) => {
40
+ const balance = isNativeTransaction ? nativeBalance : tokenBalance ;
41
+ return getFormattedBalance ( balance , token ) ;
42
+ } , [ isNativeTransaction , nativeBalance , tokenBalance , token ] ) ;
35
43
36
44
return (
37
45
< Container >
38
- { isUndefined ( formattedBalance ) ? < StyledAmountSkeleton /> : null }
39
- { ! isUndefined ( formattedBalance ) && formattedBalance !== "0" ? formattedBalance : null }
46
+ { isUndefined ( formattedBalance ) ? < StyledAmountSkeleton /> : formattedBalance !== "0" ? formattedBalance : null }
40
47
</ Container >
41
48
) ;
42
49
} ;
0 commit comments