-
Notifications
You must be signed in to change notification settings - Fork 793
fix: clear restored walletconnect connections on disconnect #2688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
5ba97bb
1236816
3ce83e7
44be001
1b81bb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@rainbow-me/rainbowkit': patch | ||
| --- | ||
|
|
||
| Fix WalletConnect disconnect after page refresh by clearing every restored connection in one click. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
| import { fireEvent, render, screen, waitFor } from '@testing-library/react'; | ||
| import React from 'react'; | ||
| import type { Address } from 'viem'; | ||
| import { http } from 'viem'; | ||
| import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
| import { | ||
| createConfig, | ||
| useAccount, | ||
| useConnections, | ||
| useDisconnect, | ||
| WagmiProvider, | ||
| } from 'wagmi'; | ||
| import { mainnet } from 'wagmi/chains'; | ||
| import { connectorsForWallets } from '../wallets/connectorsForWallets'; | ||
| import { | ||
| metaMaskWallet, | ||
| rainbowWallet, | ||
| walletConnectWallet, | ||
| } from '../wallets/walletConnectors'; | ||
| import { useDisconnectAll } from './useDisconnectAll'; | ||
|
|
||
| const exampleProjectId = '21fef48091f12692cad574a6f7753643'; | ||
| const account = '0x1111111111111111111111111111111111111111' as Address; | ||
|
|
||
| function createWalletConnectConfig() { | ||
| return createConfig({ | ||
| chains: [mainnet], | ||
| connectors: connectorsForWallets( | ||
| [ | ||
| { | ||
| groupName: 'Popular', | ||
| wallets: [rainbowWallet, metaMaskWallet, walletConnectWallet], | ||
| }, | ||
| ], | ||
| { | ||
| projectId: exampleProjectId, | ||
| appName: 'rainbowkit.com', | ||
| appUrl: 'https://rainbowkit.com', | ||
| }, | ||
| ), | ||
| transports: { | ||
| [mainnet.id]: http(), | ||
| }, | ||
| ssr: true, | ||
| }); | ||
| } | ||
|
|
||
| function seedRestoredWalletConnectConnections( | ||
| config: ReturnType<typeof createWalletConnectConfig>, | ||
| ) { | ||
| const walletConnectConnectors = config.connectors.filter( | ||
| (connector) => connector.id === 'walletConnect', | ||
| ); | ||
|
|
||
| expect(walletConnectConnectors.length).toBeGreaterThan(1); | ||
|
|
||
| const [currentConnector] = walletConnectConnectors; | ||
| if (!currentConnector) { | ||
| throw new Error('Expected at least one WalletConnect connector'); | ||
| } | ||
|
|
||
| for (const connector of walletConnectConnectors) { | ||
| connector.disconnect = vi.fn().mockResolvedValue(undefined); | ||
| } | ||
|
|
||
| const connections = new Map( | ||
| walletConnectConnectors.map((connector) => [ | ||
| connector.uid, | ||
| { | ||
| accounts: [account] as const, | ||
| chainId: mainnet.id, | ||
| connector, | ||
| }, | ||
| ]), | ||
| ); | ||
|
|
||
| config.setState({ | ||
| chainId: mainnet.id, | ||
| connections, | ||
| current: currentConnector.uid, | ||
| status: 'connected', | ||
| }); | ||
|
|
||
| return walletConnectConnectors.length; | ||
| } | ||
|
|
||
| function DisconnectHarness({ mode }: { mode: 'current' | 'all' }) { | ||
| const { status, isConnected } = useAccount(); | ||
| const connections = useConnections(); | ||
| const { disconnect } = useDisconnect(); | ||
| const disconnectAll = useDisconnectAll(); | ||
|
|
||
| return ( | ||
| <div> | ||
| <div data-testid="status">{status}</div> | ||
| <div data-testid="connected">{String(isConnected)}</div> | ||
| <div data-testid="connection-count">{connections.length}</div> | ||
| <button | ||
| data-testid="disconnect" | ||
| onClick={() => { | ||
| if (mode === 'current') { | ||
| disconnect(); | ||
| return; | ||
| } | ||
|
|
||
| void disconnectAll(); | ||
| }} | ||
| type="button" | ||
| > | ||
| Disconnect | ||
| </button> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| describe('useDisconnectAll', () => { | ||
| const queryClient = new QueryClient({ | ||
| defaultOptions: { | ||
| queries: { | ||
| retry: false, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| queryClient.clear(); | ||
| }); | ||
|
|
||
| it('shows why a single wagmi disconnect leaves restored walletconnect sessions connected', async () => { | ||
| const config = createWalletConnectConfig(); | ||
| const restoredCount = seedRestoredWalletConnectConnections(config); | ||
|
|
||
| render( | ||
| <WagmiProvider config={config} reconnectOnMount={false}> | ||
| <QueryClientProvider client={queryClient}> | ||
| <DisconnectHarness mode="current" /> | ||
| </QueryClientProvider> | ||
| </WagmiProvider>, | ||
| ); | ||
|
|
||
| expect(screen.getByTestId('connection-count').textContent).toBe( | ||
| String(restoredCount), | ||
| ); | ||
| expect(screen.getByTestId('connected').textContent).toBe('true'); | ||
|
|
||
| fireEvent.click(screen.getByTestId('disconnect')); | ||
|
|
||
| await waitFor(() => { | ||
| expect(screen.getByTestId('connection-count').textContent).toBe( | ||
| String(restoredCount - 1), | ||
| ); | ||
| }); | ||
|
|
||
| // This is the #2401 failure mode: one Disconnect click is not enough. | ||
| expect(screen.getByTestId('connected').textContent).toBe('true'); | ||
| expect(screen.getByTestId('status').textContent).toBe('connected'); | ||
| }); | ||
|
|
||
| it('clears every restored walletconnect connection in one click', async () => { | ||
| const config = createWalletConnectConfig(); | ||
| const restoredCount = seedRestoredWalletConnectConnections(config); | ||
|
|
||
| render( | ||
| <WagmiProvider config={config} reconnectOnMount={false}> | ||
| <QueryClientProvider client={queryClient}> | ||
| <DisconnectHarness mode="all" /> | ||
| </QueryClientProvider> | ||
| </WagmiProvider>, | ||
| ); | ||
|
|
||
| expect(screen.getByTestId('connection-count').textContent).toBe( | ||
| String(restoredCount), | ||
| ); | ||
|
|
||
| fireEvent.click(screen.getByTestId('disconnect')); | ||
|
|
||
| await waitFor(() => { | ||
| expect(screen.getByTestId('connection-count').textContent).toBe('0'); | ||
| expect(screen.getByTestId('connected').textContent).toBe('false'); | ||
| expect(screen.getByTestId('status').textContent).toBe('disconnected'); | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { useCallback } from 'react'; | ||
| import { useConfig, useDisconnect } from 'wagmi'; | ||
|
|
||
| // RainbowKit registers multiple WalletConnect connectors. After refresh, | ||
| // reconnect can restore more than one, and wagmi disconnect only clears current. | ||
| export function useDisconnectAll() { | ||
| const config = useConfig(); | ||
| const { disconnectAsync } = useDisconnect(); | ||
|
|
||
| return useCallback(async () => { | ||
| const connections = Array.from(config.state.connections.values()); | ||
|
|
||
| for (const { connector } of connections) { | ||
| await disconnectAsync({ connector }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When there are multiple restored connections and any connector's Useful? React with 👍 / 👎. |
||
| } | ||
| }, [config, disconnectAsync]); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the WalletButton QR flow is still pending and the user closes the connect modal, wagmi has
isConnectingset but has not added anything toconfig.state.connectionsyet;useDisconnectAlltherefore snapshots an empty list and this call becomes a no-op. The previous no-argumentdisconnect()still forced wagmi's status back todisconnected, which is exactly what the comment above this line says the close handler needs, so this can leave the custom WalletButton stuck in its loading/disabled connecting state until the pending connect promise settles. Preserve the old no-connection fallback when cancelling an in-flight connect.Useful? React with 👍 / 👎.