@ -2,98 +2,42 @@ import { PluginClient } from '@remixproject/plugin'
import { createClient } from '@remixproject/plugin-webview'
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 , Chain } 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 {
internalEvents : EventManager
wagmiClient
wagmiClient
chains : Chain [ ]
chains : Chain [ ]
constructor ( ) {
constructor ( ) {
super ( )
super ( )
createClient ( this )
createClient ( this )
this . methods = [ "sendAsync" ]
this . methods = [ "sendAsync" , "init" ]
this . internalEvents = new EventManager ( )
this . onload ( )
this . onload ( )
}
}
/ * *
async init() {
* Connect wallet button pressed .
* /
async onConnect() {
try {
try {
this . chains = [ arbitrum , mainnet , polygon ]
this . chains = [ arbitrum , mainnet , polygon , optimism , goerli , sepolia ]
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 : tru e,
autoConnect : fals e,
connectors : w3mConnectors ( { projectId : PROJECT_ID , version : 1 , chains : this.chains } ) ,
connectors : w3mConnectors ( { projectId : PROJECT_ID , version : 1 , chains : this.chains } ) ,
provider
provider
} )
} )
this . internalEvents . emit ( 'accountsChanged' , this . wagmiClient . provider . accounts || [ ] )
this . internalEvents . emit ( 'chainChanged' , await this . detectNetwork ( this . wagmiClient . provider . chainId ) )
// Subscribe to accounts change
this . wagmiClient . provider . on ( "accountsChanged" , ( accounts ) = > {
this . internalEvents . emit ( 'accountsChanged' , accounts || [ ] )
} )
// Subscribe to chainId change
this . wagmiClient . provider . on ( "chainChanged" , async ( chainId ) = > {
this . internalEvents . emit ( 'chainChanged' , await this . detectNetwork ( chainId ) )
} )
// Subscribe to networkId change
this . wagmiClient . provider . on ( "networkChanged" , ( networkId ) = > {
this . internalEvents . emit ( 'networkChanged' , networkId )
} )
// Subscribe to networkId change
this . wagmiClient . provider . on ( "disconnect" , ( ) = > {
this . internalEvents . emit ( 'disconnect' )
} )
} catch ( e ) {
} catch ( e ) {
return console . error ( "Could not get a wallet connection" , e )
return console . error ( "Could not get a wallet connection" , e )
}
}
}
}
async detectNetwork ( id ) {
let networkName = null
id = parseInt ( id )
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md
if ( id === 1 ) networkName = "Main"
else if ( id === 2 ) networkName = "Morden (deprecated)"
else if ( id === 3 ) networkName = "Ropsten"
else if ( id === 4 ) networkName = "Rinkeby"
else if ( id === 5 ) networkName = "Goerli"
else if ( id === 42 ) networkName = "Kovan"
else networkName = "Custom"
return networkName
}
/ * *
* Disconnect wallet button pressed .
* /
async onDisconnect() {
// TODO: Which providers have close method?
if ( this . wagmiClient . provider && this . wagmiClient . provider . close ) {
await this . wagmiClient . provider . close ( )
this . wagmiClient . provider = null
} else {
this . internalEvents . emit ( 'disconnect' )
}
}
sendAsync = ( data ) = > {
sendAsync = ( data ) = > {
return new Promise ( ( resolve , reject ) = > {
return new Promise ( ( resolve , reject ) = > {
if ( this . wagmiClient . provider ) {
if ( this . wagmiClient . provider ) {
this . wagmiClient . provider . sendAsync ( data , ( error , message ) = > {
this . wagmiClient . provider . send ( data . method , data . params ) . then ( ( message ) = > {
if ( error ) return reject ( error )
resolve ( { "jsonrpc" : "2.0" , "result" : message , "id" : data . id } )
resolve ( message )
} ) . catch ( ( error ) = > {
reject ( error )
} )
} )
} else {
} else {
resolve ( { "jsonrpc" : "2.0" , "result" : [ ] , "id" : data . id } )
resolve ( { "jsonrpc" : "2.0" , "result" : [ ] , "id" : data . id } )