mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-06 00:37:23 +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 { Input } from '@/components/ui/input';
|
||||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger } from '@/components/ui/dropdown-menu';
|
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger } from '@/components/ui/dropdown-menu';
|
||||||
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog';
|
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 { useState, useEffect } from 'react';
|
||||||
import { exchangeRateService } from '@/lib/exchangeRateService';
|
import { exchangeRateService } from '@/lib/exchangeRateService';
|
||||||
|
|
||||||
@@ -33,6 +33,9 @@ interface BalanceDisplayProps {
|
|||||||
onCopyToClipboard: (text: string) => Promise<void>;
|
onCopyToClipboard: (text: string) => Promise<void>;
|
||||||
onShowLightningAddressQR: (qrCode: string) => void;
|
onShowLightningAddressQR: (qrCode: string) => void;
|
||||||
generateQRCode: (text: string) => Promise<string>;
|
generateQRCode: (text: string) => Promise<string>;
|
||||||
|
// Refresh functionality
|
||||||
|
onRefreshQuotes: () => Promise<void>;
|
||||||
|
isRefreshingQuotes: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function BalanceDisplay({
|
export function BalanceDisplay({
|
||||||
@@ -56,6 +59,8 @@ export function BalanceDisplay({
|
|||||||
onCopyToClipboard,
|
onCopyToClipboard,
|
||||||
onShowLightningAddressQR,
|
onShowLightningAddressQR,
|
||||||
generateQRCode,
|
generateQRCode,
|
||||||
|
onRefreshQuotes,
|
||||||
|
isRefreshingQuotes,
|
||||||
}: BalanceDisplayProps) {
|
}: BalanceDisplayProps) {
|
||||||
const [lightningAddressCopied, setLightningAddressCopied] = useState(false);
|
const [lightningAddressCopied, setLightningAddressCopied] = useState(false);
|
||||||
const [btcUsdRate, setBtcUsdRate] = useState<number | null>(null);
|
const [btcUsdRate, setBtcUsdRate] = useState<number | null>(null);
|
||||||
@@ -88,6 +93,18 @@ export function BalanceDisplay({
|
|||||||
>
|
>
|
||||||
{convertCurrency(totalBalance, showSats)}
|
{convertCurrency(totalBalance, showSats)}
|
||||||
</button>
|
</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>
|
</div>
|
||||||
|
|
||||||
{/* Main Mode Toggle */}
|
{/* Main Mode Toggle */}
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ export function CashuWallet() {
|
|||||||
const {
|
const {
|
||||||
setMint: setNpubCashMint,
|
setMint: setNpubCashMint,
|
||||||
getUserInfo: getNpubCashUserInfo,
|
getUserInfo: getNpubCashUserInfo,
|
||||||
|
getAllQuotes,
|
||||||
isLoggedIn: isNpubCashLoggedIn,
|
isLoggedIn: isNpubCashLoggedIn,
|
||||||
isLoading: isNpubCashLoading,
|
isLoading: isNpubCashLoading,
|
||||||
error: npubCashError,
|
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
|
// HELPER FUNCTIONS - Mint Management
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
@@ -666,6 +748,8 @@ export function CashuWallet() {
|
|||||||
setShowLightningAddressQR(true);
|
setShowLightningAddressQR(true);
|
||||||
}}
|
}}
|
||||||
generateQRCode={generateQRCode}
|
generateQRCode={generateQRCode}
|
||||||
|
onRefreshQuotes={handleRefreshQuotes}
|
||||||
|
isRefreshingQuotes={isNpubCashLoading}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{/* ========================================================================
|
{/* ========================================================================
|
||||||
|
|||||||
Reference in new issue
Block a user