From 7bf0467fd64ed313e44df408d0911b428a34ad04 Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Tue, 25 Oct 2022 13:54:09 -0700 Subject: [PATCH 1/2] fix: ignore integrator onSwitchChain errors --- src/hooks/useSwitchChain.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/hooks/useSwitchChain.ts b/src/hooks/useSwitchChain.ts index d21b999c2..fe3c83ab1 100644 --- a/src/hooks/useSwitchChain.ts +++ b/src/hooks/useSwitchChain.ts @@ -77,8 +77,12 @@ 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 (switching) return await switching + } catch (error) { + return // ignores integrator-originated errors + } } try { From ac6d732cbb5a1c04bfbd4fe7acb49adc697ea08d Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Wed, 26 Oct 2022 18:20:58 -0700 Subject: [PATCH 2/2] docs: onSwitchChain --- src/hooks/useSwitchChain.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/hooks/useSwitchChain.ts b/src/hooks/useSwitchChain.ts index fe3c83ab1..3c0734cff 100644 --- a/src/hooks/useSwitchChain.ts +++ b/src/hooks/useSwitchChain.ts @@ -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 export const onSwitchChainAtom = atom(undefined) @@ -79,6 +84,7 @@ export default function useSwitchChain(): (chainId: SupportedChainId) => Promise if (connector === connectors.user) { 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