From 41965f033fbdaea8517b0d22a5d4f1ad5aaf3f32 Mon Sep 17 00:00:00 2001 From: Corantin Date: Wed, 5 Jul 2023 11:17:42 -0400 Subject: [PATCH] Switch network --- .../src/components/views/quest-list.tsx | 3 +- .../react-app/src/contexts/wallet.context.tsx | 36 ++++++++++--------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/packages/react-app/src/components/views/quest-list.tsx b/packages/react-app/src/components/views/quest-list.tsx index 57075c75..f8e5c3ee 100644 --- a/packages/react-app/src/components/views/quest-list.tsx +++ b/packages/react-app/src/components/views/quest-list.tsx @@ -75,7 +75,7 @@ const ScrollLabelStyled = styled.div` `; export default function QuestList() { - const { walletAddress, walletConnected } = useWallet(); + const { walletAddress } = useWallet(); const [quests, setQuests] = useState([]); const [isLoading, setIsLoading] = useState(false); const [newQuestLoading, setNewQuestLoading] = useState(false); @@ -172,7 +172,6 @@ export default function QuestList() { setHasMore(res.length >= QUESTS_PAGE_SIZE); }, ); - console.log(walletAddress, walletConnected); } }; diff --git a/packages/react-app/src/contexts/wallet.context.tsx b/packages/react-app/src/contexts/wallet.context.tsx index 756581e0..5c0cf8cf 100644 --- a/packages/react-app/src/contexts/wallet.context.tsx +++ b/packages/react-app/src/contexts/wallet.context.tsx @@ -36,7 +36,8 @@ type Props = { function WalletAugmented({ children }: Props) { const wallet = useWallet(); const ethereum = wallet?.ethereum; - const [isWrongNetwork, setIsWrongNetwork] = useState(false); + const isWrongChainError = (window as any).globalError?.name === 'ChainUnknownError'; + const [isWrongNetwork, setIsWrongNetwork] = useState(isWrongChainError); const [activatingId, setActivating] = useState(); const [isConnected, setIsConnected] = useState(false); const [walletConnectOpened, openWalletConnect] = useState(false); @@ -44,23 +45,19 @@ function WalletAugmented({ children }: Props) { let timeoutInstance: number | undefined; useEffect(() => { + if (!isWrongChainError) { + const lastWalletConnected = localStorage.getItem('LAST_WALLET_CONNECTOR'); + if (lastWalletConnected) { + handleConnect(lastWalletConnected); + } + } + const handleErr = (ev: ErrorEvent) => { if (ev.error.name === 'ChainUnknownError') { (window as any).globalError = ev.error; } }; window.addEventListener('error', handleErr); - const globalErrorName = (window as any).globalError?.name; - if (globalErrorName === 'ChainUnknownError') { - setIsWrongNetwork(true); - setIsConnected(false); - setActivating(undefined); - } else { - const lastWalletConnected = localStorage.getItem('LAST_WALLET_CONNECTOR'); - if (lastWalletConnected) { - handleConnect(lastWalletConnected); - } - } return () => { window.removeEventListener('error', handleErr); @@ -143,9 +140,16 @@ function WalletAugmented({ children }: Props) { if (!newChainId) { newChainId = chainId; } - if (ethereum && EXPECTED_CHAIN_ID.includes(newChainId) && +ethereum.chainId !== newChainId) { + + const ethConnection = ethereum ?? (window as any).ethereum; + + if ( + ethConnection && + EXPECTED_CHAIN_ID.includes(newChainId) && + +ethConnection.chainId !== newChainId + ) { try { - await ethereum.request({ + await ethConnection.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: Web3.utils.toHex(newChainId) }], }); @@ -154,7 +158,7 @@ function WalletAugmented({ children }: Props) { if (error.code === 4902) { const network = getNetwork(); try { - await ethereum.request({ + await ethConnection.request({ method: 'wallet_addEthereumChain', params: [ { @@ -205,7 +209,7 @@ function WalletAugmented({ children }: Props) { function WalletProvider({ children }: Props) { const connectors = getUseWalletConnectors(); return ( - + {children} );