Wallet Connect
Simplify wallet connections to your dApp using the OP Connect React component library.
Getting Started
1. Install
Install Other Page Connect and its peer dependencies:
-
Wagmi is a React Hooks library for Ethereum, this is the library you will use to interact with the connected wallet.
-
Viem is a TypeScript interface for Ethereum that performs blockchain operations.
-
TanStack Query is an async state manager that handles requests, caching, and more.
-
TypeScript is optional, but highly recommended.
2. API Keys
WalletConnect 2.0
OPConnect utilises WalletConnect’s SDK to help with connecting wallets. If you wish to use WalletConnect 2.0, it requires a walletConnectProjectId
which you can create quickly and easily for free over at WalletConnect Cloud.
Sign In With Other Page (SIWOP)
If you intend to use Sign in With Other Page (SIWOP) to log users into your app in addition to wallet connection, you will need to create a connect app in the Other Page Portal and set the required configuration.
See: Sign in with Other Page.
3. Implementation
It is recommended to wrap your app within a new component that will help you set up OPConnect and its dependencies.
Start by creating a new component called Web3Provider
. Here you will import the required providers and create a config using wagmi’s createConfig method. OPConnect supplies a pre-configured getDefaultConfig
function to simplify the process of creating a config.
Below is a simple example app using getDefaultConfig()
to help you get started:
When using a framework that supports React Server Components, you will need to include the "use client"
directive at the beginning of this file.
Now that you have your Web3Provider
component, you can wrap your app with it:
4. Connected Wallet Info
In a lot of use cases, you will want to access the connected wallet from OPConnect in order to be able to interact with it further. You can do so by using the different hooks, such as useAccount, from wagmi (a OPConnect dependency).
In the previous example above we wrapped our app with a <OPConnectProvider>
top-level. Before utilizing any wagmi hook, make sure the components you build are mounted under this provider.
Below is a simple example component that utilizes the useAccount hook to access connection state and the connected wallet address:
Additional Build Tooling Setup
Some build tools require additional setup to work with ConnectKit.
Next.js
OPConnect uses WalletConnect’s SDK to help with connecting wallets. WalletConnect 2.0 pulls in Node.js dependencies that Next.js does not support by default.
You can mitigate this by adding the following to your next.config.js
file:
Next.js App Router
If using Next.js App Router, or any framework that supports React Server Components, you will need to include the "use client"
directive at the beginning of your Web3Provider
file.
Examples
There are various runnable examples included in this repository in the examples folder:
Running Examples Locally
Clone the OPConnect project and install the necessary dependencies:
and start the code bundlers:
and then simply select the example you’d like to run: