@@ -13,7 +13,7 @@ import {
13
13
import { ERC20_ABI } from "abi/erc20" ;
14
14
import { publicClient } from "hooks/useViem" ;
15
15
import { useActiveWallet } from "hooks/useWallet" ;
16
- import { type FC , useState } from "react" ;
16
+ import { type FC , useCallback , useEffect , useState } from "react" ;
17
17
import { toast } from "react-toastify" ;
18
18
import { parseEther } from "viem" ;
19
19
import { BasicButton } from "~/components/BasicButton" ;
@@ -38,16 +38,48 @@ const Transaction: FC = () => {
38
38
const { wallet } = useActiveWallet ( ) ;
39
39
40
40
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" ) ;
42
45
const [ recipient , setRecipient ] = useState ( "" ) ;
43
46
const [ amount , setAmount ] = useState ( "" ) ;
44
47
const [ isLoading , setIsLoading ] = useState ( false ) ;
45
48
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
+
46
78
/**
47
79
* handleTransactionExecution function
48
80
* トランザクションを実行する
49
81
*/
50
- const handleTransactionExecution = async ( ) => {
82
+ const handleTransactionExecution = useCallback ( async ( ) => {
51
83
if ( ! wallet ) {
52
84
alert ( "ウォレットを接続してください。" ) ;
53
85
return ;
@@ -87,8 +119,6 @@ const Transaction: FC = () => {
87
119
await publicClient . waitForTransactionReceipt ( {
88
120
hash : transferTxHash ,
89
121
} ) ;
90
-
91
- toast . success ( "トランザクションが正常に実行されました。" ) ;
92
122
} else {
93
123
if ( ! recipient || ! amount ) {
94
124
alert ( "全ての項目を入力してください。" ) ;
@@ -107,19 +137,29 @@ const Transaction: FC = () => {
107
137
} ) ;
108
138
}
109
139
toast . success ( "トランザクションが正常に実行されました。" ) ;
140
+ setRecipient ( "" ) ;
141
+ setAmount ( "0" ) ;
142
+ await fetchERC20Balance ( ) ;
110
143
} catch ( error ) {
111
144
console . error ( "Transaction execution error:" , error ) ;
112
145
toast . error ( "エラーが発生しました。" ) ;
113
146
}
114
147
setIsLoading ( false ) ;
115
- } ;
148
+ } , [
149
+ fetchERC20Balance ,
150
+ wallet ,
151
+ transactionType ,
152
+ contractAddress ,
153
+ recipient ,
154
+ amount ,
155
+ ] ) ;
116
156
117
157
return (
118
158
< Grid gridTemplateRows = "1fr auto" h = "calc(100vh - 72px)" >
119
159
< Box w = "100%" >
120
160
< PageHeader title = "トランザクション実行" />
121
161
122
- < SettingsSubSection headingText = "トランザクションタイプ" >
162
+ { /* <SettingsSubSection headingText="トランザクションタイプ">
123
163
<SelectRoot
124
164
collection={transactionTypes}
125
165
onValueChange={(e) => setTransactionType(e.value[0])}
@@ -152,9 +192,18 @@ const Transaction: FC = () => {
152
192
mt={2}
153
193
/>
154
194
</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 >
156
205
157
- < SettingsSubSection headingText = "パラメータ " >
206
+ < SettingsSubSection headingText = "" >
158
207
< Grid templateColumns = "repeat(4, 1fr)" gap = "6" alignItems = "center" >
159
208
< GridItem colSpan = { 1 } >
160
209
< Text > recipient</ Text >
@@ -184,6 +233,7 @@ const Transaction: FC = () => {
184
233
</ GridItem >
185
234
< GridItem colSpan = { 3 } >
186
235
< CommonInput
236
+ type = "number"
187
237
placeholder = "Amount"
188
238
value = { amount }
189
239
onChange = { ( e ) => setAmount ( e . target . value ) }
0 commit comments