From 4d0bc6a59bfbc0dfe5ff8f57c5e8ef07f83bedbb Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Wed, 17 May 2023 13:20:09 +0100 Subject: [PATCH 1/2] Disconnect session onDeactivation --- apps/walletconnect/src/app/app.tsx | 5 ++--- apps/walletconnect/src/app/walletConnectUI.tsx | 2 +- apps/walletconnect/src/services/RemixClient.ts | 11 ++++++++++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/apps/walletconnect/src/app/app.tsx b/apps/walletconnect/src/app/app.tsx index a14d0fe96f..55bf606b58 100644 --- a/apps/walletconnect/src/app/app.tsx +++ b/apps/walletconnect/src/app/app.tsx @@ -1,7 +1,7 @@ import React, { useEffect, useState } from 'react' import '../css/app.css' import '@fortawesome/fontawesome-free/css/all.css' -import { EthereumClient } from '@web3modal/ethereum' +import type { EthereumClient } from '@web3modal/ethereum' import { RemixClient } from '../services/RemixClient' import { WalletConnectUI } from './walletConnectUI' @@ -18,10 +18,9 @@ function App() { remix.internalEvents.on('themeChanged', (theme: string) => { setTheme(theme) }) - const ethereumClient = new EthereumClient(remix.wagmiClient, remix.chains) setWagmiClient(remix.wagmiClient) - setEthereumClient(ethereumClient) + setEthereumClient(remix.ethereumClient) })() }, []) diff --git a/apps/walletconnect/src/app/walletConnectUI.tsx b/apps/walletconnect/src/app/walletConnectUI.tsx index 90703947f4..a120de4941 100644 --- a/apps/walletconnect/src/app/walletConnectUI.tsx +++ b/apps/walletconnect/src/app/walletConnectUI.tsx @@ -14,4 +14,4 @@ export function WalletConnectUI ({ ethereumClient, wagmiClient, theme }) { ) -} \ No newline at end of file +} diff --git a/apps/walletconnect/src/services/RemixClient.ts b/apps/walletconnect/src/services/RemixClient.ts index 9e88d5e6c2..a2cbc9cca0 100644 --- a/apps/walletconnect/src/services/RemixClient.ts +++ b/apps/walletconnect/src/services/RemixClient.ts @@ -3,11 +3,13 @@ import { createClient } from '@remixproject/plugin-webview' import { w3mConnectors, w3mProvider } from '@web3modal/ethereum' import { configureChains, createClient as wagmiCreateClient } from 'wagmi' import { arbitrum, arbitrumGoerli, mainnet, polygon, polygonMumbai, optimism, optimismGoerli, Chain, goerli, sepolia } from 'wagmi/chains' +import { EthereumClient } from '@web3modal/ethereum' import EventManager from "events" import { PROJECT_ID } from './constant' export class RemixClient extends PluginClient { wagmiClient + ethereumClient: EthereumClient chains: Chain[] internalEvents: EventManager @@ -45,6 +47,7 @@ export class RemixClient extends PluginClient { connectors: w3mConnectors({ projectId: PROJECT_ID, version: 1, chains: this.chains }), provider }) + this.ethereumClient = new EthereumClient(this.wagmiClient, this.chains) } catch (e) { return console.error("Could not get a wallet connection", e) } @@ -55,6 +58,7 @@ export class RemixClient extends PluginClient { if (event.status === 'connected') { this.emit('accountsChanged', [event.data.account]) this.emit('chainChanged', event.data.chain.id) + console.log('this.wagmiClient: ', this.wagmiClient) } else if (event.status === 'disconnected') { this.emit('accountsChanged', []) this.emit('chainChanged', 0) @@ -65,7 +69,7 @@ export class RemixClient extends PluginClient { }) } - sendAsync = (data: { method: string, params: string, id: string }) => { + sendAsync (data: { method: string, params: string, id: string }) { return new Promise((resolve, reject) => { if (this.wagmiClient) { if (this.wagmiClient.data && this.wagmiClient.data.provider && this.wagmiClient.data.provider.sendAsync) { @@ -92,4 +96,9 @@ export class RemixClient extends PluginClient { } }) } + + onDeactivation () { + console.log('deactivating walletconnect plugin...') + this.ethereumClient.disconnect() + } } \ No newline at end of file From 54574d48958414a5af097d9b55be2b7fa7d41088 Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Thu, 18 May 2023 09:42:14 +0100 Subject: [PATCH 2/2] 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