Skip to content

Commit 3e47049

Browse files
committed
ui tweaks
1 parent b3a1c4b commit 3e47049

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

src/App.tsx

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
custom,
1010
type TransactionReceipt,
1111
} from "viem";
12-
import { getAddresses, requestAddresses, waitForTransactionReceipt } from "viem/actions";
12+
import { waitForTransactionReceipt } from "viem/actions";
1313

1414
import { api, applyChainId, isOk, renderJSON } from "./utils/helpers.ts";
1515
import type {
@@ -61,8 +61,6 @@ export function App() {
6161
}, [selected, chain]);
6262

6363
const ensureServerConnected = useCallback(async () => {
64-
if (confirmed) return;
65-
6664
try {
6765
const resp = await api<
6866
ApiOk<{ connected: boolean; account?: string; chainId?: number }> | ApiErr
@@ -88,10 +86,12 @@ export function App() {
8886
}
8987
}
9088
} catch {}
91-
}, [account, chainId, confirmed]);
89+
}, [account, chainId]);
9290

9391
const pollTick = useCallback(async () => {
94-
await ensureServerConnected();
92+
if (!confirmed) {
93+
await ensureServerConnected();
94+
}
9595

9696
try {
9797
const resp = await api<ApiOk<PendingAny> | ApiErr>("/api/transaction/request");
@@ -117,26 +117,27 @@ export function App() {
117117
}, [ensureServerConnected, pending]);
118118

119119
const connect = async () => {
120-
if (!walletClient || !selected) return;
121-
if (confirmed) return;
120+
if (!selected || confirmed) return;
122121

123-
const addrs = (await requestAddresses(walletClient)) as readonly Address[];
124-
setAccount(addrs[0] as Address | undefined);
122+
const addrs = (await selected.provider.request({
123+
method: "eth_requestAccounts",
124+
})) as string[];
125+
setAccount((addrs?.[0] as Address) ?? undefined);
125126

126127
try {
127128
const raw = await selected.provider.request<string>({ method: "eth_chainId" });
128129
applyChainId(raw, setChainId, setChain);
129130
} catch {
130131
setChainId(undefined);
131132
setChain(undefined);
132-
}
133+
}
133134
};
134135

135136
const confirm = async () => {
136137
await ensureServerConnected();
137138

138139
setConfirmed(true);
139-
}
140+
};
140141

141142
const signAndSendCurrent = async () => {
142143
if (!walletClient || !selected || !pending?.request) return;
@@ -232,17 +233,17 @@ export function App() {
232233
useEffect(() => {
233234
if (!selected) return;
234235

235-
const onAccountsChanged = (accounts: readonly string[]) => {
236-
if (confirmed) return;
236+
const onAccountsChanged = (accounts: readonly string[]) => {
237+
if (confirmed) return;
237238

238-
setAccount((accounts[0] as Address) ?? undefined);
239-
};
239+
setAccount((accounts[0] as Address) ?? undefined);
240+
};
240241

241-
const onChainChanged = (raw: unknown) => {
242-
if (confirmed) return;
242+
const onChainChanged = (raw: unknown) => {
243+
if (confirmed) return;
243244

244-
applyChainId(raw, setChainId, setChain);
245-
};
245+
applyChainId(raw, setChainId, setChain);
246+
};
246247

247248
selected.provider.on?.("accountsChanged", onAccountsChanged);
248249
selected.provider.on?.("chainChanged", onChainChanged);
@@ -303,7 +304,7 @@ export function App() {
303304
{providers.length === 0 && <p>No wallets found.</p>}
304305

305306
{selected && !account && (
306-
<button type="button" className="wallet-connect" onClick={connect}>
307+
<button type="button" className="wallet-connect" onClick={connect} disabled={confirmed}>
307308
Connect Wallet
308309
</button>
309310
)}
@@ -326,7 +327,7 @@ rpc: ${chain?.rpcUrls?.default?.http?.[0] ?? chain?.rpcUrls?.public?.http?.[
326327
</>
327328
)}
328329

329-
{selected && account && confirmed && (
330+
{selected && account && confirmed && !lastTxHash && (
330331
<>
331332
<div className="section-title">To Sign</div>
332333
<div className="box">
@@ -349,7 +350,7 @@ rpc: ${chain?.rpcUrls?.default?.http?.[0] ?? chain?.rpcUrls?.public?.http?.[
349350
</>
350351
)}
351352

352-
{selected && account && pending && (
353+
{selected && account && pending && confirmed && (
353354
<button type="button" className="wallet-send" onClick={signAndSendCurrent}>
354355
Sign & Send
355356
</button>

0 commit comments

Comments
 (0)