Skip to content

Commit

Permalink
fix: useConnect with chainId error bubbling (#3384)
Browse files Browse the repository at this point in the history
* fix: useConnect with chainId error bubbling

* chore: changeset

* chore: remove log
  • Loading branch information
tmm committed Jan 5, 2024
1 parent a2dd953 commit ee868c3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
8 changes: 8 additions & 0 deletions .changeset/rich-forks-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@wagmi/connectors": patch
"@wagmi/core": patch
"create-wagmi": patch
"wagmi": patch
---

Fixed connectors not bubbling error when connecting with `chainId` and subsequent user rejection.
7 changes: 4 additions & 3 deletions packages/connectors/src/coinbaseWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ export function coinbaseWallet(parameters: CoinbaseWalletParameters) {
// Switch to chain if provided
let currentChainId = await this.getChainId()
if (chainId && currentChainId !== chainId) {
const chain = await this.switchChain!({ chainId }).catch(() => ({
id: currentChainId,
}))
const chain = await this.switchChain!({ chainId }).catch((error) => {
if (error.code === UserRejectedRequestError.code) throw error
return { id: currentChainId }
})
currentChainId = chain?.id ?? currentChainId
}

Expand Down
7 changes: 4 additions & 3 deletions packages/connectors/src/metaMask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ export function metaMask(parameters: MetaMaskParameters = {}) {
// Switch to chain if provided
let currentChainId = await this.getChainId()
if (chainId && currentChainId !== chainId) {
const chain = await this.switchChain?.({ chainId }).catch(() => ({
id: currentChainId,
}))
const chain = await this.switchChain!({ chainId }).catch((error) => {
if (error.code === UserRejectedRequestError.code) throw error
return { id: currentChainId }
})
currentChainId = chain?.id ?? currentChainId
}

Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/connectors/injected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,10 @@ export function injected(parameters: InjectedParameters = {}) {
// Switch to chain if provided
let currentChainId = await this.getChainId()
if (chainId && currentChainId !== chainId) {
const chain = await this.switchChain!({ chainId }).catch(() => ({
id: currentChainId,
}))
const chain = await this.switchChain!({ chainId }).catch((error) => {
if (error.code === UserRejectedRequestError.code) throw error
return { id: currentChainId }
})
currentChainId = chain?.id ?? currentChainId
}

Expand Down
4 changes: 1 addition & 3 deletions playgrounds/vite-react/src/wagmi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { del, get, set } from 'idb-keyval'
import { http, createConfig } from 'wagmi'
import { celo, mainnet, optimism, sepolia } from 'wagmi/chains'
import { coinbaseWallet, metaMask, safe, walletConnect } from 'wagmi/connectors'
import { coinbaseWallet, walletConnect } from 'wagmi/connectors'

// biome-ignore lint/correctness/noUnusedVariables: <explanation>
const indexedDBStorage = {
Expand All @@ -23,8 +23,6 @@ export const config = createConfig({
projectId: import.meta.env.VITE_WC_PROJECT_ID,
}),
coinbaseWallet({ appName: 'Vite React Playground', darkMode: true }),
safe({ debug: true, shimDisconnect: true }),
metaMask(),
],
transports: {
[mainnet.id]: http(
Expand Down

1 comment on commit ee868c3

@vercel
Copy link

@vercel vercel bot commented on ee868c3 Jan 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.