Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit f45df05

Browse files
committed
Try to remove invoice from pending nwc after payment
1 parent 493dfc1 commit f45df05

2 files changed

Lines changed: 57 additions & 15 deletions

File tree

mutiny-core/src/lib.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,17 @@ impl<S: MutinyStorage> MutinyWallet<S> {
13311331
.await;
13321332
match payment_result {
13331333
Ok(r) => {
1334+
// spawn a task to remove the pending invoice if it exists
1335+
let nostr_clone = self.nostr.clone();
1336+
let payment_hash = *inv.payment_hash();
1337+
let logger = self.logger.clone();
1338+
utils::spawn(async move {
1339+
if let Err(e) =
1340+
nostr_clone.remove_pending_nwc_invoice(&payment_hash).await
1341+
{
1342+
log_warn!(logger, "Failed to remove pending NWC invoice: {e}");
1343+
}
1344+
});
13341345
return Ok(r);
13351346
}
13361347
Err(e) => match e {
@@ -1368,9 +1379,22 @@ impl<S: MutinyStorage> MutinyWallet<S> {
13681379
.sum::<u64>()
13691380
> 0
13701381
{
1371-
self.node_manager
1382+
let res = self
1383+
.node_manager
13721384
.pay_invoice(None, inv, amt_sats, labels)
1373-
.await
1385+
.await?;
1386+
1387+
// spawn a task to remove the pending invoice if it exists
1388+
let nostr_clone = self.nostr.clone();
1389+
let payment_hash = *inv.payment_hash();
1390+
let logger = self.logger.clone();
1391+
utils::spawn(async move {
1392+
if let Err(e) = nostr_clone.remove_pending_nwc_invoice(&payment_hash).await {
1393+
log_warn!(logger, "Failed to remove pending NWC invoice: {e}");
1394+
}
1395+
});
1396+
1397+
Ok(res)
13741398
} else {
13751399
Err(last_federation_error.unwrap_or(MutinyError::InsufficientBalance))
13761400
}

mutiny-core/src/nostr/mod.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,35 @@ impl<S: MutinyStorage> NostrManager<S> {
966966
Ok(event_id)
967967
}
968968

969+
/// Removes an invoice from the pending list
970+
pub(crate) async fn remove_pending_nwc_invoice(
971+
&self,
972+
hash: &sha256::Hash,
973+
) -> Result<(), MutinyError> {
974+
// get lock for writing
975+
self.pending_nwc_lock.lock().await;
976+
977+
let mut pending: Vec<PendingNwcInvoice> = self
978+
.storage
979+
.get_data(PENDING_NWC_EVENTS_KEY)?
980+
.unwrap_or_default();
981+
982+
let original_len = pending.len();
983+
984+
// remove from storage
985+
pending.retain(|x| x.invoice.payment_hash() != hash);
986+
987+
// if we didn't remove anything, we don't need to save
988+
if original_len == pending.len() {
989+
return Ok(());
990+
}
991+
992+
self.storage
993+
.set_data(PENDING_NWC_EVENTS_KEY.to_string(), pending, None)?;
994+
995+
Ok(())
996+
}
997+
969998
/// Approves an invoice and sends the payment
970999
pub async fn approve_invoice(
9711000
&self,
@@ -998,19 +1027,8 @@ impl<S: MutinyStorage> NostrManager<S> {
9981027
}
9991028
};
10001029

1001-
// get lock for writing
1002-
self.pending_nwc_lock.lock().await;
1003-
1004-
// get from storage again, in case it was updated
1005-
let mut pending: Vec<PendingNwcInvoice> = self
1006-
.storage
1007-
.get_data(PENDING_NWC_EVENTS_KEY)?
1008-
.unwrap_or_default();
1009-
1010-
// remove from storage
1011-
pending.retain(|x| x.invoice.payment_hash() != &hash);
1012-
self.storage
1013-
.set_data(PENDING_NWC_EVENTS_KEY.to_string(), pending, None)?;
1030+
// remove from our pending list
1031+
self.remove_pending_nwc_invoice(&hash).await?;
10141032

10151033
Ok(event_id)
10161034
}

0 commit comments

Comments
 (0)