Wallet Integration
Implement wallet connectivity using the Weld library to allow users to connect their Cardano wallets to the application.
Introduction
In this part, we'll implement wallet connectivity using Weld, a universal wallet connector library that provides a simple, consistent interface for connecting to Cardano wallets. Weld handles all the complexity of wallet discovery, connection management, and state persistence, allowing our application to interact with various wallet providers (Eternl, Lace, etc.) through a single API.
Implementation Steps
1. Create the Weld Provider
First, let's create a provider component that will initialize the Weld library and make wallet functionality available throughout our application src/components/WeldProvider.tsx:
// 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. Update Root Layout
Now we need to update the root layout to include our Weld provider src/app/layout.tsx:
3. Create the Wallet Connector Component
Now, let's create a component for users to connect their wallets. It will:
Use Weld's
useWallethook to interact with the user's walletDisplay a connect button when no wallet is connected
Show wallet information (address and balance) when connected
Provide a disconnect button for connected wallets
Format the wallet's lovelace balance to show readable ADA amounts
Create the WalletConnector component src/components/WalletConnector.tsx:
3. Update Home Page
Update the home page to include the wallet connector component src/app/page.tsx:
Understanding Weld Integration
The Weld Provider
The WeldProvider component creates a reactive context that makes wallet functionality available throughout your application:
Universal API: Provides a consistent interface regardless of wallet implementation
Network Configuration: Automatically configures for testnet (preview/preprod) or mainnet
State Management: Handles connection state and persistence across page refreshes
The useWallet Hook
The useWallet hook exposes wallet functionality with smart TypeScript type inference:
Connection State:
isConnectedworks as a type guard for other propertiesWallet Data: Access addresses, balances, and wallet metadata
Actions: Methods for connecting, disconnecting, and signing transactions
Reactive Updates: Properties update automatically when wallet state changes
Key Concepts
Wallet Discovery: Weld detects all CIP-30 compatible wallets installed in the browser
Address Formats: Works with Bech32 addresses (human-readable format starting with
addr_)Balance Units: Converts between lovelace (smallest unit) and ADA (1 ADA = 1,000,000 lovelace)
Connection Persistence: Maintains wallet connection across page reloads
Testing Your Wallet Integration
To test your wallet integration:
Start your development server:
Visit http://localhost:3000 in your browser
Make sure you have a CIP-30 compatible wallet extension installed
Fully-tested wallets include: Eternl, Lace, Flint, GeroWallet, Typhon, and Vespr
Click the "Connect Wallet" button
Select your wallet from the popup and approve the connection
Verify that your address and balance are displayed correctly
Troubleshooting
Wallet Not Detected
If your wallet isn't detected:
Verify the wallet extension is installed, unlocked and refreshed after installation
Confirm that your wallet implements the CIP-30 standard
Try clearing browser cache or restarting your browser
Network Mismatch
If you see connection errors:
Ensure your wallet is set to the same network as your application (Preview/Preprod)
Remember that wallets require explicit testnet mode activation
Check the wallet's network selector in its settings menu
Congratulations! You've completed Part 2 of the guide. Your application can now connect to Cardano wallets and display the user's address and balance.
Last updated
Was this helpful?

