From f2412f237725f985873d5964061bfd0755fb23a3 Mon Sep 17 00:00:00 2001 From: Johnson Date: Mon, 3 Jul 2023 20:34:46 +0800 Subject: [PATCH] fix: fix some bugs about walletconnect v2, resolve #129 --- src/connectors/walletConnect.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/connectors/walletConnect.ts b/src/connectors/walletConnect.ts index 12b23f1..d5f25ec 100644 --- a/src/connectors/walletConnect.ts +++ b/src/connectors/walletConnect.ts @@ -7,9 +7,15 @@ import { SwitchChainNotSupportedError, UserRejectedRequestError, } from './errors' -import { EthereumProviderOptions } from '@walletconnect/ethereum-provider/dist/types/EthereumProvider' - -export class WalletConnectConnector extends Connector { +import { + EthereumProviderOptions, + EthereumProvider, +} from '@walletconnect/ethereum-provider/dist/types/EthereumProvider' + +export class WalletConnectConnector extends Connector< + EthereumProvider, + EthereumProviderOptions +> { readonly name = 'walletConnect' #provider?: any #onDisconnectHandler?: (code: number, reason: string) => void @@ -85,8 +91,10 @@ export class WalletConnectConnector extends Connector { this.#removeListener('chainChanged', this.#onChainChangedHandler) } this.#onChainChangedHandler = handler - this.#provider.on('chainChanged', (chainId: number) => { - if (this.options?.rpc && this.options.rpc[chainId]) { + this.#provider.on('chainChanged', (hexChainId: string) => { + const chainId = parseInt(hexChainId, 16) + + if (this.options.rpcMap && this.options.rpcMap[chainId]) { handler(chainId) } else { // TODO: what's the best way to handle this? @@ -108,7 +116,9 @@ export class WalletConnectConnector extends Connector { */ async switchChain(chainId: number) { if (!this.#provider) throw new ProviderNotFoundError() - if (!this.options?.rpc?.[chainId]) throw new SwitchChainNotSupportedError() + if (!this.options.rpcMap?.[chainId]) { + throw new SwitchChainNotSupportedError() + } const id = hexValue(chainId)