You can use the Test Wallet to test your integration at test.walletconnect.org (Source code). Keep in mind that this is not a secure wallet - Do not store funds.
yarn add web3 @walletconnect/web3-provider
npm install --save web3 @walletconnect/web3-provider
Syntax shown below is Javascript ES6 which requires bundling and transpiling to run in web browsers. If unfamiliar we recommend setting up an environment using Webpack Starter or Create React App​
First, instantiate your WalletConnect web3-provider using the following options: Infura or Custom RPC mapping
import WalletConnectProvider from "@walletconnect/web3-provider";​// Create WalletConnect Providerconst provider = new WalletConnectProvider({infuraId: "27e484dcd9e3efcfd25a83a78777cdf1",});​// Enable session (triggers QR Code modal)await provider.enable();
import WalletConnectProvider from "@walletconnect/web3-provider";​// Create WalletConnect Providerconst provider = new WalletConnectProvider({rpc: {1: "https://mainnet.mycustomnode.com",3: "https://ropsten.mycustomnode.com",100: "https://dai.poa.network",// ...},});​// Enable session (triggers QR Code modal)await provider.enable();
Then you can integrate your dapp using your favorite Ethereum library: ethers.js or web3.js
import { providers } from "ethers";​// Wrap with Web3Provider from ethers.jsconst web3Provider = new providers.Web3Provider(provider);
import Web3 from "web3";​// Create Web3 instanceconst web3 = new Web3(provider);
After setting up your provider you should listen to EIP-1193 events to detect accounts and chain change and also disconnection.
// Subscribe to accounts changeprovider.on("accountsChanged", (accounts: string[]) => {console.log(accounts);});​// Subscribe to chainId changeprovider.on("chainChanged", (chainId: number) => {console.log(chainId);});​// Subscribe to session disconnectionprovider.on("disconnect", (code: number, reason: string) => {console.log(code, reason);});
interface RequestArguments {method: string;params?: unknown[] | object;}​// Send JSON RPC requestsconst result = await provider.request(payload: RequestArguments);​// Close provider sessionawait provider.disconnect()
// Get Accountsconst accounts = await web3.eth.getAccounts();​// Get Chain Idconst chainId = await web3.eth.chainId();​// Get Network Idconst networkId = await web3.eth.net.getId();​// Send Transactionconst txHash = await web3.eth.sendTransaction(tx);​// Sign Transactionconst signedTx = await web3.eth.signTransaction(tx);​// Sign Messageconst signedMessage = await web3.eth.sign(msg);​// Sign Typed Dataconst signedTypedData = await web3.eth.signTypedData(msg);
In order to resolve non-signing requests you need to provide one of the following:
The infuraId will support the following chainId's: Mainnet (1), Ropsten (3), Rinkeby(4), Goerli (5) and Kovan (42)
const provider = new WalletConnectProvider({infuraId: "27e484dcd9e3efcfd25a83a78777cdf1",});
The RPC URL mapping should be indexed by chainId and it requires at least one value.
const provider = new WalletConnectProvider({rpc: {1: "https://mainnet.mycustomnode.com",3: "https://ropsten.mycustomnode.com",100: "https://dai.poa.network",// ...},});
You can also customize the connector through the provider using the following options
Use your own hosted bridge by providing the url
const provider = new WalletConnectProvider({infuraId: "27e484dcd9e3efcfd25a83a78777cdf1",bridge: "https://bridge.myhostedserver.com",});
Use your own custom qrcode modal and disable the built-in one
const provider = new WalletConnectProvider({infuraId: "27e484dcd9e3efcfd25a83a78777cdf1",qrcode: false,});​provider.connector.on("display_uri", (err, payload) => {const uri = payload.params[0];CustomQRCodeModal.display(uri);});
If you would like to reduce the number of mobile linking options or customize its order, you can provide an array of wallet names
const provider = new WalletConnectProvider({infuraId: "27e484dcd9e3efcfd25a83a78777cdf1",qrcodeModalOptions: {mobileLinks: ["rainbow","metamask","argent","trust","imtoken","pillar",],},});