From 50de54f54db133b5a06ae41f3f9d610127925843 Mon Sep 17 00:00:00 2001 From: Rinat Date: Fri, 7 Jul 2023 13:45:44 +0700 Subject: [PATCH] fix: remove useAutoConnect --- packages/nextjs/hooks/scaffold-eth/index.ts | 1 - .../hooks/scaffold-eth/useAutoConnect.ts | 73 ------------------- 2 files changed, 74 deletions(-) delete mode 100644 packages/nextjs/hooks/scaffold-eth/useAutoConnect.ts diff --git a/packages/nextjs/hooks/scaffold-eth/index.ts b/packages/nextjs/hooks/scaffold-eth/index.ts index 8f1972705..73184bf56 100644 --- a/packages/nextjs/hooks/scaffold-eth/index.ts +++ b/packages/nextjs/hooks/scaffold-eth/index.ts @@ -1,6 +1,5 @@ export * from "./useAccountBalance"; export * from "./useAnimationConfig"; -export * from "./useAutoConnect"; export * from "./useBurnerWallet"; export * from "./useDeployedContractInfo"; export * from "./useNativeCurrencyPrice"; diff --git a/packages/nextjs/hooks/scaffold-eth/useAutoConnect.ts b/packages/nextjs/hooks/scaffold-eth/useAutoConnect.ts deleted file mode 100644 index 3d1755bbb..000000000 --- a/packages/nextjs/hooks/scaffold-eth/useAutoConnect.ts +++ /dev/null @@ -1,73 +0,0 @@ -import { useEffect } from "react"; -import { useEffectOnce, useLocalStorage } from "usehooks-ts"; -import { Connector, useAccount, useConnect } from "wagmi"; -import { hardhat } from "wagmi/chains"; -import scaffoldConfig from "~~/scaffold.config"; -import { burnerWalletId, defaultBurnerChainId } from "~~/services/web3/wagmi-burner/BurnerConnector"; -import { getTargetNetwork } from "~~/utils/scaffold-eth"; - -const walletIdStorageKey = "scaffoldEth2.wallet"; - -/** - * This function will get the initial wallet connector (if any), the app will connect to - * @param previousWalletId - * @param connectors - * @returns - */ -const getInitialConnector = ( - previousWalletId: string, - connectors: Connector[], -): { connector: Connector | undefined; chainId?: number } | undefined => { - const burnerConfig = scaffoldConfig.burnerWallet; - const targetNetwork = getTargetNetwork(); - - const allowBurner = burnerConfig.enabled && (burnerConfig.onlyLocal ? targetNetwork.id === hardhat.id : true); - - if (!previousWalletId) { - // The user was not connected to a wallet - if (allowBurner && scaffoldConfig.walletAutoConnect) { - const connector = connectors.find(f => f.id === burnerWalletId); - return { connector, chainId: defaultBurnerChainId }; - } - } else { - // the user was connected to wallet - if (scaffoldConfig.walletAutoConnect) { - if (previousWalletId === burnerWalletId && !allowBurner) { - return; - } - - const connector = connectors.find(f => f.id === previousWalletId); - return { connector }; - } - } - - return undefined; -}; - -/** - * Automatically connect to a wallet/connector based on config and prior wallet - */ -export const useAutoConnect = (): void => { - const [walletId, setWalletId] = useLocalStorage(walletIdStorageKey, ""); - const connectState = useConnect(); - const accountState = useAccount(); - - useEffect(() => { - if (accountState.isConnected) { - // user is connected, set walletName - setWalletId(accountState.connector?.id ?? ""); - } else { - // user has disconnected, reset walletName - setWalletId(""); - } - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [accountState.isConnected, accountState.connector?.name]); - - useEffectOnce(() => { - const initialConnector = getInitialConnector(walletId, connectState.connectors); - - if (initialConnector?.connector) { - connectState.connect({ connector: initialConnector.connector, chainId: initialConnector.chainId }); - } - }); -};