From 66453ef5f249e656f63eb345a5f79618013d2968 Mon Sep 17 00:00:00 2001 From: tanguyenvn Date: Sat, 12 Oct 2024 16:20:42 +0700 Subject: [PATCH] refactor code --- .../default-evm-adapter/src/injectedEvmAdapter.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/adapters/default-evm-adapter/src/injectedEvmAdapter.ts b/packages/adapters/default-evm-adapter/src/injectedEvmAdapter.ts index 09a530a5c..1f5c0c98d 100644 --- a/packages/adapters/default-evm-adapter/src/injectedEvmAdapter.ts +++ b/packages/adapters/default-evm-adapter/src/injectedEvmAdapter.ts @@ -70,10 +70,11 @@ class InjectedEvmAdapter extends BaseEvmAdapter { async connect(): Promise { super.checkConnectionRequirements(); + if (!this.injectedProvider) throw WalletLoginError.connectionError("Injected provider is not available"); this.status = ADAPTER_STATUS.CONNECTING; this.emit(ADAPTER_EVENTS.CONNECTING, { adapter: this.name }); try { - await this.injectedProvider?.request({ method: "eth_requestAccounts" }); + await this.injectedProvider.request({ method: "eth_requestAccounts" }); // switch chain if not connected to the right chain if (this.injectedProvider.chainId !== this.chainConfig.chainId) { try { @@ -85,14 +86,14 @@ class InjectedEvmAdapter extends BaseEvmAdapter { } this.status = ADAPTER_STATUS.CONNECTED; const chainDisconnectHandler = () => { - this.disconnect(); - this.injectedProvider?.removeListener("disconnect", chainDisconnectHandler); + super.disconnect(); + if (this.injectedProvider.removeListener) this.injectedProvider.removeListener("disconnect", chainDisconnectHandler); }; this.injectedProvider.on("disconnect", chainDisconnectHandler); const accountDisconnectHandler = (accounts: string[]) => { if (accounts.length === 0) { - this.disconnect(); - this.injectedProvider?.removeListener("accountsChanged", accountDisconnectHandler); + super.disconnect(); + if (this.injectedProvider.removeListener) this.injectedProvider.removeListener("accountsChanged", accountDisconnectHandler); } }; this.injectedProvider.on("accountsChanged", accountDisconnectHandler);