Skip to content

Commit 03e78a5

Browse files
committed
fix issue external wallets are stuck when enabling WC
1 parent 392bee1 commit 03e78a5

File tree

1 file changed

+55
-48
lines changed

1 file changed

+55
-48
lines changed

packages/modal/src/modalManager.ts

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -314,33 +314,35 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal {
314314
if (externalWalletsInitialized) return;
315315
const connectorsConfig: Record<string, BaseConnectorConfig> = {};
316316
// we do it like this because we don't want one slow connector to delay the load of the entire external wallet section.
317-
this.connectors.forEach(async (connector) => {
318-
const connectorName = connector.name;
319-
if (connector?.type === CONNECTOR_CATEGORY.EXTERNAL) {
320-
log.debug("init external wallet", this.cachedConnector, connectorName, connector.status);
321-
this.subscribeToConnectorEvents(connector);
322-
// we are not initializing cached connector here as it is already being initialized in initModal before.
323-
if (this.cachedConnector === connectorName) {
324-
return;
325-
}
326-
if (connector.status === CONNECTOR_STATUS.NOT_READY) {
327-
await connector
328-
.init({ autoConnect: this.cachedConnector === connectorName, chainId: this.currentChain.chainId })
329-
.then<undefined>(() => {
330-
const connectorModalConfig = (this.modalConfig.connectors as Record<WALLET_CONNECTOR_TYPE, ModalConfig>)[connectorName];
331-
connectorsConfig[connectorName] = { ...connectorModalConfig, isInjected: connector.isInjected };
332-
this.loginModal.addWalletLogins(connectorsConfig, { showExternalWalletsOnly: !!options?.showExternalWalletsOnly });
333-
return undefined;
334-
})
335-
.catch((error: unknown) => log.error(error, "error while initializing connector", connectorName));
336-
} else if (connector.status === CONNECTOR_STATUS.READY || connector.status === CONNECTOR_STATUS.CONNECTING) {
337-
// we use connecting status for wallet connect
338-
const connectorModalConfig = (this.modalConfig.connectors as Record<WALLET_CONNECTOR_TYPE, ModalConfig>)[connectorName];
339-
connectorsConfig[connectorName] = { ...connectorModalConfig, isInjected: connector.isInjected };
340-
this.loginModal.addWalletLogins(connectorsConfig, { showExternalWalletsOnly: !!options?.showExternalWalletsOnly });
317+
await Promise.all(
318+
this.connectors.map(async (connector) => {
319+
const connectorName = connector.name;
320+
if (connector?.type === CONNECTOR_CATEGORY.EXTERNAL) {
321+
log.debug("init external wallet", this.cachedConnector, connectorName, connector.status);
322+
this.subscribeToConnectorEvents(connector);
323+
// we are not initializing cached connector here as it is already being initialized in initModal before.
324+
if (this.cachedConnector === connectorName) {
325+
return;
326+
}
327+
if (connector.status === CONNECTOR_STATUS.NOT_READY) {
328+
await connector
329+
.init({ autoConnect: this.cachedConnector === connectorName, chainId: this.currentChain.chainId })
330+
.then<undefined>(() => {
331+
const connectorModalConfig = (this.modalConfig.connectors as Record<WALLET_CONNECTOR_TYPE, ModalConfig>)[connectorName];
332+
connectorsConfig[connectorName] = { ...connectorModalConfig, isInjected: connector.isInjected };
333+
this.loginModal.addWalletLogins(connectorsConfig, { showExternalWalletsOnly: !!options?.showExternalWalletsOnly });
334+
return undefined;
335+
})
336+
.catch((error: unknown) => log.error(error, "error while initializing connector", connectorName));
337+
} else if (connector.status === CONNECTOR_STATUS.READY || connector.status === CONNECTOR_STATUS.CONNECTING) {
338+
// we use connecting status for wallet connect
339+
const connectorModalConfig = (this.modalConfig.connectors as Record<WALLET_CONNECTOR_TYPE, ModalConfig>)[connectorName];
340+
connectorsConfig[connectorName] = { ...connectorModalConfig, isInjected: connector.isInjected };
341+
this.loginModal.addWalletLogins(connectorsConfig, { showExternalWalletsOnly: !!options?.showExternalWalletsOnly });
342+
}
341343
}
342-
}
343-
});
344+
})
345+
);
344346
}
345347

346348
private subscribeToLoginModalEvents(): void {
@@ -353,6 +355,7 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal {
353355
});
354356
this.loginModal.on(LOGIN_MODAL_EVENTS.INIT_EXTERNAL_WALLETS, async (params: { externalWalletsInitialized: boolean }) => {
355357
await this.initExternalConnectors(params.externalWalletsInitialized);
358+
this.setupWalletConnect({ isModalActive: true });
356359
});
357360
this.loginModal.on(LOGIN_MODAL_EVENTS.DISCONNECT, async () => {
358361
try {
@@ -364,29 +367,33 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal {
364367
this.loginModal.on(LOGIN_MODAL_EVENTS.MODAL_VISIBILITY, async (visibility: boolean) => {
365368
log.debug("is login modal visible", visibility);
366369
this.emit(LOGIN_MODAL_EVENTS.MODAL_VISIBILITY, visibility);
367-
const connector = this.getConnector(WALLET_CONNECTORS.WALLET_CONNECT_V2);
368-
if (connector) {
369-
const walletConnectStatus = connector?.status;
370-
log.debug("trying refreshing wc session", visibility, walletConnectStatus);
371-
if (visibility && (walletConnectStatus === CONNECTOR_STATUS.READY || walletConnectStatus === CONNECTOR_STATUS.CONNECTING)) {
372-
log.debug("refreshing wc session");
373-
374-
// refreshing session for wallet connect whenever modal is opened.
375-
try {
376-
connector.connect({ chainId: this.currentChain.chainId });
377-
} catch (error) {
378-
log.error(`Error while disconnecting to wallet connect in core`, error);
379-
}
380-
}
381-
if (
382-
!visibility &&
383-
this.status === CONNECTOR_STATUS.CONNECTED &&
384-
(walletConnectStatus === CONNECTOR_STATUS.READY || walletConnectStatus === CONNECTOR_STATUS.CONNECTING)
385-
) {
386-
log.debug("this stops wc connector from trying to reconnect once proposal expires");
387-
connector.status = CONNECTOR_STATUS.READY;
370+
this.setupWalletConnect({ isModalActive: visibility });
371+
});
372+
}
373+
374+
private setupWalletConnect({ isModalActive }: { isModalActive: boolean }) {
375+
const connector = this.getConnector(WALLET_CONNECTORS.WALLET_CONNECT_V2);
376+
if (connector) {
377+
const walletConnectStatus = connector?.status;
378+
log.debug("trying refreshing wc session", isModalActive, walletConnectStatus);
379+
if (isModalActive && (walletConnectStatus === CONNECTOR_STATUS.READY || walletConnectStatus === CONNECTOR_STATUS.CONNECTING)) {
380+
log.debug("refreshing wc session");
381+
382+
// refreshing session for wallet connect whenever modal is opened.
383+
try {
384+
connector.connect({ chainId: this.currentChain.chainId });
385+
} catch (error) {
386+
log.error(`Error while disconnecting to wallet connect in core`, error);
388387
}
389388
}
390-
});
389+
if (
390+
!isModalActive &&
391+
this.status === CONNECTOR_STATUS.CONNECTED &&
392+
(walletConnectStatus === CONNECTOR_STATUS.READY || walletConnectStatus === CONNECTOR_STATUS.CONNECTING)
393+
) {
394+
log.debug("this stops wc connector from trying to reconnect once proposal expires");
395+
connector.status = CONNECTOR_STATUS.READY;
396+
}
397+
}
391398
}
392399
}

0 commit comments

Comments
 (0)