Skip to content

Commit

Permalink
fix: switch
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Apr 25, 2024
1 parent 51c1d52 commit 12df904
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions packages/core/src/connectors/injected.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
type AddEthereumChainParameter,
type Address,
type EIP1193EventMap,
type EIP1193Provider,
type ProviderConnectInfo,
ProviderRpcError,
Expand Down Expand Up @@ -383,21 +382,10 @@ export function injected(parameters: InjectedParameters = {}) {
if (!chain) throw new SwitchChainError(new ChainNotConfiguredError())

try {
await Promise.all([
provider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: numberToHex(chainId) }],
}),
new Promise<void>((resolve) => {
const listener: EIP1193EventMap['chainChanged'] = (data) => {
if (Number(data) === chainId) {
provider.removeListener('chainChanged', listener)
resolve()
}
}
provider.on('chainChanged', listener)
}),
])
await provider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: numberToHex(chainId) }],
})

This comment has been minimized.

Copy link
@holic

holic Apr 25, 2024

Contributor

just so this isn't lost in chat, copying over my messages:

without the two promises, there's a flash where the chain switch promise returns but chain hasn't switched yet, then the event triggers and wagmi gets the new chain
with the two promises, its seamless, you get the result of the chain switch when the chain has actually switched

see #3859 for my workaround for this

return chain
} catch (err) {
const error = err as RpcError
Expand Down

0 comments on commit 12df904

Please sign in to comment.