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>
);
}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:
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.
Connection Persistence: The
updateIntervalconfiguration keeps an active wallet connection stable through periodic state checks.Seamless Reconnection: The
tryToReconnectToproperty 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:
Start your development server:
Navigate to your application (usually at http://localhost:3000)
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
Test disconnection:
Click the "Disconnect" button
Verify that the UI returns to the wallet selection state
If wallet connection fails, check your browser console for errors. Common issues include:
Wallet extension not properly installed
Wallet needs to be enabled for dApp interactions.
Wallet locked (needs to be unlocked first)
Incompatible wallet versions
Troubleshooting
No Wallets Detected
If no wallets are appearing in the dropdown list:
Make sure you have wallet extensions installed (Eternl, Lace, etc.)
Refresh the page after installing a new wallet extension
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.
Congratulations! You've completed Part 2 of the guide. Your application can now detect wallets, connect to them, and display wallet information.
Last updated
Was this helpful?

