@@ -24,7 +24,9 @@ function AccountDetail({ address }: { address: string }) {
24
24
const web3Context : IWeb3Context = useContext ( Web3Context ) ;
25
25
26
26
const [ transferTo , setTransferTo ] = useState < string > ( "" ) ;
27
- const [ transferAmount , setTransferAmount ] = useState < number > ( NaN ) ;
27
+ const [ transferAmount , setTransferAmount ] = useState < bigint | undefined > (
28
+ undefined ,
29
+ ) ;
28
30
const [ isFormValid , setIsFormValid ] = useState < boolean > ( false ) ;
29
31
const [ transactions , setTransactions ] = useState < Map < string , string > > (
30
32
new Map ( ) ,
@@ -62,7 +64,7 @@ function AccountDetail({ address }: { address: string }) {
62
64
) ;
63
65
} ) ;
64
66
65
- const [ balance , setBalance ] = useState < bigint | undefined > ( undefined ) ;
67
+ const [ balance , setBalance ] = useState < number > ( NaN ) ;
66
68
const subscriptionId : MutableRefObject < string | undefined > =
67
69
useRef ( undefined ) ;
68
70
@@ -91,7 +93,9 @@ function AccountDetail({ address }: { address: string }) {
91
93
} ) ;
92
94
93
95
function updateBalance ( ) : void {
94
- web3Context . web3 . eth . getBalance ( address ) . then ( setBalance ) ;
96
+ web3Context . web3 . eth . getBalance ( address ) . then ( ( balance : bigint ) => {
97
+ setBalance ( parseFloat ( web3Context . web3 . utils . fromWei ( balance , "ether" ) ) ) ;
98
+ } ) ;
95
99
}
96
100
97
101
function isValidAddress ( address : string ) : boolean {
@@ -105,13 +109,13 @@ function AccountDetail({ address }: { address: string }) {
105
109
setTransferTo ( to ) ;
106
110
}
107
111
108
- let amount : number = transferAmount ;
112
+ let amount : bigint | undefined = transferAmount ;
109
113
if ( e . target . name === "amount" ) {
110
- amount = parseInt ( e . target . value ) ;
114
+ amount = BigInt ( e . target . value ) ;
111
115
setTransferAmount ( amount ) ;
112
116
}
113
117
114
- setIsFormValid ( isValidAddress ( to ) && ! isNaN ( amount ) ) ;
118
+ setIsFormValid ( isValidAddress ( to ) && amount !== undefined ) ;
115
119
}
116
120
117
121
function transfer ( e : FormEvent < HTMLFormElement > ) : void {
@@ -129,32 +133,33 @@ function AccountDetail({ address }: { address: string }) {
129
133
return ;
130
134
}
131
135
132
- const value : number = parseInt ( amount as string ) ;
133
- if ( isNaN ( value ) ) {
136
+ const value : bigint | undefined =
137
+ ( amount as string ) !== "" ? BigInt ( amount as string ) : undefined ;
138
+ if ( value === undefined ) {
134
139
return ;
135
140
}
136
141
137
142
setTransferTo ( "" ) ;
138
- setTransferAmount ( NaN ) ;
143
+ setTransferAmount ( undefined ) ;
139
144
setIsFormValid ( false ) ;
140
145
141
146
const transactionId : string = uuid ( ) ;
142
147
setTransactions ( ( prev : Map < string , string > ) => {
143
148
const next : Map < string , string > = new Map ( prev ) ;
144
- next . set ( transactionId , `Preparing to send ${ amount } wei to ${ to } ` ) ;
149
+ next . set ( transactionId , `Preparing to send ${ amount } ether to ${ to } ` ) ;
145
150
return next ;
146
151
} ) ;
147
152
148
153
const transferEvent : TransferEvent = web3Context . web3 . eth
149
154
. sendTransaction ( {
150
155
from : address ,
151
156
to : to as string ,
152
- value,
157
+ value : web3Context . web3 . utils . toWei ( value , "ether" ) ,
153
158
} )
154
159
. on ( "sent" , ( ) => {
155
160
setTransactions ( ( prev : Map < string , string > ) => {
156
161
const next = new Map < string , string > ( prev ) ;
157
- next . set ( transactionId , `Sending ${ amount } wei to ${ to } ` ) ;
162
+ next . set ( transactionId , `Sending ${ amount } ether to ${ to } ` ) ;
158
163
return next ;
159
164
} ) ;
160
165
} )
@@ -163,7 +168,7 @@ function AccountDetail({ address }: { address: string }) {
163
168
const next = new Map < string , string > ( prev ) ;
164
169
next . set (
165
170
transactionId ,
166
- `Sending ${ amount } wei to ${ to } [Hash: ${ data } ]` ,
171
+ `Sending ${ amount } ether to ${ to } [Hash: ${ data } ]` ,
167
172
) ;
168
173
169
174
return next ;
@@ -174,7 +179,7 @@ function AccountDetail({ address }: { address: string }) {
174
179
const next = new Map < string , string > ( prev ) ;
175
180
next . set (
176
181
transactionId ,
177
- `${ amount } wei sent to ${ to } [Hash: ${ data . transactionHash } Block #: ${ data . blockNumber } ]` ,
182
+ `${ amount } ether sent to ${ to } [Hash: ${ data . transactionHash } Block #: ${ data . blockNumber } ]` ,
178
183
) ;
179
184
180
185
return next ;
@@ -187,7 +192,7 @@ function AccountDetail({ address }: { address: string }) {
187
192
const next = new Map < string , string > ( prev ) ;
188
193
next . set (
189
194
transactionId ,
190
- `${ amount } wei sent to ${ to } [Hash: ${ receipt . transactionHash } Block #: ${ receipt . blockNumber } Confirmations: ${ numConfirmations } ]` ,
195
+ `${ amount } ether sent to ${ to } [Hash: ${ receipt . transactionHash } Block #: ${ receipt . blockNumber } Confirmations: ${ numConfirmations } ]` ,
191
196
) ;
192
197
193
198
return next ;
@@ -203,7 +208,7 @@ function AccountDetail({ address }: { address: string }) {
203
208
const next = new Map < string , string > ( prev ) ;
204
209
next . set (
205
210
transactionId ,
206
- `Error sending ${ amount } wei to ${ to } : ${ data } ` ,
211
+ `Error sending ${ amount } ether to ${ to } : ${ data } ` ,
207
212
) ;
208
213
209
214
return next ;
@@ -242,7 +247,7 @@ function AccountDetail({ address }: { address: string }) {
242
247
return (
243
248
< >
244
249
< div > { address } </ div >
245
- < div > Balance in wei : { `${ balance } ` } </ div >
250
+ < div > Balance in ether : { `${ balance } ` } </ div >
246
251
< form onSubmit = { transfer } >
247
252
< label >
248
253
Transfer to:{ " " }
@@ -257,9 +262,11 @@ function AccountDetail({ address }: { address: string }) {
257
262
< span style = { { paddingRight : "5px" } } > </ span >
258
263
259
264
< label >
260
- Transfer amount in wei :{ " " }
265
+ Transfer amount in ether :{ " " }
261
266
< input
262
- value = { ! isNaN ( transferAmount ) ? transferAmount : "" }
267
+ value = {
268
+ transferAmount === undefined ? "" : transferAmount . toString ( )
269
+ }
263
270
onChange = { transferFormChange }
264
271
name = "amount"
265
272
type = "number"
0 commit comments