|
|
@ -1,7 +1,6 @@ |
|
|
|
import {PluginClient} from '@remixproject/plugin' |
|
|
|
import {PluginClient} from '@remixproject/plugin' |
|
|
|
import {createClient} from '@remixproject/plugin-webview' |
|
|
|
import {createClient} from '@remixproject/plugin-webview' |
|
|
|
import {w3mConnectors, w3mProvider} from '@web3modal/ethereum' |
|
|
|
import {defaultWagmiConfig, createWeb3Modal} from '@web3modal/wagmi/react' |
|
|
|
import {createConfig, configureChains} from 'wagmi' |
|
|
|
|
|
|
|
import { |
|
|
|
import { |
|
|
|
arbitrum, |
|
|
|
arbitrum, |
|
|
|
arbitrumGoerli, |
|
|
|
arbitrumGoerli, |
|
|
@ -12,15 +11,18 @@ import { |
|
|
|
optimismGoerli, |
|
|
|
optimismGoerli, |
|
|
|
Chain, |
|
|
|
Chain, |
|
|
|
goerli, |
|
|
|
goerli, |
|
|
|
sepolia |
|
|
|
sepolia, |
|
|
|
|
|
|
|
ronin, |
|
|
|
|
|
|
|
saigon |
|
|
|
} from 'viem/chains' |
|
|
|
} from 'viem/chains' |
|
|
|
import {EthereumClient} from '@web3modal/ethereum' |
|
|
|
|
|
|
|
import EventManager from 'events' |
|
|
|
import EventManager from 'events' |
|
|
|
import {PROJECT_ID} from './constant' |
|
|
|
import {PROJECT_ID as projectId, METADATA as metadata} from './constant' |
|
|
|
|
|
|
|
import { Config, disconnect, getAccount, watchAccount } from '@wagmi/core' |
|
|
|
|
|
|
|
import { EIP1193Provider, RequestArguments } from '../types' |
|
|
|
|
|
|
|
|
|
|
|
export class WalletConnectRemixClient extends PluginClient { |
|
|
|
export class WalletConnectRemixClient extends PluginClient { |
|
|
|
wagmiConfig |
|
|
|
web3modal: ReturnType<typeof createWeb3Modal> |
|
|
|
ethereumClient: EthereumClient |
|
|
|
wagmiConfig: Config |
|
|
|
chains: Chain[] |
|
|
|
chains: Chain[] |
|
|
|
currentChain: number |
|
|
|
currentChain: number |
|
|
|
internalEvents: EventManager |
|
|
|
internalEvents: EventManager |
|
|
@ -45,9 +47,9 @@ export class WalletConnectRemixClient extends PluginClient { |
|
|
|
console.log('initializing walletconnect plugin...') |
|
|
|
console.log('initializing walletconnect plugin...') |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async initClient() { |
|
|
|
initClient() { |
|
|
|
try { |
|
|
|
try { |
|
|
|
this.chains = [ |
|
|
|
const chains = [ |
|
|
|
mainnet, |
|
|
|
mainnet, |
|
|
|
arbitrum, |
|
|
|
arbitrum, |
|
|
|
arbitrumGoerli, |
|
|
|
arbitrumGoerli, |
|
|
@ -56,62 +58,64 @@ export class WalletConnectRemixClient extends PluginClient { |
|
|
|
optimism, |
|
|
|
optimism, |
|
|
|
optimismGoerli, |
|
|
|
optimismGoerli, |
|
|
|
goerli, |
|
|
|
goerli, |
|
|
|
sepolia |
|
|
|
sepolia, |
|
|
|
] |
|
|
|
ronin, |
|
|
|
const {publicClient} = configureChains(this.chains, [ |
|
|
|
saigon |
|
|
|
w3mProvider({projectId: PROJECT_ID}) |
|
|
|
] as [Chain, ...Chain[]] |
|
|
|
], { |
|
|
|
|
|
|
|
pollingInterval: 5000 |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.wagmiConfig = createConfig({ |
|
|
|
const wagmiConfig = defaultWagmiConfig({ |
|
|
|
autoConnect: false, |
|
|
|
chains, |
|
|
|
connectors: w3mConnectors({projectId: PROJECT_ID, chains: this.chains}), |
|
|
|
projectId, |
|
|
|
publicClient |
|
|
|
metadata, |
|
|
|
|
|
|
|
//ssr: true
|
|
|
|
}) |
|
|
|
}) |
|
|
|
this.ethereumClient = new EthereumClient(this.wagmiConfig, this.chains) |
|
|
|
|
|
|
|
|
|
|
|
this.web3modal = createWeb3Modal({ wagmiConfig, projectId, chains }) |
|
|
|
|
|
|
|
this.wagmiConfig = wagmiConfig |
|
|
|
|
|
|
|
this.chains = chains |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
return console.error('Could not get a wallet connection', e) |
|
|
|
return console.error('Could not get a wallet connection', e) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
subscribeToEvents() { |
|
|
|
subscribeToEvents() { |
|
|
|
this.wagmiConfig.subscribe((event) => { |
|
|
|
watchAccount(this.wagmiConfig, { |
|
|
|
if (event.status === 'connected') { |
|
|
|
onChange(account) { |
|
|
|
if (event.data.account !== this.currentAcount) { |
|
|
|
if(account.isConnected){ |
|
|
|
this.currentAcount = event.data.account |
|
|
|
if (account.address !== this.currentAcount) { |
|
|
|
this.emit('accountsChanged', [event.data.account])
|
|
|
|
this.currentAcount = account.address |
|
|
|
} |
|
|
|
this.emit('accountsChanged', [account.address])
|
|
|
|
if (this.currentChain !== event.data.chain.id) { |
|
|
|
} |
|
|
|
this.currentChain = event.data.chain.id |
|
|
|
if (this.currentChain !== account.chainId) { |
|
|
|
this.emit('chainChanged', event.data.chain.id) |
|
|
|
this.currentChain = account.chainId |
|
|
|
|
|
|
|
this.emit('chainChanged', account.chainId) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}else{ |
|
|
|
|
|
|
|
this.emit('accountsChanged', []) |
|
|
|
|
|
|
|
this.currentAcount = '' |
|
|
|
|
|
|
|
this.emit('chainChanged', 0) |
|
|
|
|
|
|
|
this.currentChain = 0 |
|
|
|
} |
|
|
|
} |
|
|
|
} else if (event.status === 'disconnected') { |
|
|
|
}, |
|
|
|
this.emit('accountsChanged', []) |
|
|
|
|
|
|
|
this.currentAcount = '' |
|
|
|
|
|
|
|
this.emit('chainChanged', 0) |
|
|
|
|
|
|
|
this.currentChain = 0 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
this.on('theme', 'themeChanged', (theme: any) => { |
|
|
|
this.on('theme', 'themeChanged', (theme: any) => { |
|
|
|
this.internalEvents.emit('themeChanged', theme.quality) |
|
|
|
this.web3modal.setThemeMode(theme.quality) |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async sendAsync(data: {method: string; params: string; id: string}) { |
|
|
|
async sendAsync(data: RequestArguments) { |
|
|
|
if (this.wagmiConfig.status === 'connected') { |
|
|
|
const account = getAccount(this.wagmiConfig) |
|
|
|
|
|
|
|
if (account.isConnected) { |
|
|
|
if (data.method === 'eth_accounts') { |
|
|
|
if (data.method === 'eth_accounts') { |
|
|
|
return { |
|
|
|
return { |
|
|
|
jsonrpc: '2.0', |
|
|
|
jsonrpc: '2.0', |
|
|
|
result: [this.wagmiConfig.data.account], |
|
|
|
result: [account.address], |
|
|
|
id: data.id |
|
|
|
id: data.id |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
const provider = await this.wagmiConfig.connector.getProvider({ |
|
|
|
const provider = await account.connector.getProvider() as EIP1193Provider |
|
|
|
chainId: this.wagmiConfig.data.chain.id |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (provider.isMetaMask) { |
|
|
|
if (provider) { |
|
|
|
return new Promise((resolve) => { |
|
|
|
return new Promise((resolve) => { |
|
|
|
provider.sendAsync(data, (error, response) => { |
|
|
|
provider.sendAsync(data, (error, response) => { |
|
|
|
if (error) { |
|
|
|
if (error) { |
|
|
@ -156,6 +160,6 @@ export class WalletConnectRemixClient extends PluginClient { |
|
|
|
|
|
|
|
|
|
|
|
async deactivate() { |
|
|
|
async deactivate() { |
|
|
|
console.log('deactivating walletconnect plugin...') |
|
|
|
console.log('deactivating walletconnect plugin...') |
|
|
|
await this.ethereumClient.disconnect() |
|
|
|
await disconnect(this.wagmiConfig) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|