From bf553a2c2d8b7dfaaf2dec2ef2e22b43c13ba9ab Mon Sep 17 00:00:00 2001 From: Ben Kremer Date: Mon, 14 Feb 2022 17:18:52 +0100 Subject: [PATCH] refactor: simplify by using higher-level `provider.disconnect` --- .../src/contexts/ClientContext.tsx | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/dapps/react-dapp-v2-with-ethers/src/contexts/ClientContext.tsx b/dapps/react-dapp-v2-with-ethers/src/contexts/ClientContext.tsx index aab7062..fd94840 100644 --- a/dapps/react-dapp-v2-with-ethers/src/contexts/ClientContext.tsx +++ b/dapps/react-dapp-v2-with-ethers/src/contexts/ClientContext.tsx @@ -51,6 +51,7 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac const [pairings, setPairings] = useState([]); const [session, setSession] = useState(); + const [ethereumProvider, setEthereumProvider] = useState(); const [web3Provider, setWeb3Provider] = useState(); const [isFetchingBalances, setIsFetchingBalances] = useState(false); @@ -70,15 +71,11 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac }; const disconnect = useCallback(async () => { - if (typeof client === "undefined") { - throw new Error("WalletConnect is not initialized"); + if (typeof ethereumProvider === "undefined") { + throw new Error("ethereumProvider is not initialized"); } - const _session = await client.session.get(client.session.topics[0]); - await client.disconnect({ - topic: _session.topic, - reason: ERROR.USER_DISCONNECTED.format(), - }); - }, [client]); + await ethereumProvider.disconnect(); + }, [ethereumProvider]); const _subscribeToClientEvents = useCallback(async (_client: Client) => { if (typeof _client === "undefined") { @@ -153,6 +150,7 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac console.log(ethereumProvider); + setEthereumProvider(ethereumProvider); setWeb3Provider(web3Provider); const _accounts = await ethereumProvider.enable();