Skip to main content

Overview

For complete control over wallet connection flows, you can implement a custom WalletProvider that bridges any wallet management solution with Layerswap. This deep integration approach enables you to:
  • Use your own custom wallet connection UI
  • Integrate 3rd party wallet libraries (Dynamic, Reown, RainbowKit, etc.)
  • Control the entire wallet lifecycle (connect, disconnect, state management)
  • Customize wallet behavior to match your application’s UX

Implementation Steps

1. Create Custom Hook

Implement WalletConnectionProvider that:
  • Connects to your wallet management solution (Dynamic, Reown, RainbowKit, Privy, etc.)
  • Transforms external wallet format to Layerswap Wallet type
  • Handles connect/disconnect lifecycle and error states
  • Manages wallet state and active wallet selection
  • Listens to wallet events (connection, disconnection, account changes)

2. Define Supported Networks

Specify which networks your provider supports:
  • asSource - Networks that can be used as swap source
  • autofill - Networks that support address autofill
  • withdrawal - Networks that support withdrawals

3. Implement Connect Flow

The connectWallet function is called when users click “Connect Wallet” in the widget:

4. Handle Wallet State

Keep track of connected wallets and active wallet:

5. Create Provider with Custom Hook

Use the factory function with your custom hook:
Deprecated Pattern: Do NOT use the old spreading pattern:
Always use the factory function (createStarknetProvider, createEVMProvider, etc.) with the customHook parameter.

User Experience Flow

Connection Flow

  1. User opens Layerswap widget
  2. User clicks “Connect Wallet” button
  3. Your custom connectWallet function is called
  4. Your custom UI/modal appears (Dynamic, Reown, RainbowKit, etc.)
  5. User selects wallet and approves connection
  6. Your hook transforms the wallet to Layerswap format
  7. Widget displays connected wallet and enables swap functionality

Disconnection Flow

  1. User clicks disconnect in widget
  2. Your wallet’s disconnect callback is called
  3. Your wallet management library handles cleanup
  4. Widget updates to show “Connect Wallet” button

Critical Implementation Details

Important: These details are critical for correct implementation
  1. Icon Resolution: The icon property in the Wallet type must return a React component, NOT a URL string. Always use the utility function:
  2. Ready State: Always return ready: true in your WalletConnectionProvider when your provider is initialized and ready to handle connections. This is a required field - the widget will not function without it.
  3. WalletConnect Configs: Only needed for native EVM/SVM/Starknet providers when NOT using a custom hook. If you provide customHook, the WalletConnect configuration is ignored since you control the connection flow.
  4. Type Imports: Import types from @layerswap/widget/types:
  5. Disconnect Callback: The disconnectWallets function in WalletConnectionProvider is optional but recommended for proper cleanup. Each individual Wallet must have its own disconnect callback.

Creating a Custom Provider

To create a custom provider, use the provider creator function with the customHook parameter:

WalletConnectionProvider Interface

Your custom hook must implement this interface:

Props Passed to Your Hook

Return Values

Wallet Type

Your custom provider must transform external wallet objects to Layerswap’s Wallet format:

Complete Example: Dynamic Labs Integration

This example demonstrates integrating Starknet wallets using Dynamic Labs SDK. The same pattern applies to any 3rd party library.

Live Demo

Explore the complete source code at layerswap/layerswapapp - nextjs-dynamic example

Installation

Step 1: Create Custom Connection Hook

Create a hook that implements the WalletConnectionProvider interface:

Step 2: Set Up Providers in Main Component

Step 3: Environment Variables

Create a .env.local file:
Get Your API Keys:

Additional Examples

For more custom integration examples: