Skip to content

Commit

Permalink
Minor rewrites for clarity in melt.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
ok300 authored and thesimplekid committed Oct 25, 2024
1 parent 419b1a3 commit c91297a
Showing 1 changed file with 18 additions and 40 deletions.
58 changes: 18 additions & 40 deletions crates/cdk/src/mint/melt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,20 @@ impl Mint {
return Err(Error::MeltingDisabled);
}

match nut05.get_settings(&unit, &method) {
Some(settings) => {
if settings
.max_amount
.map_or(false, |max_amount| amount > max_amount)
{
return Err(Error::AmountOutofLimitRange(
settings.min_amount.unwrap_or_default(),
settings.max_amount.unwrap_or_default(),
amount,
));
}

if settings
.min_amount
.map_or(false, |min_amount| amount < min_amount)
{
return Err(Error::AmountOutofLimitRange(
settings.min_amount.unwrap_or_default(),
settings.max_amount.unwrap_or_default(),
amount,
));
}
}
None => {
return Err(Error::UnitUnsupported);
}
let settings = nut05
.get_settings(&unit, &method)
.ok_or(Error::UnitUnsupported)?;

let is_above_max = matches!(settings.max_amount, Some(max) if amount > max);
let is_below_min = matches!(settings.min_amount, Some(min) if amount < min);
match is_above_max || is_below_min {
true => Err(Error::AmountOutofLimitRange(
settings.min_amount.unwrap_or_default(),
settings.max_amount.unwrap_or_default(),
amount,
)),
false => Ok(()),
}

Ok(())
}

/// Get melt bolt11 quote
Expand Down Expand Up @@ -252,17 +236,11 @@ impl Mint {
.await?;

match state {
MeltQuoteState::Unpaid | MeltQuoteState::Failed => (),
MeltQuoteState::Pending => {
return Err(Error::PendingQuote);
}
MeltQuoteState::Paid => {
return Err(Error::PaidQuote);
}
MeltQuoteState::Unknown => {
return Err(Error::UnknownPaymentState);
}
}
MeltQuoteState::Unpaid | MeltQuoteState::Failed => Ok(()),
MeltQuoteState::Pending => Err(Error::PendingQuote),
MeltQuoteState::Paid => Err(Error::PaidQuote),
MeltQuoteState::Unknown => Err(Error::UnknownPaymentState),
}?;

let ys = melt_request.inputs.ys()?;

Expand Down

0 comments on commit c91297a

Please sign in to comment.