Conditionally select theme and update wallet balance

pull/3674/head
ioedeveloper 2 years ago committed by Aniket
parent c184795d9f
commit b12118fb18
  1. 1
      apps/remix-ide/src/blockchain/blockchain.tsx
  2. 6
      apps/walletconnect/src/app/app.tsx
  3. 4
      apps/walletconnect/src/app/walletConnectUI.tsx
  4. 11
      apps/walletconnect/src/services/RemixClient.ts
  5. 11
      libs/remix-ui/run-tab/src/lib/actions/events.ts

@ -125,6 +125,7 @@ export class Blockchain extends Plugin {
this.off('injected', 'chainChanged') this.off('injected', 'chainChanged')
this.off('injected-trustwallet', 'chainChanged') this.off('injected-trustwallet', 'chainChanged')
this.off('walletconnect', 'chainChanged') this.off('walletconnect', 'chainChanged')
this.off('walletconnect', 'accountsChanged')
} }
setupEvents () { setupEvents () {

@ -10,10 +10,14 @@ const remix = new RemixClient()
function App() { function App() {
const [ethereumClient, setEthereumClient] = useState<EthereumClient>(null) const [ethereumClient, setEthereumClient] = useState<EthereumClient>(null)
const [wagmiClient, setWagmiClient] = useState(null) const [wagmiClient, setWagmiClient] = useState(null)
const [theme, setTheme] = useState<string>('dark')
useEffect(() => { useEffect(() => {
(async () => { (async () => {
await remix.initClient() await remix.initClient()
remix.internalEvents.on('themeChanged', (theme: string) => {
setTheme(theme)
})
const ethereumClient = new EthereumClient(remix.wagmiClient, remix.chains) const ethereumClient = new EthereumClient(remix.wagmiClient, remix.chains)
setWagmiClient(remix.wagmiClient) setWagmiClient(remix.wagmiClient)
@ -24,7 +28,7 @@ function App() {
return ( return (
<div className="App"> <div className="App">
<h4 className='mt-1'>WalletConnect</h4> <h4 className='mt-1'>WalletConnect</h4>
{ ethereumClient && wagmiClient && <WalletConnectUI wagmiClient={wagmiClient} ethereumClient={ethereumClient} /> } { ethereumClient && wagmiClient && <WalletConnectUI wagmiClient={wagmiClient} ethereumClient={ethereumClient} theme={theme} /> }
</div> </div>
) )
} }

@ -2,7 +2,7 @@ import { Web3Button, Web3Modal } from "@web3modal/react"
import { WagmiConfig } from "wagmi" import { WagmiConfig } from "wagmi"
import { PROJECT_ID } from "../services/constant" import { PROJECT_ID } from "../services/constant"
export function WalletConnectUI ({ ethereumClient, wagmiClient }) { export function WalletConnectUI ({ ethereumClient, wagmiClient, theme }) {
return ( return (
<div> <div>
@ -11,7 +11,7 @@ export function WalletConnectUI ({ ethereumClient, wagmiClient }) {
<Web3Button label='Connect to a wallet' /> <Web3Button label='Connect to a wallet' />
</WagmiConfig> </WagmiConfig>
</div> </div>
<Web3Modal projectId={PROJECT_ID} ethereumClient={ethereumClient} themeMode={'dark'} /> <Web3Modal projectId={PROJECT_ID} ethereumClient={ethereumClient} themeMode={theme} />
</div> </div>
) )
} }

@ -3,21 +3,27 @@ import { createClient } from '@remixproject/plugin-webview'
import { w3mConnectors, w3mProvider } from '@web3modal/ethereum' import { w3mConnectors, w3mProvider } from '@web3modal/ethereum'
import { configureChains, createClient as wagmiCreateClient } from 'wagmi' import { configureChains, createClient as wagmiCreateClient } from 'wagmi'
import { arbitrum, mainnet, polygon, optimism, Chain, goerli, sepolia } from 'wagmi/chains' import { arbitrum, mainnet, polygon, optimism, Chain, goerli, sepolia } from 'wagmi/chains'
import EventManager from "events"
import { PROJECT_ID } from './constant' import { PROJECT_ID } from './constant'
export class RemixClient extends PluginClient { export class RemixClient extends PluginClient {
wagmiClient wagmiClient
chains: Chain[] chains: Chain[]
internalEvents: EventManager
constructor() { constructor() {
super() super()
createClient(this) createClient(this)
this.internalEvents = new EventManager()
this.methods = ["sendAsync", "init"] this.methods = ["sendAsync", "init"]
this.onload() this.onload()
} }
onActivation () { onActivation () {
this.subscribeToEvents() this.subscribeToEvents()
this.call('theme', 'currentTheme').then((theme: any) => {
this.internalEvents.emit('themeChanged', theme.quality.toLowerCase())
})
} }
init () { init () {
@ -30,7 +36,7 @@ export class RemixClient extends PluginClient {
const { provider } = configureChains(this.chains, [w3mProvider({ projectId: PROJECT_ID })]) const { provider } = configureChains(this.chains, [w3mProvider({ projectId: PROJECT_ID })])
this.wagmiClient = wagmiCreateClient({ this.wagmiClient = wagmiCreateClient({
autoConnect: true, autoConnect: false,
connectors: w3mConnectors({ projectId: PROJECT_ID, version: 1, chains: this.chains }), connectors: w3mConnectors({ projectId: PROJECT_ID, version: 1, chains: this.chains }),
provider provider
}) })
@ -49,6 +55,9 @@ export class RemixClient extends PluginClient {
this.emit('chainChanged', 0) this.emit('chainChanged', 0)
} }
}) })
this.on('theme', 'themeChanged', (theme: any) => {
this.internalEvents.emit('themeChanged', theme.quality)
})
} }
sendAsync = (data: { method: string, params: string, id: string }) => { sendAsync = (data: { method: string, params: string, id: string }) => {

@ -28,6 +28,7 @@ export const setupEvents = (plugin: RunTab, dispatch: React.Dispatch<any>) => {
plugin.blockchain.event.register('contextChanged', (context) => { plugin.blockchain.event.register('contextChanged', (context) => {
dispatch(resetProxyDeployments()) dispatch(resetProxyDeployments())
if (!context.startsWith('vm')) getNetworkProxyAddresses(plugin, dispatch) if (!context.startsWith('vm')) getNetworkProxyAddresses(plugin, dispatch)
if (context !== 'walletconnect') plugin.call('manager', 'deactivatePlugin', 'walletconnect')
setFinalContext(plugin, dispatch) setFinalContext(plugin, dispatch)
fillAccountsList(plugin, dispatch) fillAccountsList(plugin, dispatch)
updateAccountBalances(plugin, dispatch) updateAccountBalances(plugin, dispatch)
@ -135,9 +136,15 @@ export const setupEvents = (plugin: RunTab, dispatch: React.Dispatch<any>) => {
dispatch(fetchAccountsListSuccess(accountsMap)) dispatch(fetchAccountsListSuccess(accountsMap))
}) })
plugin.on('walletconnect', 'accountsChanged', (accounts: Array<string>) => { plugin.on('walletconnect', 'accountsChanged', async (accounts: Array<string>) => {
const accountsMap = {} 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)) dispatch(fetchAccountsListSuccess(accountsMap))
}) })

Loading…
Cancel
Save