Skip to content

Commit

Permalink
Fix WalletConnect initial chain issue
Browse files Browse the repository at this point in the history
  • Loading branch information
tolgahan-arikan committed Dec 13, 2024
1 parent b3d933a commit 1e3695d
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions packages/kit/src/connectors/walletConnect/walletConnect.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createConnector } from 'wagmi'
import { walletConnect as walletConnectbase, WalletConnectParameters } from 'wagmi/connectors'

import { Wallet } from '../../types'
Expand All @@ -8,13 +9,51 @@ export const walletConnect = (options: WalletConnectParameters): Wallet => ({
id: 'wallet-connect',
logoDark: WalletConnectLogo,
logoLight: WalletConnectLogo,
// iconBackground: '#fff',
name: 'Walletconnect',
type: 'wallet',
createConnector: () => {
const connector = walletConnectbase({
...options
const baseConnector = walletConnectbase(options)

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

const connect = async (params?: { chainId?: number }) => {
const targetChainId = params?.chainId ?? 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 1e3695d

Please sign in to comment.