From f78013b0834d5ed95e1078fd17d159286058a255 Mon Sep 17 00:00:00 2001 From: lemonknowsall Date: Wed, 15 Oct 2025 21:29:39 -0700 Subject: [PATCH] npubxcash lightning address redemption working --- src/components/wallet/BalanceDisplay.tsx | 19 +++++- src/components/wallet/CashuWallet.tsx | 84 ++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) diff --git a/src/components/wallet/BalanceDisplay.tsx b/src/components/wallet/BalanceDisplay.tsx index 26df5e2..9d35fb9 100644 --- a/src/components/wallet/BalanceDisplay.tsx +++ b/src/components/wallet/BalanceDisplay.tsx @@ -2,7 +2,7 @@ import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger } from '@/components/ui/dropdown-menu'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog'; -import { Wallet, ChevronDown, Plus, X, Zap, Coins, HelpCircle, QrCode, Copy, Check } from 'lucide-react'; +import { Wallet, ChevronDown, Plus, X, Zap, Coins, HelpCircle, QrCode, Copy, Check, RefreshCw } from 'lucide-react'; import { useState, useEffect } from 'react'; import { exchangeRateService } from '@/lib/exchangeRateService'; @@ -33,6 +33,9 @@ interface BalanceDisplayProps { onCopyToClipboard: (text: string) => Promise; onShowLightningAddressQR: (qrCode: string) => void; generateQRCode: (text: string) => Promise; + // Refresh functionality + onRefreshQuotes: () => Promise; + isRefreshingQuotes: boolean; } export function BalanceDisplay({ @@ -56,6 +59,8 @@ export function BalanceDisplay({ onCopyToClipboard, onShowLightningAddressQR, generateQRCode, + onRefreshQuotes, + isRefreshingQuotes, }: BalanceDisplayProps) { const [lightningAddressCopied, setLightningAddressCopied] = useState(false); const [btcUsdRate, setBtcUsdRate] = useState(null); @@ -88,6 +93,18 @@ export function BalanceDisplay({ > {convertCurrency(totalBalance, showSats)} + + {/* Refresh Quotes Button */} + {userPubkey && ( + + )} {/* Main Mode Toggle */} diff --git a/src/components/wallet/CashuWallet.tsx b/src/components/wallet/CashuWallet.tsx index 7f51c07..556c116 100644 --- a/src/components/wallet/CashuWallet.tsx +++ b/src/components/wallet/CashuWallet.tsx @@ -38,6 +38,7 @@ export function CashuWallet() { const { setMint: setNpubCashMint, getUserInfo: getNpubCashUserInfo, + getAllQuotes, isLoggedIn: isNpubCashLoggedIn, isLoading: isNpubCashLoading, error: npubCashError, @@ -154,6 +155,87 @@ export function CashuWallet() { } }, []); + // ============================================================================ + // HELPER FUNCTIONS - Quote Refresh + // ============================================================================ + + /** + * Refresh quotes from npub.cash and add them to the wallet + * This matches the functionality from main.ts example + */ + const handleRefreshQuotes = useCallback(async () => { + if (!coco || !isNpubCashLoggedIn) return; + + try { + // Fetch all quotes from npub.cash + const quotes = await getAllQuotes(); + + if (quotes.length === 0) { + toast({ + title: "No Quotes Found", + description: "No pending quotes found on npub.cash", + }); + return; + } + + // Group quotes by mint URL + const quotesByMint = quotes.reduce((acc, quote) => { + if (!acc[quote.mintUrl]) { + acc[quote.mintUrl] = []; + } + acc[quote.mintUrl].push(quote); + return acc; + }, {} as Record); + + let addedQuotes = 0; + + // Process each mint's quotes + for (const [mintUrl, mintQuotes] of Object.entries(quotesByMint)) { + try { + // Check if mint exists, add if not + const isKnown = await coco.mint.isKnownMint(mintUrl); + if (!isKnown) { + await coco.mint.addMint(mintUrl); + } + + // Add quotes to the wallet (matching main.ts format) + coco.quotes.addMintQuote( + mintUrl, + mintQuotes.map((q) => ({ + ...q, + expiry: q.expiresAt, + quote: q.quoteId, + state: "PAID", + unit: "sat", + })), + ); + + addedQuotes += mintQuotes.length; + } catch (err) { + console.warn(`Failed to process quotes for mint ${mintUrl}:`, err); + } + } + + if (addedQuotes > 0) { + toast({ + title: "Quotes Refreshed", + description: `Successfully added ${addedQuotes} quotes from npub.cash`, + }); + } else { + toast({ + title: "No New Quotes", + description: "All quotes from npub.cash are already processed", + }); + } + } catch (err) { + toast({ + variant: "destructive", + title: "Refresh Failed", + description: err instanceof Error ? err.message : 'Failed to refresh quotes', + }); + } + }, [coco, isNpubCashLoggedIn, getAllQuotes, toast]); + // ============================================================================ // HELPER FUNCTIONS - Mint Management // ============================================================================ @@ -666,6 +748,8 @@ export function CashuWallet() { setShowLightningAddressQR(true); }} generateQRCode={generateQRCode} + onRefreshQuotes={handleRefreshQuotes} + isRefreshingQuotes={isNpubCashLoading} /> {/* ========================================================================