11import { PluginClient } from '@remixproject/plugin'
22import { createClient } from '@remixproject/plugin-webview'
3- import { w3mConnectors , w3mProvider } from '@web3modal/ethereum'
4- import { createConfig , configureChains } from 'wagmi'
3+ import { defaultWagmiConfig , createWeb3Modal } from '@web3modal/wagmi/react'
54import {
65 arbitrum ,
76 arbitrumGoerli ,
@@ -12,15 +11,18 @@ import {
1211 optimismGoerli ,
1312 Chain ,
1413 goerli ,
15- sepolia
14+ sepolia ,
15+ ronin ,
16+ saigon
1617} from 'viem/chains'
17- import { EthereumClient } from '@web3modal/ethereum'
1818import EventManager from 'events'
19- import { PROJECT_ID } from './constant'
19+ import { PROJECT_ID as projectId , METADATA as metadata } from './constant'
20+ import { Config , disconnect , getAccount , watchAccount } from '@wagmi/core'
21+ import { EIP1193Provider , RequestArguments } from '../types'
2022
2123export class WalletConnectRemixClient extends PluginClient {
22- wagmiConfig
23- ethereumClient : EthereumClient
24+ web3modal : ReturnType < typeof createWeb3Modal >
25+ wagmiConfig : Config
2426 chains : Chain [ ]
2527 currentChain : number
2628 internalEvents : EventManager
@@ -45,9 +47,9 @@ export class WalletConnectRemixClient extends PluginClient {
4547 console . log ( 'initializing walletconnect plugin...' )
4648 }
4749
48- async initClient ( ) {
50+ initClient ( ) {
4951 try {
50- this . chains = [
52+ const chains = [
5153 mainnet ,
5254 arbitrum ,
5355 arbitrumGoerli ,
@@ -56,62 +58,64 @@ export class WalletConnectRemixClient extends PluginClient {
5658 optimism ,
5759 optimismGoerli ,
5860 goerli ,
59- sepolia
60- ]
61- const { publicClient} = configureChains ( this . chains , [
62- w3mProvider ( { projectId : PROJECT_ID } )
63- ] , {
64- pollingInterval : 5000
65- } )
61+ sepolia ,
62+ ronin ,
63+ saigon
64+ ] as [ Chain , ...Chain [ ] ]
6665
67- this . wagmiConfig = createConfig ( {
68- autoConnect : false ,
69- connectors : w3mConnectors ( { projectId : PROJECT_ID , chains : this . chains } ) ,
70- publicClient
66+ const wagmiConfig = defaultWagmiConfig ( {
67+ chains,
68+ projectId,
69+ metadata,
70+ //ssr: true
7171 } )
72- this . ethereumClient = new EthereumClient ( this . wagmiConfig , this . chains )
72+
73+ this . web3modal = createWeb3Modal ( { wagmiConfig, projectId, chains } )
74+ this . wagmiConfig = wagmiConfig
75+ this . chains = chains
7376 } catch ( e ) {
7477 return console . error ( 'Could not get a wallet connection' , e )
7578 }
7679 }
7780
7881 subscribeToEvents ( ) {
79- this . wagmiConfig . subscribe ( ( event ) => {
80- if ( event . status === 'connected' ) {
81- if ( event . data . account !== this . currentAcount ) {
82- this . currentAcount = event . data . account
83- this . emit ( 'accountsChanged' , [ event . data . account ] )
84- }
85- if ( this . currentChain !== event . data . chain . id ) {
86- this . currentChain = event . data . chain . id
87- this . emit ( 'chainChanged' , event . data . chain . id )
82+ watchAccount ( this . wagmiConfig , {
83+ onChange ( account ) {
84+ if ( account . isConnected ) {
85+ if ( account . address !== this . currentAcount ) {
86+ this . currentAcount = account . address
87+ this . emit ( 'accountsChanged' , [ account . address ] )
88+ }
89+ if ( this . currentChain !== account . chainId ) {
90+ this . currentChain = account . chainId
91+ this . emit ( 'chainChanged' , account . chainId )
92+ }
93+ } else {
94+ this . emit ( 'accountsChanged' , [ ] )
95+ this . currentAcount = ''
96+ this . emit ( 'chainChanged' , 0 )
97+ this . currentChain = 0
8898 }
89- } else if ( event . status === 'disconnected' ) {
90- this . emit ( 'accountsChanged' , [ ] )
91- this . currentAcount = ''
92- this . emit ( 'chainChanged' , 0 )
93- this . currentChain = 0
94- }
99+ } ,
95100 } )
96101 this . on ( 'theme' , 'themeChanged' , ( theme : any ) => {
97- this . internalEvents . emit ( 'themeChanged' , theme . quality )
102+ this . web3modal . setThemeMode ( theme . quality )
98103 } )
99104 }
100105
101- async sendAsync ( data : { method : string ; params : string ; id : string } ) {
102- if ( this . wagmiConfig . status === 'connected' ) {
106+ async sendAsync ( data : RequestArguments ) {
107+ const account = getAccount ( this . wagmiConfig )
108+ if ( account . isConnected ) {
103109 if ( data . method === 'eth_accounts' ) {
104110 return {
105111 jsonrpc : '2.0' ,
106- result : [ this . wagmiConfig . data . account ] ,
112+ result : [ account . address ] ,
107113 id : data . id
108114 }
109115 } else {
110- const provider = await this . wagmiConfig . connector . getProvider ( {
111- chainId : this . wagmiConfig . data . chain . id
112- } )
116+ const provider = await account . connector . getProvider ( ) as EIP1193Provider
113117
114- if ( provider . isMetaMask ) {
118+ if ( provider ) {
115119 return new Promise ( ( resolve ) => {
116120 provider . sendAsync ( data , ( error , response ) => {
117121 if ( error ) {
@@ -156,6 +160,6 @@ export class WalletConnectRemixClient extends PluginClient {
156160
157161 async deactivate ( ) {
158162 console . log ( 'deactivating walletconnect plugin...' )
159- await this . ethereumClient . disconnect ( )
163+ await disconnect ( this . wagmiConfig )
160164 }
161165}
0 commit comments