From 54574d48958414a5af097d9b55be2b7fa7d41088 Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Thu, 18 May 2023 09:42:14 +0100 Subject: [PATCH] Fix chainId being out of sync --- .../walletconnect/src/services/RemixClient.ts | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/apps/walletconnect/src/services/RemixClient.ts b/apps/walletconnect/src/services/RemixClient.ts index a2cbc9cca0..df251663da 100644 --- a/apps/walletconnect/src/services/RemixClient.ts +++ b/apps/walletconnect/src/services/RemixClient.ts @@ -11,6 +11,7 @@ export class RemixClient extends PluginClient { wagmiClient ethereumClient: EthereumClient chains: Chain[] + currentChain: number internalEvents: EventManager constructor() { @@ -32,11 +33,6 @@ export class RemixClient extends PluginClient { console.log('initializing walletconnect plugin...') } - async deactivate(){ - console.log('deactivating walletconnect plugin...') - return true - } - async initClient () { try { this.chains = [arbitrum, arbitrumGoerli, mainnet, polygon, polygonMumbai, optimism, optimismGoerli, goerli, sepolia] @@ -57,11 +53,14 @@ export class RemixClient extends PluginClient { this.wagmiClient.subscribe((event) => { if (event.status === 'connected') { this.emit('accountsChanged', [event.data.account]) - this.emit('chainChanged', event.data.chain.id) - console.log('this.wagmiClient: ', this.wagmiClient) + if (this.currentChain !== event.data.chain.id) { + this.currentChain = event.data.chain.id + this.emit('chainChanged', event.data.chain.id) + } } else if (event.status === 'disconnected') { this.emit('accountsChanged', []) this.emit('chainChanged', 0) + this.currentChain = 0 } }) this.on('theme', 'themeChanged', (theme: any) => { @@ -78,11 +77,15 @@ export class RemixClient extends PluginClient { resolve(message) }) } else if (this.wagmiClient.data && this.wagmiClient.data.provider && this.wagmiClient.data.provider.jsonRpcFetchFunc) { - this.wagmiClient.data.provider.jsonRpcFetchFunc(data.method, data.params).then((message) => { - resolve({"jsonrpc": "2.0", "result": message, "id": data.id}) - }).catch((error) => { - reject(error) - }) + if (data.method === 'net_version' || data.method === 'eth_chainId') { + resolve({"jsonrpc": "2.0", "result": this.currentChain, "id": data.id}) + } else { + this.wagmiClient.data.provider.jsonRpcFetchFunc(data.method, data.params).then((message) => { + resolve({"jsonrpc": "2.0", "result": message, "id": data.id}) + }).catch((error) => { + reject(error) + }) + } } else { this.wagmiClient.provider.send(data.method, data.params).then((message) => { resolve({"jsonrpc": "2.0", "result": message, "id": data.id}) @@ -97,8 +100,8 @@ export class RemixClient extends PluginClient { }) } - onDeactivation () { + async deactivate(){ console.log('deactivating walletconnect plugin...') - this.ethereumClient.disconnect() + await this.ethereumClient.disconnect() } } \ No newline at end of file