1
1
import { type ReactNode , useEffect , useState } from 'react'
2
- import { useSwitchChain } from 'wagmi'
2
+ import { useChainId , useSwitchChain } from 'wagmi'
3
3
import type { NetworkDef } from '@ui/utils'
4
4
import { ConnectionContext , useWagmiWallet } from '@ui-kit/features/connect-wallet/lib/ConnectionContext'
5
5
import { ConnectState } from '@ui-kit/features/connect-wallet/lib/types'
@@ -27,6 +27,7 @@ export const ConnectionProvider = <TChainId extends number, NetworkConfig extend
27
27
children : ReactNode
28
28
} ) => {
29
29
const [ connectState , setConnectState ] = useState < ConnectState > ( LOADING )
30
+ const chainId = useChainId ( )
30
31
const { switchChainAsync } = useSwitchChain ( )
31
32
const { wallet, provider, isReconnecting } = useWagmiWallet ( )
32
33
const isFocused = useIsDocumentFocused ( )
@@ -38,12 +39,12 @@ export const ConnectionProvider = <TChainId extends number, NetworkConfig extend
38
39
* This is separate from the rest of initApp to avoid unnecessary reinitialization, as isFocused can change frequently.
39
40
*/
40
41
async function updateWalletChain ( ) {
41
- const chainId = Number ( network . chainId ) as TChainId
42
- if ( wallet && wallet ?. chainId !== chainId ) {
42
+ const networkChainId = Number ( network . chainId ) as TChainId
43
+ if ( networkChainId !== chainId ) {
43
44
setConnectState ( LOADING )
44
- if ( isFocused && ! ( await switchChainAsync ( { chainId : chainId as WagmiChainId } ) ) ) {
45
+ if ( isFocused && ! ( await switchChainAsync ( { chainId : networkChainId as WagmiChainId } ) ) ) {
45
46
setConnectState ( FAILURE )
46
- onChainUnavailable ( [ chainId , wallet ?. chainId as TChainId ] )
47
+ onChainUnavailable ( [ networkChainId , chainId as TChainId ] )
47
48
}
48
49
return // hook is called again after since it depends on walletChainId
49
50
}
@@ -52,7 +53,7 @@ export const ConnectionProvider = <TChainId extends number, NetworkConfig extend
52
53
console . error ( 'Error updating wallet chain' , e )
53
54
setConnectState ( FAILURE )
54
55
} )
55
- } , [ isFocused , network . chainId , onChainUnavailable , switchChainAsync , wallet ] )
56
+ } , [ isFocused , chainId , network . chainId , onChainUnavailable , switchChainAsync , wallet ] )
56
57
57
58
useEffect ( ( ) => {
58
59
if ( isReconnecting ) return // wait for wagmi to auto-reconnect
@@ -63,14 +64,14 @@ export const ConnectionProvider = <TChainId extends number, NetworkConfig extend
63
64
* Initialize the app by connecting to the wallet and setting up the library.
64
65
*/
65
66
const initApp = async ( ) => {
66
- const chainId = Number ( network . chainId ) as TChainId
67
+ const networkChainId = Number ( network . chainId ) as TChainId
67
68
try {
68
- if ( wallet && wallet ?. chainId !== chainId ) {
69
+ if ( chainId !== networkChainId ) {
69
70
return // wait for the wallet to be connected to the right chain
70
71
}
71
72
72
73
const prevLib = globalLibs . get ( libKey )
73
- if ( isWalletMatching ( wallet , prevLib , chainId ) ) {
74
+ if ( isWalletMatching ( wallet , prevLib , networkChainId ) ) {
74
75
setConnectState ( SUCCESS )
75
76
return // already connected to the right chain and wallet, no need to reinitialize
76
77
}
@@ -79,7 +80,7 @@ export const ConnectionProvider = <TChainId extends number, NetworkConfig extend
79
80
setConnectState ( LOADING )
80
81
console . info (
81
82
`Initializing ${ libKey } for ${ network . name } (${ network . chainId } )` ,
82
- wallet ? `Wallet ${ wallet ?. account ?. address } with chain ${ wallet ?. chainId } ` : 'without wallet' ,
83
+ wallet ? `Wallet ${ wallet ?. account ?. address } with chain ${ chainId } ` : 'without wallet' ,
83
84
prevLib
84
85
? `Old library had ${ prevLib . signerAddress ? `signer ${ prevLib . signerAddress } ` : 'no signer' } with chain ${ prevLib . chainId } `
85
86
: `First initialization` ,
@@ -97,7 +98,7 @@ export const ConnectionProvider = <TChainId extends number, NetworkConfig extend
97
98
}
98
99
void initApp ( )
99
100
return ( ) => abort . abort ( )
100
- } , [ isReconnecting , libKey , network , wallet ] )
101
+ } , [ chainId , isReconnecting , libKey , network , wallet ] )
101
102
102
103
const curveApi = globalLibs . getMatching ( 'curveApi' , wallet , network ?. chainId )
103
104
const llamaApi = globalLibs . getMatching ( 'llamaApi' , wallet , network ?. chainId )
0 commit comments