Skip to main content

What We’re Building

A React (Next.js) app that connects Dynamic’s MPC wallets to LI.FI’s cross-chain bridge aggregator, allowing users to:
  • Swap tokens across different blockchain networks
  • Find optimal routes with best rates and fees
  • Execute cross-chain transactions seamlessly
  • Monitor transaction progress across chains
If you want to take a quick look at the final code, check out the GitHub repository.

Building the Application

Project setup

Follow the React Quickstart and use the Custom setup path: Ethereum (EVM) with Wagmi and viem. This recipe’s example uses Next.js—scaffold with create-next-app and apply the same packages and provider wiring as in the quickstart, or mirror the layout in the GitHub repository linked above.
Dashboard: Under Chains & Networks, enable every EVM network your LI.FI routes use (not only Ethereum mainnet). Under Sign-in Methods and Wallets, enable what your flow needs (including Embedded wallets). Under SecurityAllowed Origins, add the origin where the app runs (for example http://localhost:3000).

Install LI.FI Dependencies

Add the LI.FI SDK:
This installs the LI.FI SDK for cross-chain routing and execution capabilities.

Configure Environment

Create a .env.local file with your Dynamic environment ID:
.env.local
Set your Dynamic environment ID and LI.FI API key for the integration.

Get LI.FI API Key with Improved Rate Limits

To get an API key with improved rate limits, you need to create an integration on the LI.FI Partner Portal.

Enable Transaction Simulation (Optional)

  1. Go to your Dynamic dashboard
  2. Navigate to Developer SettingsEmbedded WalletsDynamic
  3. Enable “Show Confirmation UI” and “Transaction Simulation” toggles
This enables transaction previews showing asset transfers, fees, gas costs, and contract addresses before execution.
When enabled, users will see a comprehensive transaction preview like this when swapping tokens:
Note: These features are optional. You can disable the toggles if you prefer a more streamlined experience without transaction previews and confirmation UI.

Create LI.FI Client

Create src/lib/lifi.ts to configure the LI.FI SDK:
src/lib/lifi.ts
Here, we’ve created a loadLiFiChains function that fetches EVM chains from LI.FI. The other function initializeLiFiConfig is used to initialize the LI.FI SDK with wagmi, whose state is managed by Dynamic.

Create LI.FI Provider

Create src/lib/lifi-provider.tsx to wrap the LI.FI configuration:
src/lib/lifi-provider.tsx
Here, we have created a LiFiProvider component that can get the chains from LI.FI and initialize the LI.FI SDK with wagmi. We also call the useSyncWagmiConfig function to sync the wagmi config with the LI.FI SDK.

Configure Providers

Update src/lib/providers.tsx to include the LI.FI configuration:
src/lib/providers.tsx
This sets up Dynamic wallet providers with LI.FI integration and React Query for state management.

Create Multi-Chain Swap Component

Create src/components/MultiChainSwap.tsx for the main swap interface:
src/components/MultiChainSwap.tsx
We’ve created several UI components that you can check out in the GitHub repository. All the functionality is in the MultiChainSwap component. Let’s break it down and see how it works.

How It Works

Data Flow

  1. Chains: Gets available networks from LI.FI when the component loads
  2. Tokens: Fetches tokens for selected chains (updates when chains change)
  3. Routes: Finds swap paths when user selects tokens and enters amount
  4. Execution: Runs the swap using the selected route

Key Parameters You Can Customize

Swap Options (in getRoutes):
Execution Hooks (in executeRoute):
  • slippage: How much price can change before failing
  • maxPriceImpact: Maximum acceptable price impact
  • fee: Your platform fee percentage
We use useDynamicContext() for wallet management, switchNetwork() for chain switching, manage state updates through a single state object, and provide comprehensive error handling throughout the swap flow.

Collecting Fees

We can collect fees from the user by adding a fee to the getRoutes function.
You can set up wallet addresses where you want to collect fees in the portal dashboard. Configure wallets for fees on LI.FI

Run the Application

Start the development server:
This starts the development server at http://localhost:3000.

Configure CORS

Add your local development URL to the CORS origins in your Dynamic dashboard under Developer SettingsCORS Origins.

Conclusion

This integration demonstrates Dynamic’s MPC wallets connecting to LI.FI’s cross-chain bridge aggregator for seamless multi-chain token swaps. The implementation provides comprehensive route management, advanced state handling, and modular architecture for production-ready cross-chain DeFi applications.

Additional Resources

Last modified on April 4, 2026