infinite loop initialization, wallet endpoint structure, zapdialog debug

This commit is contained in:
lemonknowsall committed 2025-10-06 17:27:30 -07:00
1 parent 4672413b04
commit e14d4e887f
6 files changed
+284 -23

No files matched your search

+89 -10
View File
@@ -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);