Skip to content

Commit

Permalink
Fix WalletConnect initial chain issue (#215)
Browse files Browse the repository at this point in the history
* Fix WalletConnect initial chain issue

* Pass defaultChainId to the connector

* Fix WalletConnect display name
  • Loading branch information
tolgahan-arikan authored Dec 13, 2024
1 parent b3d933a commit 4b9013e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
6 changes: 4 additions & 2 deletions packages/kit/src/config/defaultConnectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ export const getDefaultWaasConnectors = (options: DefaultWaasConnectorOptions):

wallets.push(
walletConnect({
projectId
projectId,
defaultNetwork: defaultChainId
})
)
}
Expand Down Expand Up @@ -283,7 +284,8 @@ export const getDefaultUniversalConnectors = (options: DefaultUniversalConnector

wallets.push(
walletConnect({
projectId
projectId,
defaultNetwork: defaultChainId
})
)
}
Expand Down
56 changes: 50 additions & 6 deletions packages/kit/src/connectors/walletConnect/walletConnect.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,64 @@
import { createConnector } from 'wagmi'
import { walletConnect as walletConnectbase, WalletConnectParameters } from 'wagmi/connectors'

import { Wallet } from '../../types'

import { WalletConnectLogo } from './WalletConnectLogo'

export const walletConnect = (options: WalletConnectParameters): Wallet => ({
interface WalletConnectOptions extends WalletConnectParameters {
defaultNetwork?: number
}

export const walletConnect = (options: WalletConnectOptions): Wallet => ({
id: 'wallet-connect',
logoDark: WalletConnectLogo,
logoLight: WalletConnectLogo,
// iconBackground: '#fff',
name: 'Walletconnect',
name: 'WalletConnect',
type: 'wallet',
createConnector: () => {
const connector = walletConnectbase({
...options
const { defaultNetwork, ...walletConnectOptions } = options
const baseConnector = walletConnectbase(walletConnectOptions)

return createConnector(config => {
const connector = baseConnector(config)

const connect = async (params?: { chainId?: number }) => {
const targetChainId = params?.chainId ?? defaultNetwork ?? config.chains[0]?.id
if (!targetChainId) {
throw new Error('No target chain ID available')
}

if (!connector.connect || !connector.switchChain) {
throw new Error('WalletConnect connector not properly initialized')
}

// First establish the basic connection
const result = await connector.connect()

// Only attempt to switch chains if we're not already on the target chain
if (result.chainId !== targetChainId) {
try {
// Switch to the target chain
await connector.switchChain({ chainId: targetChainId })

// Return the connection with the updated chain
return {
accounts: result.accounts,
chainId: targetChainId
}
} catch (error) {
console.warn('Failed to switch chain:', error)
return result
}
}

return result
}

return {
...connector,
connect
}
})
return connector
}
})

0 comments on commit 4b9013e

Please sign in to comment.