After initialization, call provider.connect() to establish a connection. You must define namespaces to specify which chains and methods your application supports.
For EIP-155 (Ethereum) namespaces, you should provide:
methods: An array of supported JSON-RPC methods (e.g., eth_sendTransaction).chains: An array of chain identifiers in the format <namespace>:<chainId> (e.g., eip155:80001).events: An array of supported events (e.g., chainChanged).rpcMap: A mapping of chain IDs to their respective RPC URLs.
Optional connection parameters:
pairingTopic: A specific topic to connect to.skipPairing: A boolean (defaults to false) to skip the pairing process.
import { ethers } from "ethers";
import UniversalProvider from "@walletconnect/universal-provider";
// Initialize the provider
const provider = await UniversalProvider.init({
logger: "info",
relayUrl: "ws://<relay-url>",
projectId: "12345678",
metadata: {
name: "React App",
description: "React App for WalletConnect",
url: "https://walletconnect.com/",
icons: ["https://avatars.githubusercontent.com/u/37784886"],
},
client: undefined, // optional instance of @walletconnect/sign-client
});
// create sub providers for each namespace/chain
await provider.connect({
namespaces: {
eip155: {
methods: [
"eth_sendTransaction",
"eth_signTransaction",
"eth_sign",
"personal_sign",
"eth_signTypedData",
],
chains: ["eip155:80001"],
events: ["chainChanged", "accountsChanged"],
rpcMap: {
80001:
"https://rpc.walletconnect.org?chainId=eip155:80001&projectId=<your walletconnect project id>",
},
},
pairingTopic: "<123...topic>", // optional topic to connect to
skipPairing: false, // optional to skip pairing ( later it can be resumed by invoking .pair())
},
});
// Create Web3 Provider
const web3Provider = new ethers.providers.Web3Provider(provider);