This tutorial is a step-by-step guide on how to integrate a wallet such as Phantom into your dApp using the RainbowKit library. RainbowKit is an EVM wallet library that supports Phantom on Ethereum and Polygon.
We will be going through step by step how to go from 0 to a fully integrated RainbowKit button. If you already have a dApp that you are trying to integrate Phantom in, you can use our project as reference. At the end of this guide, your adapter will look like this:
Prerequisites
Node version >=16.12.0
A text editor/IDE (such as VSCode)
Some Knowledge of React
Creating The App
We recommend using Vite to create new react applications.
To create a new React application using Vite, run the following command in your terminal:
yarncreatevite
This will ask you for a project name. Provide it a name here. For purposes of this tutorial I used "Rainbow-Sandbox".
It will then ask you to select a framework. Select "React" here.
Next it will ask for a variant. Select "Typescript" here.
Now change directory into your project and run:
yarninstall
And make sure your app runs by running the command:
yarndev
Configuring TypeScript
You will need to change the moduleResolution keys to be switched from "bundler" to "node".
Next, we will configure rainbowkit and wagmi like so.
// =============================================================================// Rainbowkit Configuration// =============================================================================// initalize which chains your dapp will use, and set up a providerconst { chains,provider } =configureChains([goerli], [publicProvider()]);constconnectors=connectorsForWallets([ { groupName:'Phantom', wallets: [phantomWallet({ chains })], },]);constwagmiClient=createClient({ connectors, provider,});
Here we configure what chains we will support in our dApp, as well as the provider that wagmi requires to interface with the blockchain.
Then we set up our connectors. This is all of the wallets that you want your dApp to support. You can have as many objects in the array as you would like, each categorized into different groups by the groupName and wallets keys.
To see all of the different ways that you can configure your connection modal, you can go to RainbowKit's documentation.
After that, we will pass the connectors and provider to the createClient function, which we will later use to wrap our app in a Wagmi Client and RainbowKitProvider.
With this bit of prep done, we can now go to our app and add the providers as well as a connection button.
Wrapping Your App With The Providers
In main.tsx wrap your <App />component with the providers. Your file should look like this with all of the configuration and wrapping completed.
import React from'react'import ReactDOM from'react-dom/client'import App from'./App'import'./index.css'import'@rainbow-me/rainbowkit/styles.css';import { connectorsForWallets, RainbowKitProvider, darkTheme } from'@rainbow-me/rainbowkit';import { phantomWallet } from'@rainbow-me/rainbowkit/wallets';import { configureChains, createClient, goerli, WagmiConfig,} from'wagmi';import { publicProvider } from'wagmi/providers/public';// =============================================================================// Rainbowkit Configuration// =============================================================================// initalize which chains your dapp will use, and set up a providerconst { chains,provider } =configureChains([goerli], [publicProvider()]);constconnectors=connectorsForWallets([ { groupName:'Phantom', wallets: [phantomWallet({ chains })], },]);constwagmiClient=createClient({ connectors, provider,});ReactDOM.createRoot(document.getElementById('root') asHTMLElement).render(<React.StrictMode><WagmiConfig client={wagmiClient}><RainbowKitProvider chains={chains} theme={darkTheme()}><App /></RainbowKitProvider></WagmiConfig></React.StrictMode>,)
Now we have everything we need done to add a connect wallet button to the app.
Adding A Connect Button
At the top of your App.tsx file you can import the connect button from RainbowKit.
import reactLogo from'./assets/react.svg'import viteLogo from'/vite.svg'import'./App.css'import { ConnectButton } from'@rainbow-me/rainbowkit';functionApp() {return ( <> <div> <ahref="https://vitejs.dev"target="_blank"> <imgsrc={viteLogo} className="logo"alt="Vite logo" /> </a> <ahref="https://react.dev"target="_blank"> <imgsrc={reactLogo} className="logo react"alt="React logo" /> </a> </div> <h1>Vite + React</h1> <divclassName="card"> <ConnectButton /> <p> Edit <code>src/App.tsx</code> and save to test HMR </p> </div> <pclassName="read-the-docs"> Click on the Vite and React logos to learn more </p> </> )}exportdefault App
Now when you run everything locally it should look like this!
And then once you click the connect button, it will pop open a modal like the picture below
Conclusion
RainbowKit provides quite a lot out of the gate with very little work on your end as a developer.
It also lets you leverage the wagmi library to it's fullest potential, as RainbowKit uses wagmi under the hood to manage all of the wallets. This allows you to use the other wagmi hooks to do things like sign messages, and send transactions. To see what a more advanced app might look like, you can look at the code for our sandbox for RainbowKit. And you can also check out the functionality here for a live demo.