Skip to content

Commit

Permalink
fix: fix some bugs about walletconnect v2, resolve #129
Browse files Browse the repository at this point in the history
  • Loading branch information
johnson86tw committed Jul 3, 2023
1 parent cd4df36 commit f2412f2
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/connectors/walletConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand All @@ -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)

Expand Down

0 comments on commit f2412f2

Please sign in to comment.