fix: add HTTP polling fallback for mint quote payment detection

This commit is contained in:
Forte11Cuba
2026-04-03 16:56:22 -06:00
parent 060c6acd4e
commit 4d2ad39323
+53 -10
View File
@@ -234,14 +234,64 @@ impl Wallet {
let expired_amount = quote.amount;
let expired_expiry = quote.expiry;
// Detect payment via two parallel paths:
// - Path A: WebSocket (NUT-17) — fast when it works (sats on most mints)
// - Path B: HTTP polling every 5s — fallback for mints that don't send
// WebSocket notifications for all units (e.g. Nutshell 0.20.0 + USD)
// First one to detect payment wins.
let quote_id_for_poll = quote.id.clone();
let poll_wallet = _self.clone();
let result = tokio::time::timeout(timeout_dur, async {
// Enum to unify both detection paths
enum Detected {
Paid,
Issued,
}
let detected = tokio::select! {
// Path A: WebSocket subscription
result = async {
while let Some(event) = subscription.recv().await {
match event.into_inner() {
NotificationPayload::MintQuoteBolt11Response(info)
if info.state == CdkMintQuoteState::Paid =>
{
info!("Mint quote {} paid via subscription", quote.id);
info!("Mint quote {} paid via WebSocket", quote.id);
return Detected::Paid;
}
NotificationPayload::MintQuoteBolt11Response(info)
if info.state == CdkMintQuoteState::Issued =>
{
return Detected::Issued;
}
_ => continue,
}
}
// Subscription closed without detecting payment — wait for poll path
std::future::pending::<Detected>().await
} => result,
// Path B: HTTP polling fallback
result = async {
loop {
tokio::time::sleep(Duration::from_secs(5)).await;
match poll_wallet.inner.check_mint_quote_status(&quote_id_for_poll).await {
Ok(q) if q.state == CdkMintQuoteState::Paid => {
info!("Mint quote {} paid via HTTP polling", quote_id_for_poll);
return Detected::Paid;
}
Ok(q) if q.state == CdkMintQuoteState::Issued => {
return Detected::Issued;
}
_ => continue,
}
}
} => result,
};
match detected {
Detected::Paid => {
// Notify Dart: payment detected
let _ = sink.add(MintQuote {
id: quote.id.clone(),
@@ -304,13 +354,9 @@ impl Wallet {
});
}
}
return;
}
NotificationPayload::MintQuoteBolt11Response(info)
if info.state == CdkMintQuoteState::Issued =>
{
// Already issued (recovered from previous session) — notify Dart
// so it can clean up pending metadata and show success UI
Detected::Issued => {
// Already issued (recovered from previous session)
let _ = sink.add(MintQuote {
id: quote.id.clone(),
request: quote.request.clone(),
@@ -322,9 +368,6 @@ impl Wallet {
transaction_id: None,
});
_self.update_balance_streams().await;
return;
}
_ => continue,
}
}
})