From bfb021f5946cf7e38dc96ac30bd9e9098f63ac44 Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Wed, 12 Oct 2022 13:43:10 -0700 Subject: [PATCH] fix: export AddEthereumChainParameter (#275) * fix: export AddEthereumChainParameter * fix: do not callback on auto switch * fix: missing comma * fix: better logic --- src/hooks/useSwitchChain.ts | 16 ++++++++++++---- src/hooks/useSyncWidgetEventHandlers.ts | 2 +- src/index.tsx | 1 + 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/hooks/useSwitchChain.ts b/src/hooks/useSwitchChain.ts index d63348e14..d21b999c2 100644 --- a/src/hooks/useSwitchChain.ts +++ b/src/hooks/useSwitchChain.ts @@ -8,6 +8,8 @@ import { atom } from 'jotai' import { useAtomValue } from 'jotai/utils' import { useCallback } from 'react' +import useConnectors from './web3/useConnectors' + /** Defined by EIP-3085. */ export interface AddEthereumChainParameter { chainId: string @@ -56,6 +58,7 @@ async function switchChain( export default function useSwitchChain(): (chainId: SupportedChainId) => Promise { const { connector, provider } = useWeb3React() + const connectors = useConnectors() const urlMap = useJsonRpcUrlsMap() const onSwitchChain = useAtomValue(onSwitchChainAtom) return useCallback( @@ -69,9 +72,14 @@ export default function useSwitchChain(): (chainId: SupportedChainId) => Promise rpcUrls: urlMap[chainId], } try { - // If the integrator implements onSwitchChain, use that instead. - const switching = onSwitchChain?.(addChainParameter) - if (switching) return switching + // The network connector does not implement EIP--3326. + if (connector === connectors.network) return await connector.activate(chainId) + + // The user connector may require custom logic: try onSwitchChain if it is available. + if (connector === connectors.user) { + const switching = onSwitchChain?.(addChainParameter) + if (switching) return switching + } try { // A custom Connector may use a customProvider, in which case it should handle its own chain switching. @@ -91,6 +99,6 @@ export default function useSwitchChain(): (chainId: SupportedChainId) => Promise throw new Error(`Failed to switch network: ${error}`) } }, - [connector, onSwitchChain, provider, urlMap] + [connector, connectors.network, connectors.user, onSwitchChain, provider, urlMap] ) } diff --git a/src/hooks/useSyncWidgetEventHandlers.ts b/src/hooks/useSyncWidgetEventHandlers.ts index b4d342eb9..2288b6a02 100644 --- a/src/hooks/useSyncWidgetEventHandlers.ts +++ b/src/hooks/useSyncWidgetEventHandlers.ts @@ -4,7 +4,7 @@ import { useUpdateAtom } from 'jotai/utils' import { useEffect } from 'react' import { OnConnectWalletClick, onConnectWalletClickAtom } from 'state/wallet' export type { OnError } from 'components/Error/ErrorBoundary' -export type { OnSwitchChain } from 'hooks/useSwitchChain' +export type { AddEthereumChainParameter, OnSwitchChain } from 'hooks/useSwitchChain' export type { OnConnectWalletClick } from 'state/wallet' export interface WidgetEventHandlers { diff --git a/src/index.tsx b/src/index.tsx index 0e3015aaa..3785f3e2e 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -19,6 +19,7 @@ export type { FeeOptions } from 'hooks/swap/useSyncConvenienceFee' export type { DefaultAddress, TokenDefaults } from 'hooks/swap/useSyncTokenDefaults' export type { OnTxFail, OnTxSubmit, OnTxSuccess, TransactionEventHandlers } from 'hooks/transactions' export type { + AddEthereumChainParameter, OnConnectWalletClick, OnError, OnSwitchChain,