Skip to content

Commit

Permalink
fix: export AddEthereumChainParameter (#275)
Browse files Browse the repository at this point in the history
* fix: export AddEthereumChainParameter

* fix: do not callback on auto switch

* fix: missing comma

* fix: better logic
  • Loading branch information
zzmp authored Oct 12, 2022
1 parent 14c17cc commit bfb021f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/hooks/useSwitchChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,6 +58,7 @@ async function switchChain(

export default function useSwitchChain(): (chainId: SupportedChainId) => Promise<void> {
const { connector, provider } = useWeb3React()
const connectors = useConnectors()
const urlMap = useJsonRpcUrlsMap()
const onSwitchChain = useAtomValue(onSwitchChainAtom)
return useCallback(
Expand All @@ -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.
Expand All @@ -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]
)
}
2 changes: 1 addition & 1 deletion src/hooks/useSyncWidgetEventHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

1 comment on commit bfb021f

@vercel
Copy link

@vercel vercel bot commented on bfb021f Oct 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

widgets – ./

widgets-uniswap.vercel.app
widgets-seven-tau.vercel.app
widgets-git-main-uniswap.vercel.app

Please sign in to comment.