From b54dc99ddbf9b8c6ce4c052ac5abf5e2e2fafd41 Mon Sep 17 00:00:00 2001 From: Vignesh Mohankumar Date: Thu, 14 Mar 2024 15:41:40 -0400 Subject: [PATCH] isPrivyWalletConnection --- src/constants/wallets.ts | 7 +++++++ src/hooks/useAccounts.tsx | 17 +++-------------- src/hooks/useMatchingEvmNetwork.ts | 7 ++----- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/src/constants/wallets.ts b/src/constants/wallets.ts index 9fa91f457..ed1ba0556 100644 --- a/src/constants/wallets.ts +++ b/src/constants/wallets.ts @@ -57,6 +57,7 @@ export enum WalletErrorType { type WalletConnectionTypeConfig = { name: string; wagmiConnectorId?: string; + isPrivy?: boolean; }; export const walletConnectionTypes: Record = { @@ -80,9 +81,11 @@ export const walletConnectionTypes: Record { + return walletConnectionType && walletConnectionTypes[walletConnectionType]?.isPrivy; +}; + // dYdX Chain wallets export const COSMOS_DERIVATION_PATH = "m/44'/118'/0'/0/0"; diff --git a/src/hooks/useAccounts.tsx b/src/hooks/useAccounts.tsx index a99dcf43e..c8f2dc504 100644 --- a/src/hooks/useAccounts.tsx +++ b/src/hooks/useAccounts.tsx @@ -19,6 +19,7 @@ import { WalletType, WalletConnectionType, getSignTypedData, + isPrivyWalletConnection, } from '@/constants/wallets'; import { setOnboardingState, setOnboardingGuard } from '@/state/account'; @@ -88,12 +89,7 @@ const useAccountsContext = () => { // Create a wallet once the user has authenticated via Privy. Embedded wallets are not created on initial login. useEffect(() => { (async function () { - if ( - !evmAddress && - walletConnectionType && - [WalletConnectionType.OAuth, WalletConnectionType.Email].includes(walletConnectionType) && - authenticated - ) { + if (!evmAddress && isPrivyWalletConnection(walletConnectionType) && authenticated) { await createWallet(); } })(); @@ -260,14 +256,7 @@ const useAccountsContext = () => { const evmDerivedAccount = evmDerivedAddresses[evmAddress]; - if ( - walletConnectionType && - [WalletConnectionType.OAuth, WalletConnectionType.Email].includes( - walletConnectionType - ) && - authenticated && - ready - ) { + if (isPrivyWalletConnection(walletConnectionType) && authenticated && ready) { try { const signature = await signTypedDataAsync(); diff --git a/src/hooks/useMatchingEvmNetwork.ts b/src/hooks/useMatchingEvmNetwork.ts index ac7778b42..a086d629b 100644 --- a/src/hooks/useMatchingEvmNetwork.ts +++ b/src/hooks/useMatchingEvmNetwork.ts @@ -3,7 +3,7 @@ import { useCallback, useEffect, useMemo } from 'react'; import { useSwitchNetwork as useSwitchNetworkPrivy } from '@privy-io/wagmi-connector'; import { useNetwork, useSwitchNetwork } from 'wagmi'; -import { WalletConnectionType } from '@/constants/wallets'; +import { isPrivyWalletConnection } from '@/constants/wallets'; import { useWalletConnection } from './useWalletConnection'; @@ -30,10 +30,7 @@ export const useMatchingEvmNetwork = ({ const matchNetwork = useCallback(async () => { if (!isMatchingNetwork) { - if ( - walletConnectionType && - [WalletConnectionType.Email, WalletConnectionType.OAuth].includes(walletConnectionType) - ) { + if (isPrivyWalletConnection(walletConnectionType)) { await switchNetworkAsyncPrivy?.(Number(chainId)); } else { await switchNetworkAsync?.(Number(chainId));