Skip to content

Commit

Permalink
fix: ignore integrator onSwitchChain errors (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzmp authored Oct 27, 2022
1 parent be77a8d commit 3eddc33
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/hooks/useSwitchChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export interface AddEthereumChainParameter {
rpcUrls: string[]
}

/**
* An integration hook called when the user tries to switch chains.
* If the hook returns a Promise, it is assumed the integrator is attempting to switch the chain, and no further attempts will be made.
* If that Promise rejects, the error will be ignored so as not to crash the widget.
*/
export type OnSwitchChain = (addChainParameter: AddEthereumChainParameter) => void | Promise<void>
export const onSwitchChainAtom = atom<OnSwitchChain | undefined>(undefined)

Expand Down Expand Up @@ -77,8 +82,13 @@ export default function useSwitchChain(): (chainId: SupportedChainId) => Promise

// 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 {
const switching = onSwitchChain?.(addChainParameter)
// If onSwitchChain returns a Promise, the integrator is responsible for any chain switching. Await and return.
if (switching) return await switching
} catch (error) {
return // ignores integrator-originated errors
}
}

try {
Expand Down

1 comment on commit 3eddc33

@vercel
Copy link

@vercel vercel bot commented on 3eddc33 Oct 27, 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-git-main-uniswap.vercel.app
widgets-seven-tau.vercel.app

Please sign in to comment.