Skip to content

Commit f3f5e17

Browse files
author
Dan Forbes
committed
Rework Use of Numbers and BigInts
1 parent d0e1b9b commit f3f5e17

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

templates/demo/web3js-react-dapp-demo/src/AccountDetail.tsx

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ function AccountDetail({ address }: { address: string }) {
2424
const web3Context: IWeb3Context = useContext(Web3Context);
2525

2626
const [transferTo, setTransferTo] = useState<string>("");
27-
const [transferAmount, setTransferAmount] = useState<number>(NaN);
27+
const [transferAmount, setTransferAmount] = useState<bigint | undefined>(
28+
undefined,
29+
);
2830
const [isFormValid, setIsFormValid] = useState<boolean>(false);
2931
const [transactions, setTransactions] = useState<Map<string, string>>(
3032
new Map(),
@@ -62,7 +64,7 @@ function AccountDetail({ address }: { address: string }) {
6264
);
6365
});
6466

65-
const [balance, setBalance] = useState<bigint | undefined>(undefined);
67+
const [balance, setBalance] = useState<number>(NaN);
6668
const subscriptionId: MutableRefObject<string | undefined> =
6769
useRef(undefined);
6870

@@ -91,7 +93,9 @@ function AccountDetail({ address }: { address: string }) {
9193
});
9294

9395
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+
});
9599
}
96100

97101
function isValidAddress(address: string): boolean {
@@ -105,13 +109,13 @@ function AccountDetail({ address }: { address: string }) {
105109
setTransferTo(to);
106110
}
107111

108-
let amount: number = transferAmount;
112+
let amount: bigint | undefined = transferAmount;
109113
if (e.target.name === "amount") {
110-
amount = parseInt(e.target.value);
114+
amount = BigInt(e.target.value);
111115
setTransferAmount(amount);
112116
}
113117

114-
setIsFormValid(isValidAddress(to) && !isNaN(amount));
118+
setIsFormValid(isValidAddress(to) && amount !== undefined);
115119
}
116120

117121
function transfer(e: FormEvent<HTMLFormElement>): void {
@@ -129,32 +133,33 @@ function AccountDetail({ address }: { address: string }) {
129133
return;
130134
}
131135

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) {
134139
return;
135140
}
136141

137142
setTransferTo("");
138-
setTransferAmount(NaN);
143+
setTransferAmount(undefined);
139144
setIsFormValid(false);
140145

141146
const transactionId: string = uuid();
142147
setTransactions((prev: Map<string, string>) => {
143148
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}`);
145150
return next;
146151
});
147152

148153
const transferEvent: TransferEvent = web3Context.web3.eth
149154
.sendTransaction({
150155
from: address,
151156
to: to as string,
152-
value,
157+
value: web3Context.web3.utils.toWei(value, "ether"),
153158
})
154159
.on("sent", () => {
155160
setTransactions((prev: Map<string, string>) => {
156161
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}`);
158163
return next;
159164
});
160165
})
@@ -163,7 +168,7 @@ function AccountDetail({ address }: { address: string }) {
163168
const next = new Map<string, string>(prev);
164169
next.set(
165170
transactionId,
166-
`Sending ${amount} wei to ${to} [Hash: ${data}]`,
171+
`Sending ${amount} ether to ${to} [Hash: ${data}]`,
167172
);
168173

169174
return next;
@@ -174,7 +179,7 @@ function AccountDetail({ address }: { address: string }) {
174179
const next = new Map<string, string>(prev);
175180
next.set(
176181
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}]`,
178183
);
179184

180185
return next;
@@ -187,7 +192,7 @@ function AccountDetail({ address }: { address: string }) {
187192
const next = new Map<string, string>(prev);
188193
next.set(
189194
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}]`,
191196
);
192197

193198
return next;
@@ -203,7 +208,7 @@ function AccountDetail({ address }: { address: string }) {
203208
const next = new Map<string, string>(prev);
204209
next.set(
205210
transactionId,
206-
`Error sending ${amount} wei to ${to}: ${data}`,
211+
`Error sending ${amount} ether to ${to}: ${data}`,
207212
);
208213

209214
return next;
@@ -242,7 +247,7 @@ function AccountDetail({ address }: { address: string }) {
242247
return (
243248
<>
244249
<div>{address}</div>
245-
<div>Balance in wei: {`${balance}`}</div>
250+
<div>Balance in ether: {`${balance}`}</div>
246251
<form onSubmit={transfer}>
247252
<label>
248253
Transfer to:{" "}
@@ -257,9 +262,11 @@ function AccountDetail({ address }: { address: string }) {
257262
<span style={{ paddingRight: "5px" }}></span>
258263

259264
<label>
260-
Transfer amount in wei:{" "}
265+
Transfer amount in ether:{" "}
261266
<input
262-
value={!isNaN(transferAmount) ? transferAmount : ""}
267+
value={
268+
transferAmount === undefined ? "" : transferAmount.toString()
269+
}
263270
onChange={transferFormChange}
264271
name="amount"
265272
type="number"

templates/demo/web3js-react-dapp-demo/src/Erc20Detail.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ function Erc20Detail({ address }: { address: string }) {
9696
]);
9797

9898
const [transferTo, setTransferTo] = useState<string>("");
99-
const [transferAmount, setTransferAmount] = useState<number>(NaN);
99+
const [transferAmount, setTransferAmount] = useState<bigint | undefined>(
100+
undefined,
101+
);
100102
const [isFormValid, setIsFormValid] = useState<boolean>(false);
101103

102104
function isValidAddress(address: string): boolean {
@@ -110,13 +112,13 @@ function Erc20Detail({ address }: { address: string }) {
110112
setTransferTo(to);
111113
}
112114

113-
let amount: number = transferAmount;
115+
let amount: bigint | undefined = transferAmount;
114116
if (e.target.name === "amount") {
115-
amount = parseInt(e.target.value);
117+
amount = BigInt(e.target.value);
116118
setTransferAmount(amount);
117119
}
118120

119-
setIsFormValid(isValidAddress(to) && !isNaN(amount));
121+
setIsFormValid(isValidAddress(to) && amount !== undefined);
120122
}
121123

122124
async function transfer(e: FormEvent<HTMLFormElement>): Promise<void> {
@@ -135,7 +137,7 @@ function Erc20Detail({ address }: { address: string }) {
135137
}
136138

137139
setTransferTo("");
138-
setTransferAmount(NaN);
140+
setTransferAmount(undefined);
139141
setIsFormValid(false);
140142

141143
const gas: bigint = await erc20.methods
@@ -187,7 +189,9 @@ function Erc20Detail({ address }: { address: string }) {
187189
<label>
188190
Transfer amount:{" "}
189191
<input
190-
value={!isNaN(transferAmount) ? transferAmount : ""}
192+
value={
193+
transferAmount === undefined ? "" : transferAmount.toString()
194+
}
191195
onChange={transferFormChange}
192196
name="amount"
193197
type="number"

0 commit comments

Comments
 (0)