Add Flow Cadence to Your wagmi App
This tutorial demonstrates how to enhance your existing wagmi/RainbowKit application with Flow Cadence capabilities. By integrating the Flow Client Library (FCL) with your EVM stack, you can unlock powerful features like batch transactions with a single signature.
Video Overview
Objectives
After completing this guide, you'll be able to:
- Add FCL to your existing wagmi/RainbowKit application
- Configure FCL to work alongside your EVM wallet connections
- Implement batch transactions that execute multiple EVM calls in a single Cadence transaction
- Display both Cadence and EVM addresses in your application
Prerequisites
Next.js and Modern Frontend Development
This tutorial uses Next.js. You don't need to be an expert, but it's helpful to be comfortable with development using a current React framework. You'll be on your own to select and use a package manager, manage Node versions, and other frontend environment tasks. If you don't have your own preference, you can just follow along with us and use npm.
Solidity and Cadence Smart Contract Development
Apps using the hybrid approach can interact with both Cadence and Solidity smart contracts. You don't need to be an expert in either of these, but it's helpful to be familiar with how smart contracts work in at least one of these languages.
Onchain App Frontends
We're assuming you're familiar with wagmi, viem, and RainbowKit. If you're coming from the Cadence, you might want to take a quick look at the getting started guides for these platforms. They're all excellent and will rapidly get you up to speed on how the EVM world commonly connects their apps to their contracts.
Create an App
Start by creating an app using RainbowKit's scaffold:
Install Required Dependencies
Continue by adding the necessary Flow dependencies to your project:
These packages provide:
@onflow/fcl
: The Flow Client Library for interacting with the Cadence VM@onflow/fcl-rainbowkit-adapter
: An adapter that allows RainbowKit to work with FCL-compatible wallets
Step 2: Configure FCL in Your wagmi Setup
Update your wagmi configuration (src/wagmi.ts
) to include FCL:
Step 3: Add the Batch Transaction Utility
Create a custom hook in src/hooks/useBatchTransactions.ts
to handle batch transactions. This utility allows you to execute multiple EVM transactions in a single Cadence transaction:
Step 4: Implement the UI
Now, update your application's page.tsx
to use the batch transaction utility. Update
Step 5: Test Your Application
-
Start your development server:
-
Connect your wallet using the RainbowKit
ConnectButton
- Make sure to use a Cadence-compatible wallet like Flow Wallet
-
Click the "Send Batch Transaction" button
- You'll be prompted to approve the Cadence transaction
- This transaction will execute multiple EVM calls in a single atomic operation
-
Observe the results
- The Cadence transaction ID will be displayed
- The results of each EVM transaction will be shown
How It Works
When you call sendBatchTransaction
, the following happens:
- A Cadence transaction is created that includes all your EVM calls
- The transaction is executed using FCL's
mutate
function - The Cadence transaction calls each EVM transaction in sequence
- If any transaction fails and
mustPass
is true, the entire batch is rolled back - The results of each EVM transaction are returned
This approach gives you several advantages:
- Atomic Operations: All transactions succeed or fail together
- Single Signature: Users only need to sign one transaction
- Gas Efficiency: Reduced gas costs compared to separate transactions
- Simplified UX: Users don't need to approve multiple transactions
Conclusion
You've successfully integrated Flow Cadence with your wagmi/rainbowkit application! This integration allows you to leverage the power of Cadence while maintaining the familiar EVM development experience.
Reference Implementation
For a complete reference implementation, check out the FCL + RainbowKit + wagmi Integration Demo repository.