Implement sendAsync api with newly discovered functions from wagmi codebas

pull/3674/head
ioedeveloper 2 years ago committed by Aniket
parent 5f1448f2ef
commit a856a88775
  1. 120
      apps/wallet-connect/src/app/app.tsx
  2. 59
      apps/wallet-connect/src/services/RemixClient.ts

@ -1,108 +1,46 @@
import React, { useEffect, useState } from 'react' import React, { useEffect, useState } from 'react'
import EthereumProvider from '@walletconnect/ethereum-provider'
import '../css/app.css' import '../css/app.css'
import '@fortawesome/fontawesome-free/css/all.css' import '@fortawesome/fontawesome-free/css/all.css'
import { RemixClient } from '../services/RemixClient'
import { PROJECT_ID } from '../services/constant' import { PROJECT_ID } from '../services/constant'
import { Web3Modal } from '@web3modal/standalone' import { EthereumClient } from '@web3modal/ethereum'
import { Web3Button, Web3Modal, Web3NetworkSwitch } from '@web3modal/react'
import { WagmiConfig } from 'wagmi'
import { RemixClient } from '../services/RemixClient'
const remix = new RemixClient() const p = new RemixClient()
const web3Modal = new Web3Modal({
projectId: PROJECT_ID,
standaloneChains: ["eip155:1"],
walletConnectVersion: 2
})
function App() { function App() {
const [isConnected, setIsConnected] = useState<boolean>(false) const [ethereumClient, setEthereumClient] = useState<EthereumClient>(null)
const [provider, setProvider] = useState<EthereumProvider>() const [wagmiClient, setWagmiClient] = useState(null)
const [accounts, setAccounts] = useState<string[]>([])
useEffect(() => { useEffect(() => {
if (!provider) { (async () => {
createClient() await p.init()
} const ethereumClient = new EthereumClient(p.wagmiClient, p.chains)
}, [provider])
setWagmiClient(p.wagmiClient)
const createClient = async () => { setEthereumClient(ethereumClient)
try { })()
const provider = await EthereumProvider.init({ }, [])
projectId: PROJECT_ID,
chains: [1],
showQrModal: true
})
provider.modal = web3Modal
setProvider(provider)
} catch (e) {
console.log(e)
}
}
const handleConnect = async () => {
if (!provider) throw Error("Cannot connect. Sign Client is not created")
try {
subscribeToEvents(provider)
await provider.connect()
remix.walletConnectClient = provider
} catch (e) {
console.log(e)
}
}
const handleDisconnect = async () => {
try {
await provider.disconnect()
reset()
} catch (e) {
console.log(e)
}
}
const subscribeToEvents = (provider: EthereumProvider) => {
if (!provider) throw Error("Provider does not exist")
try {
provider.on('connect', () => {
setIsConnected(true)
})
provider.on('chainChanged', (args) => {
remix.emit('chainChanged', args)
})
provider.on('accountsChanged', (args) => {
remix.emit('accountsChanged', args)
})
} catch (e) {
console.log(e)
}
}
const reset = () => {
setAccounts([])
setIsConnected(false)
}
return ( return (
<div className="App"> <div className="App">
<div className="btn-group-vertical mt-5 w-25" role="group"> <div style={{ display: 'inline-block' }}>
{ !isConnected && <button id="connectbtn" type="button" onClick={handleConnect} className="btn btn-primary">Connect to a wallet</button> } { wagmiClient && <WagmiConfig client={wagmiClient}>
{ isConnected && <button id="disconnectbtn" type="button" onClick={handleDisconnect} className="btn btn-primary mt-2">Disconnect</button> } <Web3Button label='Connect to a wallet' />
</div> </WagmiConfig>
<div id='accounts-container'> }
{ accounts.length > 0 && </div>
<div> <div style={{ display: 'inline-block', paddingLeft: 30, marginTop: 5 }}>
<label><b>Accounts: </b></label><br></br> { wagmiClient &&
<ul className="list-group list-group-flush" id="accounts"> <WagmiConfig client={wagmiClient}>
{ accounts.map((account, index) => <li key={index}>{ account }</li>) } <Web3NetworkSwitch />
</ul> </WagmiConfig>
}
</div> </div>
} { ethereumClient && <Web3Modal projectId={PROJECT_ID} ethereumClient={ethereumClient} themeMode={'dark'} /> }
{/* <div><label><b>Network: </b></label><label className="ml-1" id="chain"> - </label></div> */}
</div>
</div> </div>
) )
} }
export default App export default App

@ -1,9 +1,13 @@
import { PluginClient } from '@remixproject/plugin' import { PluginClient } from '@remixproject/plugin'
import { createClient } from '@remixproject/plugin-webview' import { createClient } from '@remixproject/plugin-webview'
import EthereumProvider from '@walletconnect/ethereum-provider' import { w3mConnectors, w3mProvider } from '@web3modal/ethereum'
import { configureChains, createClient as wagmiCreateClient } from 'wagmi'
import { arbitrum, mainnet, polygon, optimism, Chain, goerli, sepolia } from 'wagmi/chains'
import { PROJECT_ID } from './constant'
export class RemixClient extends PluginClient { export class RemixClient extends PluginClient {
#walletConnectClient: EthereumProvider wagmiClient
chains: Chain[]
constructor() { constructor() {
super() super()
@ -12,28 +16,45 @@ export class RemixClient extends PluginClient {
this.onload() this.onload()
} }
init () { async init() {
console.log('initialzing...') try {
this.chains = [arbitrum, mainnet, polygon, optimism, goerli, sepolia]
const { provider } = configureChains(this.chains, [w3mProvider({ projectId: PROJECT_ID })])
this.wagmiClient = wagmiCreateClient({
autoConnect: false,
connectors: w3mConnectors({ projectId: PROJECT_ID, version: 1, chains: this.chains }),
provider
})
} catch (e) {
return console.error("Could not get a wallet connection", e)
}
} }
set walletConnectClient (value: EthereumProvider) { sendAsync = (data: { method: string, params: string, id: string }) => {
this.#walletConnectClient = value
}
sendAsync = (data: { id: string, method: string, params: any[] }) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (this.#walletConnectClient) { if (this.wagmiClient) {
if (data.method === 'eth_chainId') data.method = 'eth_chainId' if (this.wagmiClient.data && this.wagmiClient.data.provider && this.wagmiClient.data.provider.sendAsync) {
this.#walletConnectClient.sendAsync(data, (error, message) => { this.wagmiClient.data.provider.sendAsync(data, (error, message) => {
console.log('method: ', data.method) if (error) return reject(error)
if (error) return reject(error) resolve(message)
console.log('message: ', message) })
resolve({"jsonrpc": "2.0", "result": message, "id": data.id}) } else if (this.wagmiClient.data && this.wagmiClient.data.provider && this.wagmiClient.data.provider.jsonRpcFetchFunc) {
}) this.wagmiClient.data.provider.jsonRpcFetchFunc(data.method, data.params).then((message) => {
resolve({"jsonrpc": "2.0", "result": message, "id": data.id})
}).catch((error) => {
reject(error)
})
} else {
this.wagmiClient.provider.send(data.method, data.params).then((message) => {
resolve({"jsonrpc": "2.0", "result": message, "id": data.id})
}).catch((error) => {
reject(error)
})
}
} else { } else {
console.error('Remix Client is not connected to WalletConnect Client.')
resolve({"jsonrpc": "2.0", "result": [], "id": data.id}) resolve({"jsonrpc": "2.0", "result": [], "id": data.id})
} }
}) })
} }
} }
Loading…
Cancel
Save