mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-05 16:33:36 +00:00
infinite loop initialization, wallet endpoint structure, zapdialog debug
This commit is contained in:
1 parent
4672413b04
commit
e14d4e887f
6 files changed
+284
-23
No files matched your search
+89
-10
@@ -203,12 +203,21 @@ export function useCashu(): UseCashuReturn {
|
||||
|
||||
if (claimedCount > 0) {
|
||||
console.log(`🎉 Auto-claimed ${claimedCount} paid quotes!`);
|
||||
await refreshBalances();
|
||||
// Call refreshBalances directly without dependency to avoid circular dependency
|
||||
try {
|
||||
console.log('🔄 refreshBalances: Getting balances from wallet...');
|
||||
const newBalances = await (managerRef.current as { wallet: { getBalances: () => Promise<CashuBalance> } }).wallet.getBalances();
|
||||
console.log('💰 refreshBalances: Raw balances from wallet:', newBalances);
|
||||
setBalances(newBalances);
|
||||
console.log('✅ refreshBalances: State updated with balances:', Object.keys(newBalances).length, 'mints');
|
||||
} catch (err) {
|
||||
console.error('❌ refreshBalances: Failed to refresh balances:', err);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('❌ Error checking pending quotes:', err);
|
||||
}
|
||||
}, [refreshBalances]);
|
||||
}, []); // Remove refreshBalances dependency to break the circular dependency
|
||||
|
||||
// Initialize the manager
|
||||
const initializeManager = useCallback(async () => {
|
||||
@@ -251,12 +260,81 @@ export function useCashu(): UseCashuReturn {
|
||||
);
|
||||
managerRef.current = manager;
|
||||
|
||||
// Load initial data
|
||||
await refreshBalances();
|
||||
await refreshMints();
|
||||
// Load initial data - call functions directly to avoid circular dependencies
|
||||
try {
|
||||
console.log('🔄 refreshBalances: Getting balances from wallet...');
|
||||
const newBalances = await manager.wallet.getBalances();
|
||||
console.log('💰 refreshBalances: Raw balances from wallet:', newBalances);
|
||||
setBalances(newBalances);
|
||||
console.log('✅ refreshBalances: State updated with balances:', Object.keys(newBalances).length, 'mints');
|
||||
} catch (err) {
|
||||
console.error('❌ refreshBalances: Failed to refresh balances:', err);
|
||||
}
|
||||
|
||||
try {
|
||||
const mintList = await manager.mint.getAllMints();
|
||||
const formattedMints = mintList
|
||||
.filter((mint: { mintUrl?: string }) => mint && mint.mintUrl) // Filter out invalid mints - use mintUrl not url
|
||||
.map((mint: { mintUrl: string; name?: string; mintInfo?: Record<string, unknown> }) => ({
|
||||
url: mint.mintUrl, // Map mintUrl to url for consistency
|
||||
name: mint.name,
|
||||
info: mint.mintInfo
|
||||
}));
|
||||
setMints(formattedMints);
|
||||
} catch (err) {
|
||||
console.error('Failed to refresh mints:', err);
|
||||
}
|
||||
|
||||
// Check for any pending quotes that might have been paid
|
||||
await checkAndRedeemPendingQuotes();
|
||||
try {
|
||||
console.log('🔍 Checking for pending mint quotes in IndexedDB...');
|
||||
|
||||
// Access the mint quotes repository directly
|
||||
const mintQuotesRepo = (repositories as { mintQuoteRepository?: { getPendingMintQuotes: () => Promise<Array<Record<string, unknown>>> } })?.mintQuoteRepository;
|
||||
if (mintQuotesRepo) {
|
||||
// Get all pending mint quotes (non-ISSUED quotes)
|
||||
const pendingQuotes = await mintQuotesRepo.getPendingMintQuotes();
|
||||
console.log(`📋 Found ${pendingQuotes.length} pending quotes in database`);
|
||||
|
||||
// Filter for UNPAID quotes (these might have been paid but not redeemed)
|
||||
const unpaidQuotes = pendingQuotes.filter((q: { state?: string }) => q.state === 'UNPAID');
|
||||
console.log(`⏳ Found ${unpaidQuotes.length} UNPAID quotes to check`);
|
||||
|
||||
let claimedCount = 0;
|
||||
|
||||
for (const quote of unpaidQuotes) {
|
||||
try {
|
||||
const q = quote as { quote: string; mintUrl: string; amount: number };
|
||||
console.log(`🔄 Attempting to redeem quote ${q.quote} from ${q.mintUrl}...`);
|
||||
await manager.quotes.redeemMintQuote(q.mintUrl, q.quote);
|
||||
claimedCount++;
|
||||
console.log(`✅ Successfully claimed ${q.amount} sats from quote ${q.quote}`);
|
||||
} catch {
|
||||
const q = quote as { quote: string };
|
||||
// Quote not paid yet or already claimed - this is expected, just skip
|
||||
console.log(`⏭️ Quote ${q.quote} not ready yet (invoice not paid or already claimed)`);
|
||||
}
|
||||
}
|
||||
|
||||
if (claimedCount > 0) {
|
||||
console.log(`🎉 Auto-claimed ${claimedCount} paid quotes!`);
|
||||
// Refresh balances after claiming quotes
|
||||
try {
|
||||
console.log('🔄 refreshBalances: Getting balances from wallet...');
|
||||
const newBalances = await manager.wallet.getBalances();
|
||||
console.log('💰 refreshBalances: Raw balances from wallet:', newBalances);
|
||||
setBalances(newBalances);
|
||||
console.log('✅ refreshBalances: State updated with balances:', Object.keys(newBalances).length, 'mints');
|
||||
} catch (err) {
|
||||
console.error('❌ refreshBalances: Failed to refresh balances:', err);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.log('⚠️ Mint quotes repository not available');
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('❌ Error checking pending quotes:', err);
|
||||
}
|
||||
|
||||
setIsInitialized(true);
|
||||
} catch (err) {
|
||||
@@ -266,9 +344,9 @@ export function useCashu(): UseCashuReturn {
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [isInitialized, isLoading, mnemonic, setMnemonicState, refreshBalances, refreshMints, checkAndRedeemPendingQuotes]);
|
||||
}, [isInitialized, isLoading, mnemonic, setMnemonicState]); // Remove function dependencies to break circular dependency
|
||||
|
||||
// Initialize on mount
|
||||
// Initialize on mount and when mnemonic changes
|
||||
useEffect(() => {
|
||||
initializeManager();
|
||||
}, [initializeManager]);
|
||||
@@ -459,8 +537,9 @@ export function useCashu(): UseCashuReturn {
|
||||
setMnemonicState(newMnemonic);
|
||||
// Reinitialize manager with new mnemonic
|
||||
setIsInitialized(false);
|
||||
initializeManager();
|
||||
}, [setMnemonicState, initializeManager]);
|
||||
// Don't call initializeManager directly to avoid circular dependency
|
||||
// The useEffect will handle reinitialization when isInitialized changes
|
||||
}, [setMnemonicState]);
|
||||
|
||||
const generateMnemonic = useCallback(() => {
|
||||
return bip39.generateMnemonic(wordlist);
|
||||
|
||||
+26
-4
@@ -133,7 +133,13 @@ export function useZaps(
|
||||
}, [zapEvents, actualTarget]);
|
||||
|
||||
const zap = async (amount: number, comment: string) => {
|
||||
if (amount <= 0) {
|
||||
if (amount <= 0 || isNaN(amount)) {
|
||||
toast({
|
||||
title: 'Invalid amount',
|
||||
description: 'Please enter a valid amount greater than 0.',
|
||||
variant: 'destructive',
|
||||
});
|
||||
setIsZapping(false);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -160,6 +166,17 @@ export function useZaps(
|
||||
return;
|
||||
}
|
||||
|
||||
// Additional validation to ensure actualTarget has required properties
|
||||
if (!actualTarget.pubkey || !actualTarget.id) {
|
||||
toast({
|
||||
title: 'Invalid event',
|
||||
description: 'The event is missing required properties.',
|
||||
variant: 'destructive',
|
||||
});
|
||||
setIsZapping(false);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if (!author.data || !author.data?.metadata || !author.data?.event ) {
|
||||
toast({
|
||||
@@ -197,12 +214,17 @@ export function useZaps(
|
||||
// Create zap request - use appropriate event format based on kind
|
||||
// For addressable events (30000-39999), pass the object to get 'a' tag
|
||||
// For all other events, pass the ID string to get 'e' tag
|
||||
const event = (actualTarget.kind >= 30000 && actualTarget.kind < 40000)
|
||||
? actualTarget
|
||||
: actualTarget.id;
|
||||
const event = (actualTarget!.kind >= 30000 && actualTarget!.kind < 40000)
|
||||
? actualTarget!
|
||||
: actualTarget!.id;
|
||||
|
||||
const zapAmount = amount * 1000; // convert to millisats
|
||||
|
||||
// Validate zapAmount is a valid number
|
||||
if (isNaN(zapAmount) || zapAmount <= 0) {
|
||||
throw new Error('Invalid zap amount');
|
||||
}
|
||||
|
||||
const zapRequest = nip57.makeZapRequest({
|
||||
profile: actualTarget.pubkey,
|
||||
event: event as Event,
|
||||
|
||||
Reference in new issue
Block a user