@@ -13,7 +13,7 @@ import {
1313import { ERC20_ABI } from "abi/erc20" ;
1414import { publicClient } from "hooks/useViem" ;
1515import { useActiveWallet } from "hooks/useWallet" ;
16- import { type FC , useState } from "react" ;
16+ import { type FC , useCallback , useEffect , useState } from "react" ;
1717import { toast } from "react-toastify" ;
1818import { parseEther } from "viem" ;
1919import { BasicButton } from "~/components/BasicButton" ;
@@ -38,16 +38,48 @@ const Transaction: FC = () => {
3838 const { wallet } = useActiveWallet ( ) ;
3939
4040 const [ transactionType , setTransactionType ] = useState ( "ERC20" ) ;
41- const [ contractAddress , setContractAddress ] = useState ( "" ) ;
41+ const [ contractAddress , setContractAddress ] = useState (
42+ "0x1856a7e86543F9f38c3bC68921dA0B65DbE018b6" ,
43+ ) ;
44+ const [ balance , setBalance ] = useState < string > ( "0" ) ;
4245 const [ recipient , setRecipient ] = useState ( "" ) ;
4346 const [ amount , setAmount ] = useState ( "" ) ;
4447 const [ isLoading , setIsLoading ] = useState ( false ) ;
4548
49+ const fetchERC20Balance = useCallback ( async ( ) => {
50+ if ( ! wallet || ! contractAddress ) return ;
51+ // Fetch balance and decimals in parallel
52+ const [ erc20Balance , decimals ] = await Promise . all ( [
53+ publicClient . readContract ( {
54+ address : contractAddress as `0x${string } `,
55+ abi : ERC20_ABI ,
56+ functionName : "balanceOf" ,
57+ args : [ wallet . account ?. address as `0x${string } `] ,
58+ } ) ,
59+ publicClient . readContract ( {
60+ address : contractAddress as `0x${string } `,
61+ abi : ERC20_ABI ,
62+ functionName : "decimals" ,
63+ } ) ,
64+ ] ) ;
65+
66+ const decimalsNumber = Number ( decimals ) ;
67+ const formattedBalance =
68+ decimalsNumber > 0
69+ ? Number ( erc20Balance ) / 10 ** decimalsNumber
70+ : Number ( erc20Balance ) ;
71+ setBalance ( Math . floor ( formattedBalance ) . toLocaleString ( ) ) ;
72+ } , [ wallet , contractAddress ] ) ;
73+
74+ useEffect ( ( ) => {
75+ fetchERC20Balance ( ) ;
76+ } , [ fetchERC20Balance ] ) ;
77+
4678 /**
4779 * handleTransactionExecution function
4880 * トランザクションを実行する
4981 */
50- const handleTransactionExecution = async ( ) => {
82+ const handleTransactionExecution = useCallback ( async ( ) => {
5183 if ( ! wallet ) {
5284 alert ( "ウォレットを接続してください。" ) ;
5385 return ;
@@ -87,8 +119,6 @@ const Transaction: FC = () => {
87119 await publicClient . waitForTransactionReceipt ( {
88120 hash : transferTxHash ,
89121 } ) ;
90-
91- toast . success ( "トランザクションが正常に実行されました。" ) ;
92122 } else {
93123 if ( ! recipient || ! amount ) {
94124 alert ( "全ての項目を入力してください。" ) ;
@@ -107,19 +137,29 @@ const Transaction: FC = () => {
107137 } ) ;
108138 }
109139 toast . success ( "トランザクションが正常に実行されました。" ) ;
140+ setRecipient ( "" ) ;
141+ setAmount ( "0" ) ;
142+ await fetchERC20Balance ( ) ;
110143 } catch ( error ) {
111144 console . error ( "Transaction execution error:" , error ) ;
112145 toast . error ( "エラーが発生しました。" ) ;
113146 }
114147 setIsLoading ( false ) ;
115- } ;
148+ } , [
149+ fetchERC20Balance ,
150+ wallet ,
151+ transactionType ,
152+ contractAddress ,
153+ recipient ,
154+ amount ,
155+ ] ) ;
116156
117157 return (
118158 < Grid gridTemplateRows = "1fr auto" h = "calc(100vh - 72px)" >
119159 < Box w = "100%" >
120160 < PageHeader title = "トランザクション実行" />
121161
122- < SettingsSubSection headingText = "トランザクションタイプ" >
162+ { /* <SettingsSubSection headingText="トランザクションタイプ">
123163 <SelectRoot
124164 collection={transactionTypes}
125165 onValueChange={(e) => setTransactionType(e.value[0])}
@@ -152,9 +192,18 @@ const Transaction: FC = () => {
152192 mt={2}
153193 />
154194 </SettingsSubSection>
155- ) }
195+ )} */ }
196+
197+ < SettingsSubSection headingText = "残高" >
198+ < Text fontSize = "2xl" >
199+ { balance } { " " }
200+ < Text as = "span" fontSize = "md" >
201+ kuu
202+ </ Text >
203+ </ Text >
204+ </ SettingsSubSection >
156205
157- < SettingsSubSection headingText = "パラメータ " >
206+ < SettingsSubSection headingText = "" >
158207 < Grid templateColumns = "repeat(4, 1fr)" gap = "6" alignItems = "center" >
159208 < GridItem colSpan = { 1 } >
160209 < Text > recipient</ Text >
@@ -184,6 +233,7 @@ const Transaction: FC = () => {
184233 </ GridItem >
185234 < GridItem colSpan = { 3 } >
186235 < CommonInput
236+ type = "number"
187237 placeholder = "Amount"
188238 value = { amount }
189239 onChange = { ( e ) => setAmount ( e . target . value ) }
0 commit comments