From 4b9013e4530534c43a0a0820939596df0c410eec Mon Sep 17 00:00:00 2001 From: tolgahan-arikan Date: Fri, 13 Dec 2024 18:04:20 +0300 Subject: [PATCH] Fix WalletConnect initial chain issue (#215) * Fix WalletConnect initial chain issue * Pass defaultChainId to the connector * Fix WalletConnect display name --- packages/kit/src/config/defaultConnectors.ts | 6 +- .../connectors/walletConnect/walletConnect.ts | 56 +++++++++++++++++-- 2 files changed, 54 insertions(+), 8 deletions(-) diff --git a/packages/kit/src/config/defaultConnectors.ts b/packages/kit/src/config/defaultConnectors.ts index 40eba28f..f54e7738 100644 --- a/packages/kit/src/config/defaultConnectors.ts +++ b/packages/kit/src/config/defaultConnectors.ts @@ -177,7 +177,8 @@ export const getDefaultWaasConnectors = (options: DefaultWaasConnectorOptions): wallets.push( walletConnect({ - projectId + projectId, + defaultNetwork: defaultChainId }) ) } @@ -283,7 +284,8 @@ export const getDefaultUniversalConnectors = (options: DefaultUniversalConnector wallets.push( walletConnect({ - projectId + projectId, + defaultNetwork: defaultChainId }) ) } diff --git a/packages/kit/src/connectors/walletConnect/walletConnect.ts b/packages/kit/src/connectors/walletConnect/walletConnect.ts index 99b5d778..764b7901 100644 --- a/packages/kit/src/connectors/walletConnect/walletConnect.ts +++ b/packages/kit/src/connectors/walletConnect/walletConnect.ts @@ -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 } })