Skip to content

Commit

Permalink
fix: metamask switch network error
Browse files Browse the repository at this point in the history
  • Loading branch information
namgold committed Nov 1, 2023
1 parent afdae39 commit ee0750f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/hooks/web3/useChangeNetwork.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ChainId } from '@kyberswap/ks-sdk-core'
import { t } from '@lingui/macro'
import { captureException } from '@sentry/react'
import { AddEthereumChainParameter } from '@web3-react/types'
import { useCallback } from 'react'

import { NotificationType } from 'components/Announcement/type'
Expand Down Expand Up @@ -95,7 +96,7 @@ export function useChangeNetwork() {
notify({
title,
type: NotificationType.ERROR,
summary: message,
summary: friendlyError(message),
})
customFailureCallback?.(error)
},
Expand Down Expand Up @@ -128,17 +129,21 @@ export function useChangeNetwork() {
nativeCurrency: {
name: NETWORKS_INFO[desiredChainId].nativeToken.name,
symbol: NETWORKS_INFO[desiredChainId].nativeToken.symbol,
decimals: NETWORKS_INFO[desiredChainId].nativeToken.decimal,
decimals: 18 as const,
},
blockExplorerUrls: [NETWORKS_INFO[desiredChainId].etherscanUrl],
}
const addChainParameterWeb3: AddEthereumChainParameter = {
...addChainParameter,
chainId: desiredChainId,
}

enum Solution {
web3_react = 'web3_react',
provider_request = 'provider_request',
}
const solutions = {
[Solution.web3_react]: async () => await connector.activate(addChainParameter),
[Solution.web3_react]: async () => await connector.activate(addChainParameterWeb3),
[Solution.provider_request]: async () => {
const activeProvider = library?.provider ?? window.ethereum
if (activeProvider?.request) {
Expand Down Expand Up @@ -214,7 +219,7 @@ export function useChangeNetwork() {
extra: {
wallet: walletEVM.walletKey,
desiredChainId,
addChainParameter,
addChainParameterWeb3,
friendlyMessages: errors.map(friendlyError),
errors,
},
Expand Down
3 changes: 3 additions & 0 deletions src/utils/errorMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ function parseKnownPattern(text: string): string | undefined {

if (!error || error.includes('router: expired')) return t`An error occurred. Refresh the page and try again.`

if (matchPatterns(['already pending'], error))
return t`Request already pending on your wallet. Please open your wallet and approve it.`

if (
matchPatterns(
[
Expand Down

0 comments on commit ee0750f

Please sign in to comment.