From 6ab101ab29af539d1c56ec27e453bcf2dc350c8d Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Thu, 20 Apr 2023 04:43:49 +0100 Subject: [PATCH] Conditionally select theme and update wallet balance --- apps/remix-ide/src/blockchain/blockchain.tsx | 1 + apps/walletconnect/src/app/app.tsx | 6 +++++- apps/walletconnect/src/app/walletConnectUI.tsx | 4 ++-- apps/walletconnect/src/services/RemixClient.ts | 11 ++++++++++- libs/remix-ui/run-tab/src/lib/actions/events.ts | 11 +++++++++-- 5 files changed, 27 insertions(+), 6 deletions(-) diff --git a/apps/remix-ide/src/blockchain/blockchain.tsx b/apps/remix-ide/src/blockchain/blockchain.tsx index 3c9e40fb08..e6b719f8d1 100644 --- a/apps/remix-ide/src/blockchain/blockchain.tsx +++ b/apps/remix-ide/src/blockchain/blockchain.tsx @@ -125,6 +125,7 @@ export class Blockchain extends Plugin { this.off('injected', 'chainChanged') this.off('injected-trustwallet', 'chainChanged') this.off('walletconnect', 'chainChanged') + this.off('walletconnect', 'accountsChanged') } setupEvents () { diff --git a/apps/walletconnect/src/app/app.tsx b/apps/walletconnect/src/app/app.tsx index 4c93de69a0..a14d0fe96f 100644 --- a/apps/walletconnect/src/app/app.tsx +++ b/apps/walletconnect/src/app/app.tsx @@ -10,10 +10,14 @@ const remix = new RemixClient() function App() { const [ethereumClient, setEthereumClient] = useState(null) const [wagmiClient, setWagmiClient] = useState(null) + const [theme, setTheme] = useState('dark') useEffect(() => { (async () => { await remix.initClient() + remix.internalEvents.on('themeChanged', (theme: string) => { + setTheme(theme) + }) const ethereumClient = new EthereumClient(remix.wagmiClient, remix.chains) setWagmiClient(remix.wagmiClient) @@ -24,7 +28,7 @@ function App() { return (

WalletConnect

- { ethereumClient && wagmiClient && } + { ethereumClient && wagmiClient && }
) } diff --git a/apps/walletconnect/src/app/walletConnectUI.tsx b/apps/walletconnect/src/app/walletConnectUI.tsx index f97e864674..90703947f4 100644 --- a/apps/walletconnect/src/app/walletConnectUI.tsx +++ b/apps/walletconnect/src/app/walletConnectUI.tsx @@ -2,7 +2,7 @@ import { Web3Button, Web3Modal } from "@web3modal/react" import { WagmiConfig } from "wagmi" import { PROJECT_ID } from "../services/constant" -export function WalletConnectUI ({ ethereumClient, wagmiClient }) { +export function WalletConnectUI ({ ethereumClient, wagmiClient, theme }) { return (
@@ -11,7 +11,7 @@ export function WalletConnectUI ({ ethereumClient, wagmiClient }) {
- + ) } \ No newline at end of file diff --git a/apps/walletconnect/src/services/RemixClient.ts b/apps/walletconnect/src/services/RemixClient.ts index 3cab5832a6..a2fbf1785a 100644 --- a/apps/walletconnect/src/services/RemixClient.ts +++ b/apps/walletconnect/src/services/RemixClient.ts @@ -3,21 +3,27 @@ import { createClient } from '@remixproject/plugin-webview' import { w3mConnectors, w3mProvider } from '@web3modal/ethereum' import { configureChains, createClient as wagmiCreateClient } from 'wagmi' import { arbitrum, mainnet, polygon, optimism, Chain, goerli, sepolia } from 'wagmi/chains' +import EventManager from "events" import { PROJECT_ID } from './constant' export class RemixClient extends PluginClient { wagmiClient chains: Chain[] + internalEvents: EventManager constructor() { super() createClient(this) + this.internalEvents = new EventManager() this.methods = ["sendAsync", "init"] this.onload() } onActivation () { this.subscribeToEvents() + this.call('theme', 'currentTheme').then((theme: any) => { + this.internalEvents.emit('themeChanged', theme.quality.toLowerCase()) + }) } init () { @@ -30,7 +36,7 @@ export class RemixClient extends PluginClient { const { provider } = configureChains(this.chains, [w3mProvider({ projectId: PROJECT_ID })]) this.wagmiClient = wagmiCreateClient({ - autoConnect: true, + autoConnect: false, connectors: w3mConnectors({ projectId: PROJECT_ID, version: 1, chains: this.chains }), provider }) @@ -49,6 +55,9 @@ export class RemixClient extends PluginClient { this.emit('chainChanged', 0) } }) + this.on('theme', 'themeChanged', (theme: any) => { + this.internalEvents.emit('themeChanged', theme.quality) + }) } sendAsync = (data: { method: string, params: string, id: string }) => { diff --git a/libs/remix-ui/run-tab/src/lib/actions/events.ts b/libs/remix-ui/run-tab/src/lib/actions/events.ts index 73fc0b849c..732e00146b 100644 --- a/libs/remix-ui/run-tab/src/lib/actions/events.ts +++ b/libs/remix-ui/run-tab/src/lib/actions/events.ts @@ -28,6 +28,7 @@ export const setupEvents = (plugin: RunTab, dispatch: React.Dispatch) => { plugin.blockchain.event.register('contextChanged', (context) => { dispatch(resetProxyDeployments()) if (!context.startsWith('vm')) getNetworkProxyAddresses(plugin, dispatch) + if (context !== 'walletconnect') plugin.call('manager', 'deactivatePlugin', 'walletconnect') setFinalContext(plugin, dispatch) fillAccountsList(plugin, dispatch) updateAccountBalances(plugin, dispatch) @@ -135,9 +136,15 @@ export const setupEvents = (plugin: RunTab, dispatch: React.Dispatch) => { dispatch(fetchAccountsListSuccess(accountsMap)) }) - plugin.on('walletconnect', 'accountsChanged', (accounts: Array) => { + plugin.on('walletconnect', 'accountsChanged', async (accounts: Array) => { const accountsMap = {} - accounts.map(account => { accountsMap[account] = shortenAddress(account, '0')}) + + await Promise.all(accounts.map(async (account) => { + const balance = await plugin.blockchain.getBalanceInEther(account) + const updated = shortenAddress(account, balance) + + accountsMap[account] = updated + })) dispatch(fetchAccountsListSuccess(accountsMap)) })