You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
3.4 KiB
90 lines
3.4 KiB
2 years ago
|
import { PluginClient } from '@remixproject/plugin'
|
||
|
import { createClient } from '@remixproject/plugin-webview'
|
||
2 years ago
|
import { w3mConnectors, w3mProvider } from '@web3modal/ethereum'
|
||
2 years ago
|
import { createConfig, configureChains } from 'wagmi'
|
||
|
import { arbitrum, arbitrumGoerli, mainnet, polygon, polygonMumbai, optimism, optimismGoerli, Chain, goerli, sepolia } from 'viem/chains'
|
||
2 years ago
|
import { EthereumClient } from '@web3modal/ethereum'
|
||
2 years ago
|
import EventManager from "events"
|
||
2 years ago
|
import { PROJECT_ID } from './constant'
|
||
2 years ago
|
|
||
2 years ago
|
export class WalletConnectRemixClient extends PluginClient {
|
||
|
wagmiConfig
|
||
2 years ago
|
ethereumClient: EthereumClient
|
||
2 years ago
|
chains: Chain[]
|
||
2 years ago
|
currentChain: number
|
||
2 years ago
|
internalEvents: EventManager
|
||
2 years ago
|
|
||
|
constructor() {
|
||
|
super()
|
||
|
createClient(this)
|
||
2 years ago
|
this.internalEvents = new EventManager()
|
||
2 years ago
|
this.methods = ["sendAsync", "init", "deactivate"]
|
||
2 years ago
|
this.onload()
|
||
|
}
|
||
|
|
||
2 years ago
|
onActivation () {
|
||
|
this.subscribeToEvents()
|
||
2 years ago
|
this.call('theme', 'currentTheme').then((theme: any) => {
|
||
|
this.internalEvents.emit('themeChanged', theme.quality.toLowerCase())
|
||
|
})
|
||
2 years ago
|
}
|
||
|
|
||
|
init () {
|
||
|
console.log('initializing walletconnect plugin...')
|
||
|
}
|
||
|
|
||
|
async initClient () {
|
||
2 years ago
|
try {
|
||
2 years ago
|
this.chains = [arbitrum, arbitrumGoerli, mainnet, polygon, polygonMumbai, optimism, optimismGoerli, goerli, sepolia]
|
||
2 years ago
|
const { publicClient } = configureChains(this.chains, [w3mProvider({ projectId: PROJECT_ID })])
|
||
|
|
||
|
this.wagmiConfig = createConfig({
|
||
|
autoConnect: false,
|
||
|
connectors: w3mConnectors({ projectId: PROJECT_ID, chains: this.chains }),
|
||
|
publicClient
|
||
2 years ago
|
})
|
||
2 years ago
|
this.ethereumClient = new EthereumClient(this.wagmiConfig, this.chains)
|
||
2 years ago
|
} catch (e) {
|
||
|
return console.error("Could not get a wallet connection", e)
|
||
|
}
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
subscribeToEvents () {
|
||
2 years ago
|
this.wagmiConfig.subscribe((event) => {
|
||
2 years ago
|
if (event.status === 'connected') {
|
||
|
this.emit('accountsChanged', [event.data.account])
|
||
2 years ago
|
if (this.currentChain !== event.data.chain.id) {
|
||
|
this.currentChain = event.data.chain.id
|
||
|
this.emit('chainChanged', event.data.chain.id)
|
||
|
}
|
||
2 years ago
|
} else if (event.status === 'disconnected') {
|
||
|
this.emit('accountsChanged', [])
|
||
|
this.emit('chainChanged', 0)
|
||
2 years ago
|
this.currentChain = 0
|
||
2 years ago
|
}
|
||
|
})
|
||
2 years ago
|
this.on('theme', 'themeChanged', (theme: any) => {
|
||
|
this.internalEvents.emit('themeChanged', theme.quality)
|
||
|
})
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
sendAsync (data: { method: string, params: string, id: string }) {
|
||
2 years ago
|
return new Promise((resolve, reject) => {
|
||
2 years ago
|
if (this.wagmiConfig) {
|
||
|
this.wagmiConfig.publicClient.request(data).then((message) => {
|
||
|
resolve({"jsonrpc": "2.0", "result": message, "id": data.id})
|
||
|
}).catch((error) => {
|
||
|
reject(error)
|
||
|
})
|
||
2 years ago
|
} else {
|
||
2 years ago
|
console.error(`Cannot make ${data.method} request. Remix client is not connect to walletconnect client`)
|
||
2 years ago
|
resolve({"jsonrpc": "2.0", "result": [], "id": data.id})
|
||
|
}
|
||
|
})
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
async deactivate(){
|
||
2 years ago
|
console.log('deactivating walletconnect plugin...')
|
||
2 years ago
|
await this.ethereumClient.disconnect()
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|