Skip to content

Commit 9446995

Browse files
Merge pull request #2063 from Web3Auth/fix/switch-chain-injected
fix issue of switching custom chains in injected wallet
2 parents bb9120e + 956caea commit 9446995

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

packages/adapters/default-evm-adapter/src/injectedEvmAdapter.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class InjectedEvmAdapter extends BaseEvmAdapter<void> {
7171
async connect(): Promise<IProvider | null> {
7272
super.checkConnectionRequirements();
7373
if (!this.injectedProvider) throw WalletLoginError.connectionError("Injected provider is not available");
74+
if (!this.chainConfig) throw WalletLoginError.connectionError("Chain config is not available");
7475
this.status = ADAPTER_STATUS.CONNECTING;
7576
this.emit(ADAPTER_EVENTS.CONNECTING, { adapter: this.name });
7677
try {
@@ -85,15 +86,10 @@ class InjectedEvmAdapter extends BaseEvmAdapter<void> {
8586
}
8687
}
8788
this.status = ADAPTER_STATUS.CONNECTED;
88-
const chainDisconnectHandler = () => {
89-
this.disconnect();
90-
if (this.injectedProvider.removeListener) this.injectedProvider.removeListener("disconnect", chainDisconnectHandler);
91-
};
92-
this.injectedProvider.on("disconnect", chainDisconnectHandler);
9389
const accountDisconnectHandler = (accounts: string[]) => {
9490
if (accounts.length === 0) {
9591
this.disconnect();
96-
if (this.injectedProvider.removeListener) this.injectedProvider.removeListener("accountsChanged", accountDisconnectHandler);
92+
if (this.injectedProvider?.removeListener) this.injectedProvider.removeListener("accountsChanged", accountDisconnectHandler);
9793
}
9894
};
9995
this.injectedProvider.on("accountsChanged", accountDisconnectHandler);
@@ -114,8 +110,9 @@ class InjectedEvmAdapter extends BaseEvmAdapter<void> {
114110
}
115111

116112
async disconnect(options: { cleanup: boolean } = { cleanup: false }): Promise<void> {
113+
if (!this.injectedProvider) throw WalletLoginError.connectionError("Injected provider is not available");
117114
await super.disconnectSession();
118-
if (typeof this.injectedProvider?.removeAllListeners !== "undefined") this.injectedProvider?.removeAllListeners();
115+
if (typeof this.injectedProvider.removeAllListeners !== "undefined") this.injectedProvider.removeAllListeners();
119116
try {
120117
await this.injectedProvider.request({
121118
method: "wallet_revokePermissions",
@@ -138,8 +135,9 @@ class InjectedEvmAdapter extends BaseEvmAdapter<void> {
138135
}
139136

140137
public async addChain(chainConfig: CustomChainConfig, init = false): Promise<void> {
138+
if (!this.injectedProvider) throw WalletLoginError.connectionError("Injected provider is not available");
141139
super.checkAddChainRequirements(chainConfig, init);
142-
await this.injectedProvider?.request({
140+
await this.injectedProvider.request({
143141
method: "wallet_addEthereumChain",
144142
params: [
145143
{
@@ -160,8 +158,9 @@ class InjectedEvmAdapter extends BaseEvmAdapter<void> {
160158
}
161159

162160
public async switchChain(params: { chainId: string }, init = false): Promise<void> {
161+
if (!this.injectedProvider) throw WalletLoginError.connectionError("Injected provider is not available");
163162
super.checkSwitchChainRequirements(params, init);
164-
await this.injectedProvider?.request({
163+
await this.injectedProvider.request({
165164
method: "wallet_switchEthereumChain",
166165
params: [{ chainId: params.chainId }],
167166
});

0 commit comments

Comments
 (0)