From 21982055bd125188d01c9cf48123adeab75ae241 Mon Sep 17 00:00:00 2001 From: katspaugh Date: Tue, 24 Oct 2023 09:25:56 +0200 Subject: [PATCH] Fix: don't display chain switch errors to the user, just log them --- src/services/exceptions/ErrorCodes.ts | 2 ++ .../walletconnect/WalletConnectWallet.ts | 22 ++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/services/exceptions/ErrorCodes.ts b/src/services/exceptions/ErrorCodes.ts index e05a741fa4..fb7589efa4 100644 --- a/src/services/exceptions/ErrorCodes.ts +++ b/src/services/exceptions/ErrorCodes.ts @@ -63,6 +63,8 @@ enum ErrorCodes { _902 = '902: Error loading Safe Apps list', _903 = '903: Error loading Safe App manifest', _905 = '905: Third party cookies are disabled', + + _910 = '910: WalletConnect failed to switch chain', } export default ErrorCodes diff --git a/src/services/walletconnect/WalletConnectWallet.ts b/src/services/walletconnect/WalletConnectWallet.ts index b308f984ea..20f54d4bad 100644 --- a/src/services/walletconnect/WalletConnectWallet.ts +++ b/src/services/walletconnect/WalletConnectWallet.ts @@ -11,6 +11,8 @@ import { IS_PRODUCTION, WC_PROJECT_ID } from '@/config/constants' import { EIP155, SAFE_COMPATIBLE_METHODS, SAFE_WALLET_METADATA } from './constants' import { invariant } from '@/utils/helpers' import { getEip155ChainId, stripEip155Prefix } from './utils' +import { logError } from '../exceptions' +import ErrorCodes from '../exceptions/ErrorCodes' const SESSION_ADD_EVENT = 'session_add' as Web3WalletTypes.Event // Workaround: WalletConnect doesn't emit session_add event const SESSION_REJECT_EVENT = 'session_reject' as Web3WalletTypes.Event // Workaround: WalletConnect doesn't emit session_reject event @@ -111,6 +113,19 @@ class WalletConnectWallet { }) } + /** + * Switch chain and catch errors. + * Just log the errors because they happen all the time. + */ + private async switchChain(topic: string, chainId: string) { + try { + // Align the session with the current chainId + return await this.chainChanged(topic, chainId) + } catch (e) { + logError(ErrorCodes._910, e) + } + } + public async approveSession(proposal: Web3WalletTypes.SessionProposal, currentChainId: string, safeAddress: string) { assertWeb3Wallet(this.web3Wallet) @@ -122,10 +137,7 @@ class WalletConnectWallet { namespaces, }) - try { - // Align the session with the current chainId - await this.chainChanged(session.topic, currentChainId) - } catch {} + await this.switchChain(session.topic, currentChainId) // Workaround: WalletConnect doesn't have a session_add event this.web3Wallet?.events.emit(SESSION_ADD_EVENT, session) @@ -168,7 +180,7 @@ class WalletConnectWallet { } // Switch to the new chain - await this.chainChanged(session.topic, chainId) + await this.switchChain(session.topic, chainId) // Switch to the new Safe await this.accountsChanged(session.topic, chainId, safeAddress)