diff --git a/apps/remix-ide/src/app/providers/injected-provider.tsx b/apps/remix-ide/src/app/providers/injected-provider.tsx index 193a6ea3cc..9148daa078 100644 --- a/apps/remix-ide/src/app/providers/injected-provider.tsx +++ b/apps/remix-ide/src/app/providers/injected-provider.tsx @@ -22,14 +22,24 @@ export abstract class InjectedProvider extends Plugin implements IProvider { abstract getInjectedProvider(): any abstract notFound(): string - onActivation(): void { - (window as any).ethereum.on('accountsChanged', this.listenerAccountsChanged); - (window as any).ethereum.on('chainChanged', this.listenerChainChanged); + onActivation(): void { + try { + const web3Provider = this.getInjectedProvider() + web3Provider.on('accountsChanged', this.listenerAccountsChanged); + web3Provider.on('chainChanged', this.listenerChainChanged); + } catch (error) { + console.log('unable to listen on context changed') + } } onDeactivation(): void { - (window as any).ethereum.removeListener('accountsChanged', this.listenerAccountsChanged) - (window as any).ethereum.removeListener('chainChanged', this.listenerChainChanged) + try { + const web3Provider = this.getInjectedProvider() + web3Provider.removeListener('accountsChanged', this.listenerAccountsChanged) + web3Provider.removeListener('chainChanged', this.listenerChainChanged) + } catch (error) { + console.log('unable to remove listener on context changed') + } } askPermission (throwIfNoInjectedProvider) {