Part 2: Wallet Integration

This is Part 2 of our guide to building a Cardano transaction application with Next.js and Weld. In this section, you'll implement wallet connectivity with proper SSR support.

Weld Wallet Integration Overview

Weld is a universal Cardano wallet connector library that simplifies wallet integration by providing:

  • A unified interface for multiple Cardano wallets

  • TypeScript support with full type safety

  • React hooks for state management

  • Server-side rendering compatibility

See the Weld documentation for more information.

Implementation Steps

1. Create the Weld Provider Component

First, we need to create a provider component that will make Weld available throughout your application. This component will handle wallet connectivity state and server/client hydration:

// src/components/WeldProvider.tsx
"use client";

import { WeldProvider, type WeldProviderProps } from "@ada-anvil/weld/react";

export function ClientWeldProvider({
  children,
  lastConnectedWallet,
}: {
  children: React.ReactNode;
  lastConnectedWallet?: NonNullable<
    WeldProviderProps["wallet"]
  >["tryToReconnectTo"];
}) {
  return (
    <WeldProvider
      updateInterval={30_000} // 30 seconds
      wallet={{ tryToReconnectTo: lastConnectedWallet }} // Restore wallet connection state
    >
      {children}
    </WeldProvider>
  );
}

The updateInterval option (set to 30 seconds) helps maintain active wallet connections during longer user sessions.

2. Integrate the Provider in Your Layout

Next, we'll update the app layout to use our ClientWeldProvider. Notice that we include the lastConnectedWallet prop to restore wallet connection state between server and client. This is important for maintaining wallet connectivity during page navigation, and preventing React hydration errors due to server-side rendering being inconsistent with client-side rendering.

See the Server-Side Rendering (SSR) section in the Weld documentation for more information.

3. Understanding SSR with Weld

Our implementation addresses three critical aspects of wallet integration in Next.js with SSR:

  1. Hydration Consistency: By retrieving wallet connection data from cookies during server-side rendering, we ensure the initial client-side state matches what was rendered on the server, preventing React hydration errors.

  2. Connection Persistence: The updateInterval configuration keeps an active wallet connection stable through periodic state checks.

  3. Seamless Reconnection: The tryToReconnectTo property provides a smooth user experience by maintaining wallet connections across page refreshes.

4. Create a Wallet Connector Component

Now let's create a UI component that users will interact with to connect their wallets:

5. Update the Home Page

Now that we have created the component. Lets update your home page to include the wallet connector component:

Testing Your Wallet Integration

Now let's test the wallet integration to ensure it's working correctly:

  1. Start your development server:

  1. Navigate to your application (usually at http://localhost:3000)

  2. Test the wallet connection flow:

    • Verify that available wallets are correctly detected in the dropdown

    • Select a wallet and click "Connect Wallet"

    • Confirm that the wallet popup appears requesting connection

    • After approving, verify that wallet information is displayed:

      • Connected wallet name

      • Truncated wallet address

      • ADA balance

  3. Test disconnection:

    • Click the "Disconnect" button

    • Verify that the UI returns to the wallet selection state

Troubleshooting

No Wallets Detected

If no wallets are appearing in the dropdown list:

  1. Make sure you have wallet extensions installed (Eternl, Lace, etc.)

  2. Refresh the page after installing a new wallet extension

  3. Check your browser console for any errors

What's Next?

Now that you have a working wallet integration, you're ready to implement transaction functionality. In Part 3: Building Transactions, we'll create the components and API routes needed to build and submit Cardano transactions.

Last updated

Was this helpful?