fix: update chainId check in getConnectorClient #4733
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.
Problem
When retrieving connector client using
getConnectorClient
(not passing a connector as a parameter) for a newly switched chain: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).
In
getConnectorClient
, when connection is retrieved in this manner:connection = config.state.connections.get(config.state.current!)
,ConnectorChainMismatchError
can occur if usingconnection.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 toconnection.chainId
):const chainId = parameters.chainId ?? connection.chainId