Make Wallet connect outside the Frame (wagmi)
We assume you are familiar with what a Farcaster Frame is. Additionally this guide assumes you are familiar with wagmi.sh. In case you are not learn how to build onchain Frames and do transactions.
We know Frames are web applications that run in Farcaster clients like Warpcast. But what if we want to use the same wallet connection logic on our website?
Many frames are just built for Farcaster clients like Warpcast, but could be lovely mobile first experiences. We can enable that by allowing users to bring their wallet from outside Farcaster on direct page visits too.
Prerequisites
You have built a Farcaster Frame.
If your Frame does not have wallet connection functionality right now, skip over the existing frame migration code in this guide and go straight to the migrated RainbowKit and Farcaster Frame setup.
Enable Wallet Connections beyond the Farcaster Frame
In the Farcaster Frame we use the @farcaster/frame-wagmi-connector
to connect wallets. That isn’t available when accessed outside of Farcaster Frames.
The connector is only use by the Farcaster clients to plug the users wallet right in for us to use. On our webpage, we add it ourselves.
To add the wallet ourselves there are many options of connectors we can use. Wagmi, the underlying library we use, has a lot of options documented here. Since that does not give us a user interface right away, we’ll use a ready made UI for this guide.
1) Install RainbowKit
bun add @rainbow-me/rainbowkit
npm install @rainbow-me/rainbowkit
pnpm install @rainbow-me/rainbowkit
yarn add @rainbow-me/rainbowkit
2) Add the RainbowKit Wallet Connector to your website
Now that we have RainbowKit installed, we need to integrate it with your webpage. This may be a bit different depending on your setup, but here is a general guide. To learn more refer to the RainbowKit Docs as well.
Our Frame will have a WagmiProvider.txx
or similar file setup that is adding the frameConnector. We will adapt this to use Rainbowkit. Enabling us to connect all kinds
of wallets outside of Farcaster Frames to. Turning our Frame into a full onchain app.
Your Frame WagmiProvider setup for the config likely looks like this
In case you don’t have one just create the file. We’ll update the WagmiProvider.tsx
to use RainbowKit instead of the Farcaster Frame connector.
We are adding the Frame Connector a bit later.
We are using the default WagmiConfig provided by the RainbowKit Documentation.
Make sure that your default app/page (could be layout.tsx or index.tsx or app.tsx depending on your Framework) is wrapped in the Provider!
For a remix RootLayout this could look like the following layout.tsx
Now to add connection functionality to our app in the page/app we use the ConnectButton provided from RainbowKit.
The ConnectButton handles the Wallet connection for us outside of the Farcaster Frame. We’ll later see how we can automatically ensure the FrameConnector is used when we are inside a frame using React (which you could adopt to any other Framework).
In a live App this may now look something like our nice localized german wallet connect button example below:
Now the last step is to ensure that in frame we always connect. And when connected we don’t show the wallet connect button again. We only need it when not connected right?
3) Ensure the Frame connector is used when available & Web Wallet Connect Button hidden
To make sure that when we are in the Frame we use the Frame connector and when we are on the website we use the RainbowKit connector we add some logic.
Let’s use Wagmi to tell us if there already is a user connected or not. The useAccount()
hook does that for us. If there is no account we show the ConnectButton.
The variable isConnected
is a boolean that tells us if the user is connected or not. If we conditionally render the ConnectButton based on it, we know it only shows
if there is no wallet connected.
Now we have a webpage that lets people use what also is intended to be inside a Farcaster Frame. What is missing is the Frame wallet connection itself.
Let’s add back the Farcaster Wallet connector. We can do this by manually using the frameConnector
in our page.
Let's adjust the logic calling `sdk.actions.ready()`
Likely your existing logic in react looks like the following to call the Farcaster Frame SDK ready function.
By adding the following effect to the code we ensure that when we are in a Frame we connect the Farcaster Frame connector wallet. It also handles calling ready on the sdk. If we don’t call ready, the frame will be stuck on the loading screen. To get the context, which has information about where the Frame was accessed from, we use the sdk and store it in state.
Bonus: Advanced Implementation of Frame Wallet connect/disconnect and webpage Wallet Connect
You may have noticed that we have no logic to prompt the user for connecting the wallet, well we don’t because we automatically connect in Frame or show the wallet connect button right? but what if we want to have the user take action and delay the need to prompt for the friction of connecting a wallet?
Then we want to know if we are connected at a later stage. Such a component could look like the following code, where we check if we are connected, if we are outside a Frame and tell the user to use the ConnectButton. If we are in a frame, we use the Farcaster Frame native connect/disconnect functionality. This is enabled by Wagmi.
The state and connect flows are explained previously, so we only show the relevant component here.