@@ -11,6 +11,39 @@ export const ConnectWallet = ({ children, required }: { children: React.ReactNod
11
11
const [ hasWallet , setHasWallet ] = useState < boolean > ( false ) ;
12
12
const { showBoundary } = useErrorBoundary ( ) ;
13
13
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
+
14
47
useEffect ( ( ) => {
15
48
async function init ( ) {
16
49
try {
@@ -22,42 +55,17 @@ export const ConnectWallet = ({ children, required }: { children: React.ReactNod
22
55
return
23
56
}
24
57
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 ) ;
46
60
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 ) ;
50
65
}
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
58
68
}
59
- window . avalanche . on ( "chainChanged" , onChainChanged ) ;
60
-
61
69
} catch ( error ) {
62
70
showBoundary ( error ) ;
63
71
}
@@ -66,8 +74,13 @@ export const ConnectWallet = ({ children, required }: { children: React.ReactNod
66
74
} , [ ] ) ;
67
75
68
76
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 ) => {
70
79
showBoundary ( error ) ;
80
+ } ) . then ( ( accounts ?: string [ ] | void ) => {
81
+ if ( accounts ) {
82
+ handleAccountsChanged ( accounts ) ;
83
+ }
71
84
} ) ;
72
85
}
73
86
0 commit comments