From 98d77ce2c84f009360d29356523891fa389d9271 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Sat, 8 Feb 2025 12:57:57 +0000 Subject: [PATCH] feat: check mint ln payment status --- crates/cdk-integration-tests/Cargo.toml | 2 +- crates/cdk/src/mint/ln.rs | 46 +++++++++++++++++++++++++ crates/cdk/src/mint/mint_nut04.rs | 7 ++++ crates/cdk/src/mint/mod.rs | 1 + 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 crates/cdk/src/mint/ln.rs diff --git a/crates/cdk-integration-tests/Cargo.toml b/crates/cdk-integration-tests/Cargo.toml index 0f482812..ce66f675 100644 --- a/crates/cdk-integration-tests/Cargo.toml +++ b/crates/cdk-integration-tests/Cargo.toml @@ -36,7 +36,7 @@ uuid = { version = "1", features = ["v4"] } serde = "1" serde_json = "1" # ln-regtest-rs = { path = "../../../../ln-regtest-rs" } -ln-regtest-rs = { git = "https://github.com/thesimplekid/ln-regtest-rs", rev = "f9e7bbbb" } +ln-regtest-rs = { git = "https://github.com/thesimplekid/ln-regtest-rs", rev = "bf09ad6" } lightning-invoice = { version = "0.32.0", features = ["serde", "std"] } tracing = { version = "0.1", default-features = false, features = [ "attributes", diff --git a/crates/cdk/src/mint/ln.rs b/crates/cdk/src/mint/ln.rs new file mode 100644 index 00000000..701d0b6d --- /dev/null +++ b/crates/cdk/src/mint/ln.rs @@ -0,0 +1,46 @@ +use cdk_common::common::LnKey; +use cdk_common::MintQuoteState; + +use super::Mint; +use crate::mint::Uuid; +use crate::Error; + +impl Mint { + /// Check the status of an ln payment for a quote + pub async fn check_mint_quote_paid(&self, quote_id: &Uuid) -> Result { + let mut quote = self + .localstore + .get_mint_quote(quote_id) + .await? + .ok_or(Error::UnknownQuote)?; + + let ln = match self.ln.get(&LnKey::new( + quote.unit.clone(), + cdk_common::PaymentMethod::Bolt11, + )) { + Some(ln) => ln, + None => { + tracing::info!("Could not get ln backend for {}, bolt11 ", quote.unit); + + return Err(Error::UnsupportedUnit); + } + }; + + let ln_status = ln + .check_incoming_invoice_status("e.request_lookup_id) + .await?; + + if ln_status != quote.state && quote.state != MintQuoteState::Issued { + self.localstore + .update_mint_quote_state(quote_id, ln_status) + .await?; + + quote.state = ln_status; + + self.pubsub_manager + .mint_quote_bolt11_status(quote.clone(), ln_status); + } + + Ok(quote.state) + } +} diff --git a/crates/cdk/src/mint/mint_nut04.rs b/crates/cdk/src/mint/mint_nut04.rs index 361c275f..12def5eb 100644 --- a/crates/cdk/src/mint/mint_nut04.rs +++ b/crates/cdk/src/mint/mint_nut04.rs @@ -147,6 +147,7 @@ impl Mint { // a quote while waiting for the mint response. let state = match quote.state { MintQuoteState::Pending => MintQuoteState::Paid, + MintQuoteState::Unpaid => self.check_mint_quote_paid(quote_id).await?, s => s, }; @@ -274,6 +275,12 @@ impl Mint { .update_mint_quote_state(&mint_request.quote, MintQuoteState::Pending) .await?; + let state = if state == MintQuoteState::Unpaid { + self.check_mint_quote_paid(&mint_quote.id).await? + } else { + state + }; + match state { MintQuoteState::Unpaid => { let _state = self diff --git a/crates/cdk/src/mint/mod.rs b/crates/cdk/src/mint/mod.rs index eea6e169..d17aa216 100644 --- a/crates/cdk/src/mint/mod.rs +++ b/crates/cdk/src/mint/mod.rs @@ -27,6 +27,7 @@ use crate::Amount; mod builder; mod check_spendable; mod keysets; +mod ln; mod melt; mod mint_nut04; mod start_up_check;