Ethereum Provider
Ethereum Provider for WalletConnect v2
Installation​
- npm
- Yarn
npm install @walletconnect/ethereum-provider @web3modal/standalone
yarn add @walletconnect/ethereum-provider @web3modal/standalone
Initialization​
import { EthereumProvider } from '@walletconnect/ethereum-provider'
const provider = await EthereumProvider.init({
projectId, // REQUIRED your projectId
chains, // REQUIRED chain ids
showQrModal, // REQUIRED set to "true" to use @web3modal/standalone,
methods, // OPTIONAL ethereum methods
events, // OPTIONAL ethereum events
rpcMap, // OPTIONAL rpc urls for each chain
metadata, // OPTIONAL metadata of your app
qrModalOptions // OPTIONAL - `undefined` by default, see https://docs.walletconnect.com/2.0/web3modal/options
})
Display Web3Modal with QR code / Handle connection URI​
// Web3Modal is disabled by default, enable it during init() to display a QR code modal
await provider.connect({
chains, // OPTIONAL chain ids
rpcMap, // OPTIONAL rpc urls
pairingTopic // OPTIONAL pairing topic
})
// or
await provider.enable()
// If you are not using Web3Modal,
// you can subscribe to the `display_uri` event and handle the URI yourself.
provider.on('display_uri', (uri: string) => {
// ... custom logic
})
await provider.connect()
// or
await provider.enable()
Sending Requests​
const result = await provider.request({ method: 'eth_requestAccounts' })
// OR
provider.sendAsync({ method: 'eth_requestAccounts' }, CallBackFunction)
Events​
// chain changed
provider.on('chainChanged', handler)
// accounts changed
provider.on('accountsChanged', handler)
// session established
provider.on('connect', handler)
// session event - chainChanged/accountsChanged/custom events
provider.on('session_event', handler)
// connection uri
provider.on('display_uri', handler)
// session disconnect
provider.on('disconnect', handler)