Skip to content

Commit

Permalink
fix: injected connector throwing error after switching to a chain tha…
Browse files Browse the repository at this point in the history
…t was just added via `'wallet_addEthereumChain'`.
  • Loading branch information
chybisov authored Oct 2, 2024
1 parent 924cc93 commit 7d2d1cb
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions packages/core/src/connectors/injected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,16 +478,32 @@ export function injected(parameters: InjectedParameters = {}) {
rpcUrls,
} satisfies AddEthereumChainParameter

await provider.request({
method: 'wallet_addEthereumChain',
params: [addEthereumChain],
})

const currentChainId = await this.getChainId()
if (currentChainId !== chainId)
throw new UserRejectedRequestError(
new Error('User rejected switch after adding network.'),
)
await Promise.all([
provider
.request({
method: 'wallet_addEthereumChain',
params: [addEthereumChain],
})
.then(async () => {
const currentChainId = await this.getChainId()
if (currentChainId === chainId) {
config.emitter.emit('change', { chainId })
} else {
throw new UserRejectedRequestError(
new Error('User rejected switch after adding network.'),
)
}
}),
new Promise<void>((resolve) => {
const listener = ((data) => {
if ('chainId' in data && data.chainId === chainId) {
config.emitter.off('change', listener)
resolve()
}
}) satisfies Parameters<typeof config.emitter.on>[1]
config.emitter.on('change', listener)
}),
])

return chain
} catch (error) {
Expand Down

0 comments on commit 7d2d1cb

Please sign in to comment.