Skip to content

Commit

Permalink
Fix: don't display chain switch errors to the user, just log them
Browse files Browse the repository at this point in the history
  • Loading branch information
katspaugh committed Oct 24, 2023
1 parent d58d233 commit 2198205
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/services/exceptions/ErrorCodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 17 additions & 5 deletions src/services/walletconnect/WalletConnectWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2198205

Please sign in to comment.