-
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
Open
o-mid
wants to merge
5
commits into
rainbow-me:main
Choose a base branch
from
o-mid:fix/walletconnect-disconnect-after-refresh
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5ba97bb
test: add walletconnect disconnect-after-refresh regression
o-mid 1236816
fix: clear all connections on wallet disconnect
o-mid 3ce83e7
fix: cancel pending connect when connections are empty
o-mid 44be001
fix: keep clearing connections after a failed disconnect
o-mid 1b81bb6
fix: drop failed connections from wagmi state
o-mid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
250 changes: 250 additions & 0 deletions
250
packages/rainbowkit/src/hooks/useDisconnectAll.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| 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'); | ||
| }); | ||
| }); | ||
|
|
||
| it('clears a connection even when its connector disconnect fails', async () => { | ||
| const config = createWalletConnectConfig(); | ||
| seedRestoredWalletConnectConnections(config); | ||
|
|
||
| const walletConnectConnectors = config.connectors.filter( | ||
| (connector) => connector.id === 'walletConnect', | ||
| ); | ||
| const [failingConnector, ...remainingConnectors] = walletConnectConnectors; | ||
| if (!failingConnector || remainingConnectors.length === 0) { | ||
| throw new Error('Expected multiple WalletConnect connectors'); | ||
| } | ||
|
|
||
| failingConnector.disconnect = vi | ||
| .fn() | ||
| .mockRejectedValue(new Error('teardown failed')); | ||
|
|
||
| render( | ||
| <WagmiProvider config={config} reconnectOnMount={false}> | ||
| <QueryClientProvider client={queryClient}> | ||
| <DisconnectHarness mode="all" /> | ||
| </QueryClientProvider> | ||
| </WagmiProvider>, | ||
| ); | ||
|
|
||
| fireEvent.click(screen.getByTestId('disconnect')); | ||
|
|
||
| await waitFor(() => { | ||
| expect(failingConnector.disconnect).toHaveBeenCalled(); | ||
| for (const connector of remainingConnectors) { | ||
| expect(connector.disconnect).toHaveBeenCalled(); | ||
| } | ||
| expect(screen.getByTestId('connection-count').textContent).toBe('0'); | ||
| expect(screen.getByTestId('connected').textContent).toBe('false'); | ||
| expect(screen.getByTestId('status').textContent).toBe('disconnected'); | ||
| }); | ||
| }); | ||
|
|
||
| it('cancels a pending connect when there are no connections yet', async () => { | ||
| const config = createWalletConnectConfig(); | ||
|
|
||
| config.setState({ | ||
| chainId: mainnet.id, | ||
| connections: new Map(), | ||
| current: null, | ||
| status: 'connecting', | ||
| }); | ||
|
|
||
| render( | ||
| <WagmiProvider config={config} reconnectOnMount={false}> | ||
| <QueryClientProvider client={queryClient}> | ||
| <DisconnectHarness mode="all" /> | ||
| </QueryClientProvider> | ||
| </WagmiProvider>, | ||
| ); | ||
|
|
||
| expect(screen.getByTestId('connection-count').textContent).toBe('0'); | ||
| expect(screen.getByTestId('status').textContent).toBe('connecting'); | ||
|
|
||
| fireEvent.click(screen.getByTestId('disconnect')); | ||
|
|
||
| await waitFor(() => { | ||
| expect(screen.getByTestId('status').textContent).toBe('disconnected'); | ||
| expect(screen.getByTestId('connected').textContent).toBe('false'); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 👍 / 👎.