From c964497f3d16622a0f4ffd4d5f9d76dc3f271a2c Mon Sep 17 00:00:00 2001 From: Lukas Date: Wed, 19 Jan 2022 15:03:05 +0100 Subject: [PATCH] test: does wl support network change? (#356) --- .../NetworkMismatch/index.tsx | 53 +++++++++++-------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/src/components/TxConfirmationView/NetworkMismatch/index.tsx b/src/components/TxConfirmationView/NetworkMismatch/index.tsx index 3c6a39b6e..5c291d5c6 100644 --- a/src/components/TxConfirmationView/NetworkMismatch/index.tsx +++ b/src/components/TxConfirmationView/NetworkMismatch/index.tsx @@ -14,6 +14,8 @@ import { getNetworkConfig } from '../../../helpers/config/markets-and-network-co import messages from './messages'; import staticStyles from './style'; import { ChainId } from '@aave/contract-helpers'; +import { useWeb3React } from '@web3-react/core'; +import { providers } from 'ethers'; interface NetworkMismatchProps { neededChainId: ChainId; @@ -73,12 +75,13 @@ export default function NetworkMismatch({ }: NetworkMismatchProps) { const intl = useIntl(); const { currentTheme } = useThemeContext(); + const { library } = useWeb3React(); const { handleNetworkChange } = useUserWalletDataContext(); const config = ADD_CONFIG[neededChainId]; const isAddable = (global.window as any)?.ethereum?.isMetaMask && - ['browser'].includes(currentProviderName) && + ['browser', 'wallet-link'].includes(currentProviderName) && config; const { publicJsonRPCWSUrl, publicJsonRPCUrl } = getNetworkConfig(neededChainId); @@ -126,28 +129,32 @@ export default function NetworkMismatch({ { - try { - await (window as any).ethereum?.request({ - method: 'wallet_switchEthereumChain', - params: [{ chainId: `0x${neededChainId.toString(16)}` }], - }); - } catch (switchError) { - if (switchError.code === 4902) { - try { - await (window as any).ethereum?.request({ - method: 'wallet_addEthereumChain', - params: [ - { - chainId: `0x${neededChainId.toString(16)}`, - chainName: config.name, - nativeCurrency: config.nativeCurrency, - rpcUrls: [...publicJsonRPCUrl, publicJsonRPCWSUrl], - blockExplorerUrls: config.explorerUrls, - }, - ], - }); - } catch (addError) { - // TODO: handle error somehow + if (library) { + try { + await library.provider.request!({ + method: 'wallet_switchEthereumChain', + params: [{ chainId: `0x${neededChainId.toString(16)}` }], + }); + } catch (switchError) { + console.log(switchError); + if (switchError.code === 4902) { + try { + await library.provider.request!({ + method: 'wallet_addEthereumChain', + params: [ + { + chainId: `0x${neededChainId.toString(16)}`, + chainName: config.name, + nativeCurrency: config.nativeCurrency, + rpcUrls: [...publicJsonRPCUrl, publicJsonRPCWSUrl], + blockExplorerUrls: config.explorerUrls, + }, + ], + }); + } catch (addError) { + console.log(addError); + // TODO: handle error somehow + } } } }