Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check mint ln payment status #591

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/cdk-integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 4 additions & 2 deletions crates/cdk-integration-tests/tests/fake_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -866,8 +866,10 @@ async fn test_fake_mint_input_output_mismatch() -> Result<()> {

match response {
Err(err) => match err {
cdk::Error::UnsupportedUnit => (),
_ => {}
cdk::Error::UnitMismatch => (),
err => {
bail!("Wrong error returned: {}", err);
}
},
Ok(_) => {
bail!("Should not have allowed to mint with multiple units");
Expand Down
46 changes: 46 additions & 0 deletions crates/cdk/src/mint/ln.rs
Original file line number Diff line number Diff line change
@@ -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<MintQuoteState, Error> {
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(&quote.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)
}
}
7 changes: 7 additions & 0 deletions crates/cdk/src/mint/mint_nut04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions crates/cdk/src/mint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 5 additions & 25 deletions crates/cdk/src/mint/start_up_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,12 @@ impl Mint {
/// with all the lighting backends. This check that any payments
/// received while the mint was offline are accounted for, and the wallet can mint associated ecash
pub async fn check_pending_mint_quotes(&self) -> Result<(), Error> {
let mut pending_quotes = self.get_pending_mint_quotes().await?;
let pending_quotes = self.get_pending_mint_quotes().await?;
tracing::info!("There are {} pending mint quotes.", pending_quotes.len());
let mut unpaid_quotes = self.get_unpaid_mint_quotes().await?;
tracing::info!("There are {} unpaid mint quotes.", unpaid_quotes.len());

unpaid_quotes.append(&mut pending_quotes);

for ln in self.ln.values() {
for quote in unpaid_quotes.iter() {
tracing::debug!("Checking status of mint quote: {}", quote.id);
let lookup_id = quote.request_lookup_id.as_str();
match ln.check_incoming_invoice_status(lookup_id).await {
Ok(state) => {
if state != quote.state {
tracing::trace!("Mint quote status changed: {}", quote.id);
self.localstore
.update_mint_quote_state(&quote.id, state)
.await?;
}
}

Err(err) => {
tracing::warn!("Could not check state of pending invoice: {}", lookup_id);
tracing::error!("{}", err);
}
}
for quote in pending_quotes.iter() {
tracing::debug!("Checking status of mint quote: {}", quote.id);
if let Err(err) = self.check_mint_quote_paid(&quote.id).await {
tracing::error!("Could not check status of {}, {}", quote.id, err);
}
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/cdk/src/mint/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl Mint {
output_verification.unit,
input_verification.unit
);
return Err(Error::MultipleUnits);
return Err(Error::UnitMismatch);
}

let fees = self.get_proofs_fee(inputs).await?;
Expand Down
Loading