11import React , { useMemo } from "react" ;
22import styled from "styled-components" ;
33import Skeleton from "react-loading-skeleton" ;
4- import { useAccount , useReadContract } from "wagmi" ;
4+ import { useAccount , useBalance , useReadContract } from "wagmi" ;
55import { IToken } from "context/NewTransactionContext" ;
66import { isUndefined } from "utils/index" ;
77import { getFormattedBalance } from "utils/getFormattedBalance" ;
@@ -23,20 +23,27 @@ interface IBalance {
2323
2424const Balance : React . FC < IBalance > = ( { token } ) => {
2525 const { address } = useAccount ( ) ;
26+ const isNativeTransaction = token ?. address === 'native' ;
2627
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 ,
2934 abi : erc20Abi ,
3035 functionName : 'balanceOf' ,
3136 args : [ address as `0x${string } `] ,
3237 } ) ;
3338
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 ] ) ;
3543
3644 return (
3745 < Container >
38- { isUndefined ( formattedBalance ) ? < StyledAmountSkeleton /> : null }
39- { ! isUndefined ( formattedBalance ) && formattedBalance !== "0" ? formattedBalance : null }
46+ { isUndefined ( formattedBalance ) ? < StyledAmountSkeleton /> : formattedBalance !== "0" ? formattedBalance : null }
4047 </ Container >
4148 ) ;
4249} ;
0 commit comments