@@ -45,6 +45,8 @@ import type { WCAutoConnectOptions, WCConnectOptions } from "./types.js";
4545
4646type WCProvider = InstanceType < typeof UniversalProvider > ;
4747
48+ let cachedProvider : WCProvider | null = null ;
49+
4850type SavedConnectParams = {
4951 optionalChains ?: Chain [ ] ;
5052 chain ?: Chain ;
@@ -63,7 +65,7 @@ const storageKeys = {
6365 * @returns True if the wallet is a Wallet Connect wallet, false otherwise.
6466 */
6567export function isWalletConnect (
66- wallet : Wallet < WalletId > ,
68+ wallet : Wallet < WalletId >
6769) : wallet is Wallet < "walletConnect" > {
6870 return wallet . id === "walletConnect" ;
6971}
@@ -76,7 +78,7 @@ export async function connectWC(
7678 emitter : WalletEmitter < WCSupportedWalletIds > ,
7779 walletId : WCSupportedWalletIds | "walletConnect" ,
7880 storage : AsyncStorage ,
79- sessionHandler ?: ( uri : string ) => void ,
81+ sessionHandler ?: ( uri : string ) => void
8082) : Promise < ReturnType < typeof onConnect > > {
8183 const provider = await initProvider ( options , walletId , sessionHandler ) ;
8284 const wcOptions = options . walletConnect ;
@@ -121,32 +123,34 @@ export async function connectWC(
121123 optionalChains : optionalChains ,
122124 } ) ;
123125
124- // For UniversalProvider, we need to connect with namespaces
125- await provider . connect ( {
126- ...( wcOptions ?. pairingTopic
127- ? { pairingTopic : wcOptions ?. pairingTopic }
128- : { } ) ,
129- namespaces : {
130- [ NAMESPACE ] : {
131- chains : chainsToRequest ,
132- events : [ "chainChanged" , "accountsChanged" ] ,
133- methods : [
134- "eth_sendTransaction" ,
135- "eth_signTransaction" ,
136- "eth_sign" ,
137- "personal_sign" ,
138- "eth_signTypedData" ,
139- "wallet_switchEthereumChain" ,
140- "wallet_addEthereumChain" ,
141- ] ,
142- rpcMap,
126+ if ( ! provider . session ) {
127+ // For UniversalProvider, we need to connect with namespaces
128+ await provider . connect ( {
129+ ...( wcOptions ?. pairingTopic
130+ ? { pairingTopic : wcOptions ?. pairingTopic }
131+ : { } ) ,
132+ namespaces : {
133+ [ NAMESPACE ] : {
134+ chains : chainsToRequest ,
135+ events : [ "chainChanged" , "accountsChanged" ] ,
136+ methods : [
137+ "eth_sendTransaction" ,
138+ "eth_signTransaction" ,
139+ "eth_sign" ,
140+ "personal_sign" ,
141+ "eth_signTypedData" ,
142+ "wallet_switchEthereumChain" ,
143+ "wallet_addEthereumChain" ,
144+ ] ,
145+ rpcMap,
146+ } ,
143147 } ,
144- } ,
145- } ) ;
148+ } ) ;
149+ }
146150
147151 setRequestedChainsIds (
148152 chainsToRequest . map ( ( x ) => Number ( x . split ( ":" ) [ 1 ] ) ) ,
149- storage ,
153+ storage
150154 ) ;
151155 const currentChainId = chainsToRequest [ 0 ] ?. split ( ":" ) [ 1 ] || 1 ;
152156 const providerChainId = normalizeChainId ( currentChainId ) ;
@@ -155,7 +159,7 @@ export async function connectWC(
155159 method : "eth_requestAccounts" ,
156160 params : [ ] ,
157161 } ,
158- `eip155:${ providerChainId } ` ,
162+ `eip155:${ providerChainId } `
159163 ) ;
160164 const address = accounts [ 0 ] ;
161165 if ( ! address ) {
@@ -195,7 +199,7 @@ export async function autoConnectWC(
195199 emitter : WalletEmitter < WCSupportedWalletIds > ,
196200 walletId : WCSupportedWalletIds | "walletConnect" ,
197201 storage : AsyncStorage ,
198- sessionHandler ?: ( uri : string ) => void ,
202+ sessionHandler ?: ( uri : string ) => void
199203) : Promise < ReturnType < typeof onConnect > > {
200204 const savedConnectParams : SavedConnectParams | null = storage
201205 ? await getSavedConnectParamsFromStorage ( storage , walletId )
@@ -217,7 +221,6 @@ export async function autoConnectWC(
217221 } ,
218222 walletId ,
219223 sessionHandler ,
220- true , // is auto connect
221224 ) ;
222225
223226 // For UniversalProvider, get accounts from enable() method
@@ -246,8 +249,11 @@ async function initProvider(
246249 options : WCConnectOptions ,
247250 walletId : WCSupportedWalletIds | "walletConnect" ,
248251 sessionRequestHandler ?: ( uri : string ) => void | Promise < void > ,
249- isAutoConnect = false ,
250252) {
253+ if ( cachedProvider ) {
254+ return cachedProvider ;
255+ }
256+
251257 const walletInfo = await getWalletInfo ( walletId ) ;
252258 const wcOptions = options . walletConnect ;
253259 const { UniversalProvider } = await import (
@@ -277,19 +283,10 @@ async function initProvider(
277283 url : wcOptions ?. appMetadata ?. url || getDefaultAppMetadata ( ) . url ,
278284 } ,
279285 projectId : wcOptions ?. projectId || DEFAULT_PROJECT_ID ,
280- relayUrl : "wss://relay.walletconnect.com" ,
281286 } ) ;
282287
283288 provider . events . setMaxListeners ( Number . POSITIVE_INFINITY ) ;
284289
285- // disconnect the provider if chains are stale when (if not auto connecting)
286- if ( ! isAutoConnect ) {
287- // For UniversalProvider, we handle disconnection differently
288- if ( provider . session ) {
289- await provider . disconnect ( ) ;
290- }
291- }
292-
293290 if ( walletId !== "walletConnect" ) {
294291 async function handleSessionRequest ( ) {
295292 const walletLinkToOpen =
@@ -307,9 +304,12 @@ async function initProvider(
307304 provider . on ( "session_request_sent" , handleSessionRequest ) ;
308305 provider . events . addListener ( "disconnect" , ( ) => {
309306 provider . off ( "session_request_sent" , handleSessionRequest ) ;
307+ cachedProvider = null ;
310308 } ) ;
311309 }
312310
311+ cachedProvider = provider ;
312+
313313 return provider ;
314314}
315315
@@ -340,7 +340,7 @@ function createAccount({
340340 } ,
341341 ] ,
342342 } ,
343- `eip155:${ tx . chainId } ` ,
343+ `eip155:${ tx . chainId } `
344344 ) ) as Hex ;
345345
346346 trackTransaction ( {
@@ -372,7 +372,7 @@ function createAccount({
372372 method : "personal_sign" ,
373373 params : [ messageToSign , this . address ] ,
374374 } ,
375- `eip155:${ chain . id } ` ,
375+ `eip155:${ chain . id } `
376376 ) ;
377377 } ,
378378 async signTypedData ( _data ) {
@@ -401,7 +401,7 @@ function createAccount({
401401 method : "eth_signTypedData_v4" ,
402402 params : [ this . address , typedData ] ,
403403 } ,
404- `eip155:${ chain . id } ` ,
404+ `eip155:${ chain . id } `
405405 ) ;
406406 } ,
407407 } ;
@@ -415,7 +415,7 @@ function onConnect(
415415 provider : WCProvider ,
416416 emitter : WalletEmitter < WCSupportedWalletIds > ,
417417 storage : AsyncStorage ,
418- client : ThirdwebClient ,
418+ client : ThirdwebClient
419419) : [ Account , Chain , DisconnectFn , SwitchChainFn ] {
420420 const account = createAccount ( { address, chain, client, provider } ) ;
421421
@@ -424,6 +424,7 @@ function onConnect(
424424 provider . removeListener ( "chainChanged" , onChainChanged ) ;
425425 provider . removeListener ( "disconnect" , onDisconnect ) ;
426426 await provider . disconnect ( ) ;
427+ cachedProvider = null ;
427428 }
428429
429430 function onDisconnect ( ) {
@@ -488,7 +489,7 @@ async function switchChainWC(provider: WCProvider, chain: Chain) {
488489 */
489490function setRequestedChainsIds (
490491 chains : number [ ] | undefined ,
491- storage : AsyncStorage ,
492+ storage : AsyncStorage
492493) {
493494 storage ?. setItem ( storageKeys . requestedChains , stringify ( chains ) ) ;
494495}
0 commit comments