upgrade to Web3Modal v4

pull/5370/head
Glitch 1 year ago
parent deb6d56de4
commit 0ccc17efb0
  1. 21
      apps/walletconnect/src/app/app.tsx
  2. 15
      apps/walletconnect/src/app/walletConnectUI.tsx
  3. 94
      apps/walletconnect/src/services/WalletConnectRemixClient.ts
  4. 9
      apps/walletconnect/src/services/constant.ts
  5. 12
      apps/walletconnect/src/types.ts
  6. 8
      package.json
  7. 2232
      yarn.lock

@ -1,33 +1,16 @@
import React, {useEffect, useState} from 'react'
import '../css/app.css' import '../css/app.css'
import '@fortawesome/fontawesome-free/css/all.css' import '@fortawesome/fontawesome-free/css/all.css'
import type {EthereumClient} from '@web3modal/ethereum'
import {WalletConnectRemixClient} from '../services/WalletConnectRemixClient' import {WalletConnectRemixClient} from '../services/WalletConnectRemixClient'
import {WalletConnectUI} from './walletConnectUI' import {WalletConnectUI} from './walletConnectUI'
const remix = new WalletConnectRemixClient() const remix = new WalletConnectRemixClient()
remix.initClient()
function App() { function App() {
const [ethereumClient, setEthereumClient] = useState<EthereumClient>(null)
const [wagmiConfig, setWagmiConfig] = useState(null)
const [theme, setTheme] = useState<string>('dark')
useEffect(() => {
;(async () => {
await remix.initClient()
remix.internalEvents.on('themeChanged', (theme: string) => {
setTheme(theme)
})
setWagmiConfig(remix.wagmiConfig)
setEthereumClient(remix.ethereumClient)
})()
}, [])
return ( return (
<div className="App"> <div className="App">
<h4 className="mt-1">WalletConnect</h4> <h4 className="mt-1">WalletConnect</h4>
{ethereumClient && wagmiConfig && <WalletConnectUI wagmiConfig={wagmiConfig} ethereumClient={ethereumClient} theme={theme} />} <WalletConnectUI />
</div> </div>
) )
} }

@ -1,16 +1,7 @@
import {Web3Button, Web3Modal} from '@web3modal/react' export function WalletConnectUI() {
import {WagmiConfig} from 'wagmi'
import {PROJECT_ID} from '../services/constant'
export function WalletConnectUI({ethereumClient, wagmiConfig, theme}) {
return ( return (
<div> <div style={{display: 'inline-block'}}>
<div style={{display: 'inline-block'}}> <w3m-button />
<WagmiConfig config={wagmiConfig}>
<Web3Button label="Connect to a wallet" />
</WagmiConfig>
</div>
<Web3Modal projectId={PROJECT_ID} ethereumClient={ethereumClient} themeMode={theme} />
</div> </div>
) )
} }

@ -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)
} }
} }

@ -1,2 +1,9 @@
// @ts-ignore // @ts-ignore
export const PROJECT_ID = WALLET_CONNECT_PROJECT_ID export const PROJECT_ID = "bd4997ce3ede37c95770ba10a3804dad"
export const METADATA = {
name: 'Remix IDE',
description: 'The Native IDE for Web3 Development.',
url: 'https://remix.ethereum.org/',
icons: ['https://remix.ethereum.org/favicon.ico'],
verifyUrl: ''
}

@ -0,0 +1,12 @@
export interface RequestArguments {
readonly method: string
readonly params?: readonly unknown[] | object
readonly id?: string
}
export interface EIP1193Provider {
request: <T>(args: RequestArguments) => Promise<T>
sendAsync: <T>(args: RequestArguments, callback:(error, response)=>void) => Promise<T>
on: (event: string, listener: (event: unknown) => void) => void
removeListener: (event: string, listener: (event: unknown) => void) => void
}

@ -152,8 +152,9 @@
"@remixproject/plugin-ws": "0.3.42", "@remixproject/plugin-ws": "0.3.42",
"@ricarso/react-image-magnifiers": "^1.9.0", "@ricarso/react-image-magnifiers": "^1.9.0",
"@types/nightwatch": "^2.3.1", "@types/nightwatch": "^2.3.1",
"@web3modal/ethereum": "^2.7.1", "@wagmi/connectors": "^4.1.4",
"@web3modal/react": "^2.6.2", "@wagmi/core": "^2.2.1",
"@web3modal/wagmi": "4.0.0-alpha.0",
"@xenova/transformers": "^2.7.0", "@xenova/transformers": "^2.7.0",
"ansi-gray": "^0.1.1", "ansi-gray": "^0.1.1",
"assert": "^2.1.0", "assert": "^2.1.0",
@ -234,8 +235,7 @@
"tree-kill": "^1.2.2", "tree-kill": "^1.2.2",
"ts-loader": "^9.2.6", "ts-loader": "^9.2.6",
"tslib": "^2.3.0", "tslib": "^2.3.0",
"viem": "^1.6.0", "viem": "^2.2.0",
"wagmi": "^1.3.10",
"web3": "^4.1.0", "web3": "^4.1.0",
"winston": "^3.3.3", "winston": "^3.3.3",
"ws": "^7.3.0", "ws": "^7.3.0",

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save