Skip to content

Commit 1749849

Browse files
authored
fix: Logout immediate walletconnect (#12503)
<!-- Before opening a pull request, please read the [contributing guidelines](https://github.com/pancakeswap/pancake-frontend/blob/develop/CONTRIBUTING.md) first --> <!-- start pr-codex --> --- ## PR-Codex overview This PR focuses on improving the `useSwitchNetworkV2` and `useAuth` hooks by enhancing address handling and refining connection logic. ### Detailed summary - Added `safeGetAddress` utility to ensure checksummed addresses in `useSwitchNetworkV2`. - Updated connection logic in `useAuth` to conditionally set `chainId` based on the connector type. - Removed unused `router` import and variable in `useAuth`. - Simplified dependency arrays in `useEffect` hooks. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent 14d7805 commit 1749849

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

apps/web/src/hooks/useAuth.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
} from '@pancakeswap/ui-wallets'
99
import { ConnectData } from '@pancakeswap/ui-wallets/src/types'
1010
import { usePrivy } from '@privy-io/react-auth'
11-
import { useRouter } from 'next/router'
1211
import { useCallback } from 'react'
1312
import { useAppDispatch } from 'state'
1413
import { CONNECTOR_MAP } from 'utils/wagmi'
@@ -21,12 +20,11 @@ import { useActiveChainId } from './useActiveChainId'
2120

2221
const useAuth = () => {
2322
const dispatch = useAppDispatch()
24-
const { connectAsync, connectors } = useConnect()
23+
const { connectAsync } = useConnect()
2524
const { chain } = useAccount()
2625
const { disconnectAsync } = useDisconnect()
2726
const { chainId } = useActiveChainId()
2827
const { t } = useTranslation()
29-
const router = useRouter()
3028
const { logout: privyLogout, ready, authenticated } = usePrivy()
3129
const { signOutAndClearUserStates } = useFirebaseAuth()
3230

@@ -53,7 +51,10 @@ const useAuth = () => {
5351
try {
5452
if (!connector) return
5553
// eslint-disable-next-line consistent-return
56-
return connectAsync({ connector, chainId })
54+
return connectAsync({
55+
connector,
56+
chainId: connectorId === EvmConnectorNames.WalletConnect ? undefined : chainId,
57+
})
5758
} catch (error) {
5859
if (error instanceof ConnectorNotFoundError) {
5960
throw new WalletConnectorNotFoundError()
@@ -67,7 +68,7 @@ const useAuth = () => {
6768
}
6869
}
6970
},
70-
[connectors, connectAsync, chainId, t, router],
71+
[connectAsync, chainId, t],
7172
)
7273

7374
const logout = useCallback(async () => {
@@ -87,7 +88,7 @@ const useAuth = () => {
8788
window.localStorage.removeItem('wagmi.store')
8889
}
8990
}
90-
}, [disconnectAsync, dispatch, chain?.id, authenticated, ready, signOutAndClearUserStates, privyLogout])
91+
}, [disconnectAsync, dispatch, chain?.id, chainId, authenticated, ready, signOutAndClearUserStates, privyLogout])
9192

9293
return { login, logout }
9394
}

apps/web/src/wallet/hook/useSwitchNetworkV2.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import useAuth from 'hooks/useAuth'
55
import { useAtomValue, useSetAtom } from 'jotai'
66
import { useRouter } from 'next/router'
77
import { useCallback, useEffect, useMemo, useRef } from 'react'
8+
import { safeGetAddress } from 'utils'
89
import { Connector, useAccount, useSwitchChain } from 'wagmi'
910
import { accountActiveChainAtom } from 'wallet/atoms/accountStateAtoms'
1011
import { SwitchChainRequest, switchChainUpdatingAtom } from 'wallet/atoms/switchChainRequestAtom'
@@ -98,12 +99,17 @@ const requireLogout = async (connector: Connector, chainId: number, address: `0x
9899

99100
const provider = (await connector.getProvider()) as any
100101

102+
const checksummedAddress = safeGetAddress(address)
103+
101104
return Boolean(
102-
provider &&
103-
Array.isArray(provider.session?.namespaces?.eip155?.accounts) &&
104-
!provider.session.namespaces.eip155.accounts.some((account: string) =>
105-
account?.includes(`${chainId}:${address}`),
106-
),
105+
!checksummedAddress ||
106+
(provider &&
107+
Array.isArray(provider.session?.namespaces?.eip155?.accounts) &&
108+
!provider.session.namespaces.eip155.accounts.some(
109+
(account: string) =>
110+
account?.includes(`${chainId}:${checksummedAddress}`) ||
111+
account?.includes(`${chainId}:${checksummedAddress.toLowerCase()}`),
112+
)),
107113
)
108114
} catch (error) {
109115
console.error(error, 'Error detecting provider')

0 commit comments

Comments
 (0)