-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix WalletConnect initial chain issue (#215)
* Fix WalletConnect initial chain issue * Pass defaultChainId to the connector * Fix WalletConnect display name
- Loading branch information
1 parent
b3d933a
commit 4b9013e
Showing
2 changed files
with
54 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 50 additions & 6 deletions
56
packages/kit/src/connectors/walletConnect/walletConnect.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}) |