From 3f8446b48a4f493448b262959b943756a24382d9 Mon Sep 17 00:00:00 2001 From: Alexandr Kazachenko Date: Fri, 18 Oct 2024 17:03:08 +0500 Subject: [PATCH] fix(widget): ignore selected eip6963 provider when in widget (#5009) --- apps/cowswap-frontend/src/cow-react/index.tsx | 7 ++++++- .../src/web3-react/Web3Provider/hooks/useEagerlyConnect.ts | 6 ++++-- libs/wallet/src/web3-react/Web3Provider/index.tsx | 7 ++++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/apps/cowswap-frontend/src/cow-react/index.tsx b/apps/cowswap-frontend/src/cow-react/index.tsx index c525fe3bc8..bea80941b9 100644 --- a/apps/cowswap-frontend/src/cow-react/index.tsx +++ b/apps/cowswap-frontend/src/cow-react/index.tsx @@ -70,8 +70,13 @@ function Main() { function Web3ProviderInstance({ children }: { children: ReactNode }) { const selectedWallet = useAppSelector((state) => state.user.selectedWallet) + const { standaloneMode } = useInjectedWidgetParams() - return {children} + return ( + + {children} + + ) } function Toasts() { diff --git a/libs/wallet/src/web3-react/Web3Provider/hooks/useEagerlyConnect.ts b/libs/wallet/src/web3-react/Web3Provider/hooks/useEagerlyConnect.ts index a9a86fe0a2..22ac501bef 100644 --- a/libs/wallet/src/web3-react/Web3Provider/hooks/useEagerlyConnect.ts +++ b/libs/wallet/src/web3-react/Web3Provider/hooks/useEagerlyConnect.ts @@ -27,7 +27,7 @@ async function connect(connector: Connector) { } } -export function useEagerlyConnect(selectedWallet: ConnectionType | undefined) { +export function useEagerlyConnect(selectedWallet: ConnectionType | undefined, standaloneMode?: boolean) { const [tryConnectEip6963Provider, setTryConnectEip6963Provider] = useState(false) const eagerlyConnectInitRef = useRef(false) const selectedEip6963ProviderInfo = useSelectedEip6963ProviderInfo() @@ -75,6 +75,8 @@ export function useEagerlyConnect(selectedWallet: ConnectionType | undefined) { * Activate the selected eip6963 provider */ useEffect(() => { + // Ignore remembered eip6963 provider if the app is in widget dapp mode + if (isInjectedWidget() && !standaloneMode) return if (!selectedWallet || !tryConnectEip6963Provider) return const connection = getWeb3ReactConnection(selectedWallet) @@ -90,5 +92,5 @@ export function useEagerlyConnect(selectedWallet: ConnectionType | undefined) { setTryConnectEip6963Provider(false) connect(connector) } - }, [selectedEip6963ProviderInfo, selectedWallet, setEip6963Provider, tryConnectEip6963Provider]) + }, [standaloneMode, selectedEip6963ProviderInfo, selectedWallet, setEip6963Provider, tryConnectEip6963Provider]) } diff --git a/libs/wallet/src/web3-react/Web3Provider/index.tsx b/libs/wallet/src/web3-react/Web3Provider/index.tsx index d25571b454..97cc1dc8df 100644 --- a/libs/wallet/src/web3-react/Web3Provider/index.tsx +++ b/libs/wallet/src/web3-react/Web3Provider/index.tsx @@ -13,10 +13,11 @@ import { Web3ReactConnection } from '../types' interface Web3ProviderProps { children: ReactNode selectedWallet: ConnectionType | undefined + standaloneMode?: boolean } -export function Web3Provider({ children, selectedWallet }: Web3ProviderProps) { - useEagerlyConnect(selectedWallet) +export function Web3Provider({ children, selectedWallet, standaloneMode }: Web3ProviderProps) { + useEagerlyConnect(selectedWallet, standaloneMode) const connections = useOrderedConnections(selectedWallet) const connectors: [Connector, Web3ReactHooks][] = connections @@ -25,7 +26,7 @@ export function Web3Provider({ children, selectedWallet }: Web3ProviderProps) { const key = useMemo( () => connections.map(({ type }: Web3ReactConnection) => getConnectionName(type)).join('-'), - [connections] + [connections], ) return (