Skip to main content

What We’re Building

A Next.js app that connects Dynamic’s JavaScript SDK embedded 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 JavaScript Quickstart and use the Custom setup path: choose EVM. 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 Security → Allowed Origins, add the origin where the app runs (for example http://localhost:3000).

Install Dependencies

Install the Dynamic JavaScript SDK packages alongside the LI.FI SDK:
The JavaScript SDK has no wagmi connector, so this recipe doesn’t install wagmi, @wagmi/core, or @lifi/wallet-management — LI.FI’s EVM provider talks to Dynamic wallets directly through viem, which is covered below.

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 developer console
  2. Navigate to Developer Settings → Embedded Wallets → Dynamic
  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.

Initialize Dynamic Client

Create src/lib/dynamic.ts to set up the JS SDK client and register the EVM extension:
src/lib/dynamic.ts

Create LI.FI Client

Create src/lib/lifi.ts to configure the LI.FI SDK. getWalletClient and switchChain call the same JavaScript SDK functions regardless of framework, so this file needs no JavaScript/React split — it’s plain JavaScript that both a vanilla app and this Next.js app can use as-is:
src/lib/lifi.ts
Here, we’ve created a loadLiFiChains function that fetches EVM chains from LI.FI. The other function initializeLiFiConfig initializes the LI.FI SDK’s EVM provider so it creates and refreshes viem WalletClients directly from the active Dynamic WalletAccount — createWalletClientForWalletAccount builds the client, and switchActiveNetwork moves the wallet to a new chain before the client is rebuilt. Neither call is React-specific; only the wallet account this function is fed (below) differs by framework.

Create LI.FI Provider

Create src/lib/lifi-provider.tsx to wrap the LI.FI configuration. LI.FI’s EVM provider isn’t reactive — it calls getEvmWalletAccount() fresh each time it needs a wallet, so this provider’s job is keeping that getter pointed at the latest wallet account:
The full LiFiProvider component below also loads the LI.FI chain list with React Query and gates rendering until both the Dynamic client and LI.FI are ready:
src/lib/lifi-provider.tsx
Here, we have created a LiFiProvider component that gets the chains from LI.FI and initializes the LI.FI SDK once the Dynamic client has finished initializing. There’s no wagmi config to sync anymore — LI.FI pulls the active WalletAccount from useGetWalletAccounts through the ref each time it needs a wallet client.

Configure Providers

Update src/lib/providers.tsx to include the LI.FI configuration:
src/lib/providers.tsx
This sets up the Dynamic client, React Query, and LI.FI initialization. There’s no WagmiProvider or DynamicWagmiConnector — DynamicProvider supplies the client to hooks directly, and LiFiProvider wires LI.FI to that client’s wallet state.

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 useGetWalletAccounts() and useInitStatus() for wallet state, createWalletClientForWalletAccount() to hand LI.FI a viem WalletClient for the active wallet, switchActiveNetwork() for chain switching inside switchChainHook, manage state updates through a single state object, and provide comprehensive error handling throughout the swap flow. createWalletClientForWalletAccount and switchActiveNetwork are plain JavaScript SDK calls — see the JavaScript tab under “Create LI.FI Provider” above for the non-React equivalent of getting the active wallet account that feeds them.

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 developer console under Developer Settings → CORS Origins.

Conclusion

This integration demonstrates Dynamic’s JavaScript SDK embedded 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 — all without a wagmi connector, since LI.FI’s EVM provider talks to the wallet directly through viem.

Additional Resources

Last modified on October 24, 2026