mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-05 16:33:36 +00:00
npubxcash lightning address redemption working
This commit is contained in:
1 parent
bd956cef3f
commit
f78013b083
2 files changed
+102
-1
No files matched your search
@@ -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<void>;
|
||||
onShowLightningAddressQR: (qrCode: string) => void;
|
||||
generateQRCode: (text: string) => Promise<string>;
|
||||
// Refresh functionality
|
||||
onRefreshQuotes: () => Promise<void>;
|
||||
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<number | null>(null);
|
||||
@@ -88,6 +93,18 @@ export function BalanceDisplay({
|
||||
>
|
||||
{convertCurrency(totalBalance, showSats)}
|
||||
</button>
|
||||
|
||||
{/* Refresh Quotes Button */}
|
||||
{userPubkey && (
|
||||
<button
|
||||
onClick={onRefreshQuotes}
|
||||
disabled={isRefreshingQuotes}
|
||||
className="flex items-center justify-center p-2 hover:bg-muted rounded-md transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
title="Refresh quotes from npub.cash"
|
||||
>
|
||||
<RefreshCw className={`h-5 w-5 text-muted-foreground ${isRefreshingQuotes ? 'animate-spin' : ''}`} />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Main Mode Toggle */}
|
||||
|
||||
@@ -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<string, typeof quotes>);
|
||||
|
||||
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}
|
||||
/>
|
||||
|
||||
{/* ========================================================================
|
||||
|
||||
Reference in new issue
Block a user