Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore integrator onSwitchChain errors #293

Merged
merged 2 commits into from
Oct 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
zzmp marked this conversation as resolved.
Show resolved Hide resolved
// 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