Skip to content

Commit affdeac

Browse files
fix wallet connection
1 parent 99577fe commit affdeac

File tree

1 file changed

+47
-34
lines changed

1 file changed

+47
-34
lines changed

toolbox/src/demo/ui/ConnectWallet.tsx

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,39 @@ export const ConnectWallet = ({ children, required }: { children: React.ReactNod
1111
const [hasWallet, setHasWallet] = useState<boolean>(false);
1212
const { showBoundary } = useErrorBoundary();
1313

14+
function handleAccountsChanged(accounts: string[]) {
15+
if (accounts.length === 0) {
16+
setWalletEVMAddress("");
17+
return
18+
} else if (accounts.length > 1) {
19+
showBoundary(new Error("Multiple accounts found, we don't support that yet"));
20+
return
21+
}
22+
23+
//re-create wallet with new account
24+
setCoreWalletClient(createCoreWalletClient(accounts[0] as `0x${string}`));
25+
setWalletEVMAddress(accounts[0] as `0x${string}`);
26+
27+
coreWalletClient.getPChainAddress().then(setPChainAddress).catch(showBoundary);
28+
29+
if (walletChainId === 0) {
30+
coreWalletClient.getChainId().then(onChainChanged).catch(showBoundary);
31+
}
32+
}
33+
34+
const onChainChanged = (chainId: string | number) => {
35+
if (typeof chainId === "string") {
36+
chainId = parseInt(chainId, 16);
37+
}
38+
39+
setWalletChainId(chainId);
40+
coreWalletClient.getPChainAddress().then(setPChainAddress).catch(showBoundary);
41+
42+
coreWalletClient.getEthereumChain().then(({ isTestnet }) => {
43+
setAvalancheNetworkID(isTestnet ? networkIDs.FujiID : networkIDs.MainnetID);
44+
}).catch(showBoundary);
45+
}
46+
1447
useEffect(() => {
1548
async function init() {
1649
try {
@@ -22,42 +55,17 @@ export const ConnectWallet = ({ children, required }: { children: React.ReactNod
2255
return
2356
}
2457

25-
window.avalanche?.on("accountsChanged", async (accounts: string[]) => {
26-
if (accounts.length === 0) {
27-
setWalletEVMAddress("");
28-
return
29-
} else if (accounts.length > 1) {
30-
showBoundary(new Error("Multiple accounts found, we don't support that yet"));
31-
return
32-
}
33-
34-
//re-create wallet with new account
35-
setCoreWalletClient(createCoreWalletClient(accounts[0] as `0x${string}`));
36-
37-
setWalletEVMAddress(accounts[0] as `0x${string}`);
38-
39-
coreWalletClient.getPChainAddress().then(setPChainAddress).catch(showBoundary);
40-
41-
if (walletChainId === 0) {
42-
coreWalletClient.getChainId().then(onChainChanged).catch(showBoundary);
43-
}
44-
});
45-
58+
window.avalanche?.on("accountsChanged", handleAccountsChanged);
59+
window.avalanche.on("chainChanged", onChainChanged);
4660

47-
const onChainChanged = (chainId: string | number) => {
48-
if (typeof chainId === "string") {
49-
chainId = parseInt(chainId, 16);
61+
try {
62+
const accounts = await window.avalanche?.request<string[]>({ method: "eth_accounts" });
63+
if (accounts) {
64+
handleAccountsChanged(accounts);
5065
}
51-
52-
setWalletChainId(chainId);
53-
coreWalletClient.getPChainAddress().then(setPChainAddress).catch(showBoundary);
54-
55-
coreWalletClient.getEthereumChain().then(({ isTestnet }) => {
56-
setAvalancheNetworkID(isTestnet ? networkIDs.FujiID : networkIDs.MainnetID);
57-
}).catch(showBoundary);
66+
} catch (error) {
67+
//Ignore error, it's expected if the user has not connected their wallet yet
5868
}
59-
window.avalanche.on("chainChanged", onChainChanged);
60-
6169
} catch (error) {
6270
showBoundary(error);
6371
}
@@ -66,8 +74,13 @@ export const ConnectWallet = ({ children, required }: { children: React.ReactNod
6674
}, []);
6775

6876
async function connectWallet() {
69-
window.avalanche?.request({ method: "eth_requestAccounts" }).catch((error) => {
77+
console.log("Connecting wallet");
78+
window.avalanche?.request<string[]>({ method: "eth_requestAccounts" }).catch((error) => {
7079
showBoundary(error);
80+
}).then((accounts?: string[] | void) => {
81+
if (accounts) {
82+
handleAccountsChanged(accounts);
83+
}
7184
});
7285
}
7386

0 commit comments

Comments
 (0)