diff --git a/packages/plugins/EVM/src/state/Connection/providers/WalletConnect.ts b/packages/plugins/EVM/src/state/Connection/providers/WalletConnect.ts index d4368343bedb..9d4662ae35e3 100644 --- a/packages/plugins/EVM/src/state/Connection/providers/WalletConnect.ts +++ b/packages/plugins/EVM/src/state/Connection/providers/WalletConnect.ts @@ -4,7 +4,7 @@ import type { RequestArguments } from 'web3-core' import { defer } from '@dimensiondev/kit' import WalletConnect from '@walletconnect/client' import type { ITxData } from '@walletconnect/types' -import { ChainId, chainResolver, EthereumMethodType, ProviderType } from '@masknet/web3-shared-evm' +import { ChainId, chainResolver, EthereumMethodType, isValidAddress, ProviderType } from '@masknet/web3-shared-evm' import { BaseProvider } from './Base' import type { EVM_Provider } from '../types' import type { Account } from '@masknet/web3-shared-base' @@ -122,15 +122,31 @@ export default class WalletConnectProvider extends BaseProvider implements EVM_P this.connector = this.createConnector() - await this.connector.createSession({ - chainId, - }) - this.connection = { resolve, reject, } + if (this.connector.connected) { + const { chainId, accounts } = this.connector + const account = first(accounts) + if (chainId !== 0 && account && isValidAddress(account)) { + this.connection.resolve({ + chainId, + account, + }) + } else { + await this.connector.killSession() + await this.connector.createSession({ + chainId, + }) + } + } else { + await this.connector.createSession({ + chainId, + }) + } + return deferred.finally(() => { this.connection = null })