From b13d45e6907a6739a1f4498f08498eb1dac03747 Mon Sep 17 00:00:00 2001 From: Nam Nguyen Date: Mon, 18 Sep 2023 10:16:04 +0700 Subject: [PATCH] fix: metamask auto reconnect after change account (#2244) --- src/components/Web3ReactManager/index.tsx | 4 +- src/hooks/index.ts | 45 +---------------------- 2 files changed, 3 insertions(+), 46 deletions(-) diff --git a/src/components/Web3ReactManager/index.tsx b/src/components/Web3ReactManager/index.tsx index fecea0a448..b43c750ff4 100644 --- a/src/components/Web3ReactManager/index.tsx +++ b/src/components/Web3ReactManager/index.tsx @@ -3,7 +3,7 @@ import { useEffect } from 'react' import { useDispatch, useSelector } from 'react-redux' import LocalLoader from 'components/LocalLoader' -import { useActiveWeb3React, useInactiveListener, useWeb3React } from 'hooks' +import { useActiveWeb3React, useWeb3React } from 'hooks' import { useEagerConnect } from 'hooks/web3/useEagerConnect' import { AppState } from 'state' import { updateChainId } from 'state/user/actions' @@ -16,8 +16,6 @@ export default function Web3ReactManager({ children }: { children: JSX.Element } // try to eagerly connect to an injected provider, if it exists and has granted access already const triedEager = useEagerConnect() - // when there's no account connected, react to logins (broadly speaking) on the injected provider, if it exists - useInactiveListener(!triedEager.current) const dispatch = useDispatch() /** On user change network from wallet, update chainId in store, only work on EVM wallet */ useEffect(() => { diff --git a/src/hooks/index.ts b/src/hooks/index.ts index 059a00291a..2b945bdab3 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -3,11 +3,11 @@ import { ChainId, ChainType, getChainType } from '@kyberswap/ks-sdk-core' import { Wallet, useWallet } from '@solana/wallet-adapter-react' import { useWeb3React as useWeb3ReactCore } from '@web3-react/core' import { Connector } from '@web3-react/types' -import { useEffect, useMemo } from 'react' +import { useMemo } from 'react' import { useSelector } from 'react-redux' import { useSearchParams } from 'react-router-dom' -import { blocto, gnosisSafe, metaMask, walletConnectV2 } from 'constants/connectors/evm' +import { blocto, gnosisSafe, walletConnectV2 } from 'constants/connectors/evm' import { MOCK_ACCOUNT_EVM, MOCK_ACCOUNT_SOLANA } from 'constants/env' import { NETWORKS_INFO, isSupportedChainId } from 'constants/networks' import { NetworkInfo } from 'constants/networks/type' @@ -136,44 +136,3 @@ export const useWeb3Solana = () => { const { connection } = useKyberSwapConfig() return { connection } } - -/** - * Use for network and injected - logs user in - * and out after checking what network they're on - */ -export function useInactiveListener(suppress = false) { - const { isEVM } = useActiveWeb3React() - const { active } = useWeb3React() // specifically using useWeb3React because of what this hook does - - useEffect(() => { - const { ethereum } = window - if (isEVM && ethereum?.on && !active && !suppress) { - const handleChainChanged = () => { - // eat errors - metaMask.activate().catch(error => { - console.error('Failed to activate after chain changed', error) - }) - } - - const handleAccountsChanged = (accounts: string[]) => { - if (accounts.length > 0) { - // eat errors - metaMask.activate().catch(error => { - console.error('Failed to activate after accounts changed', error) - }) - } - } - - ethereum.on('chainChanged', handleChainChanged) - ethereum.on('accountsChanged', handleAccountsChanged) - - return () => { - if (ethereum.removeListener) { - ethereum.removeListener('chainChanged', handleChainChanged) - ethereum.removeListener('accountsChanged', handleAccountsChanged) - } - } - } - return undefined - }, [active, suppress, isEVM]) -}