Skip to content

fix: update chainId check in getConnectorClient #4733

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
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

effie-ms
Copy link

Problem

When retrieving connector client using getConnectorClient (not passing a connector as a parameter) for a newly switched chain:

import {
  getConnectorClient,
} from 'wagmi/actions'
return getConnectorClient(wagmiConfig, { chainId: chain.id })

There is an occasional race condition between the connection update in the internal state and the retrieval of the connector (the event updating the state: https://github.com/wevm/wagmi/blob/main/packages/core/src/createConfig.ts#L333-L348).

image

In getConnectorClient, when connection is retrieved in this manner: connection = config.state.connections.get(config.state.current!), ConnectorChainMismatchError can occur if using connection.chainId to verify whether the connector and connection have the same chainId (because the connection state may not yet reflect the latest chain switch when the verification occurs)

Proposed solution:

Instead of using connection.chainId for comparison, the suggested approach reuses the following chain id, which ensures alignment with the initially expected chain (when provided as a parameter, or falls back to connection.chainId):

const chainId = parameters.chainId ?? connection.chainId

Copy link

changeset-bot bot commented Jul 16, 2025

🦋 Changeset detected

Latest commit: 1802181

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@wagmi/core Minor
@wagmi/connectors Major
wagmi Patch
@wagmi/vue Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Jul 16, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
wagmi ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 4, 2025 0:29am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant