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

Melt token with amountless #468

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions crates/cdk-mintd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ async fn main() -> anyhow::Result<()> {
CurrencyUnit::Sat,
PaymentMethod::Bolt11,
mint_melt_limits,
Some(false),
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
cln.clone(),
);
}
Expand All @@ -167,6 +168,7 @@ async fn main() -> anyhow::Result<()> {
unit,
PaymentMethod::Bolt11,
mint_melt_limits,
Some(false),
Arc::new(strike),
);
}
Expand All @@ -181,6 +183,7 @@ async fn main() -> anyhow::Result<()> {
CurrencyUnit::Sat,
PaymentMethod::Bolt11,
mint_melt_limits,
Some(false),
Arc::new(lnbits),
);
}
Expand All @@ -194,6 +197,7 @@ async fn main() -> anyhow::Result<()> {
CurrencyUnit::Sat,
PaymentMethod::Bolt11,
mint_melt_limits,
Some(false),
Arc::new(phd),
);
}
Expand All @@ -207,6 +211,7 @@ async fn main() -> anyhow::Result<()> {
CurrencyUnit::Sat,
PaymentMethod::Bolt11,
mint_melt_limits,
Some(false),
Arc::new(lnd),
);
}
Expand All @@ -224,6 +229,7 @@ async fn main() -> anyhow::Result<()> {
unit.clone(),
PaymentMethod::Bolt11,
mint_melt_limits,
Some(false),
fake.clone(),
);
}
Expand Down
3 changes: 3 additions & 0 deletions crates/cdk/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub enum Error {
/// Amount overflow
#[error("Amount Overflow")]
AmountOverflow,
/// Amountless Invoice Not supported
#[error("Amount Less Invoice is not allowed")]
AmountLessNotAllowed,

// Mint Errors
/// Minting is disabled
Expand Down
3 changes: 3 additions & 0 deletions crates/cdk/src/mint/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,14 @@ impl MintBuilder {
unit: CurrencyUnit,
method: PaymentMethod,
limits: MintMeltLimits,
support_amount_less: Option<bool>,
ln_backend: Arc<dyn MintLightning<Err = cdk_lightning::Error> + Send + Sync>,
) -> Self {
let ln_key = LnKey {
unit: unit.clone(),
method,
};
let support_amount_less = support_amount_less.unwrap_or(false);

let mut ln = self.ln.unwrap_or_default();

Expand Down Expand Up @@ -150,6 +152,7 @@ impl MintBuilder {
unit,
min_amount: Some(limits.melt_min),
max_amount: Some(limits.melt_max),
amount_less: support_amount_less,
};
self.mint_info.nuts.nut05.methods.push(melt_method_settings);
self.mint_info.nuts.nut05.disabled = false;
Expand Down
34 changes: 34 additions & 0 deletions crates/cdk/src/mint/melt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@ impl Mint {
}
}

fn check_amount_less_invoice(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is mostly a duplicate of the one above it, the check for is amountless is supported can just be added there.

&self,
amount: Amount,
request_amount: Amount,
unit: CurrencyUnit,
method: PaymentMethod,
) -> Result<(), Error> {
let nut05 = &self.mint_info.nuts.nut05;

if nut05.disabled {
return Err(Error::MeltingDisabled);
}
let settings = nut05
.get_settings(&unit, &method)
.ok_or(Error::UnitUnsupported)?;

if settings.amount_less && amount != request_amount {
return Err(Error::AmountLessNotAllowed);
}
Ok(())
}

/// Get melt bolt11 quote
#[instrument(skip_all)]
pub async fn get_melt_bolt11_quote(
Expand All @@ -58,8 +80,12 @@ impl Mint {
request,
unit,
options: _,
amount,
} = melt_request;

let amount_less_amount = amount.unwrap_or_default();
// let amount_less_amount = amount.unwrap_or_else(|| Amount::new(0));

let amount = match melt_request.options {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is executed on an invoice without an amount defined it will return an error. If we get an invoice without an amount we need to use the once defined in the request.

Some(mpp_amount) => mpp_amount.amount,
None => {
Expand Down Expand Up @@ -93,6 +119,14 @@ impl Mint {
Error::UnitUnsupported
})?;

// check amountless (amount in meltbolt11reqiest) is equal to payment_quote.amount
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
self.check_amount_less_invoice(
amount_less_amount,
payment_quote.amount,
unit.clone(),
PaymentMethod::Bolt11,
)?;

let quote = MeltQuote::new(
request.to_string(),
unit.clone(),
Expand Down
8 changes: 8 additions & 0 deletions crates/cdk/src/nuts/nut05.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub struct MeltQuoteBolt11Request {
pub request: Bolt11Invoice,
/// Unit wallet would like to pay with
pub unit: CurrencyUnit,
/// amount for paying amountless bolt11 invoice
pub amount: Option<Amount>,
/// Payment Options
pub options: Option<Mpp>,
}
Expand Down Expand Up @@ -264,6 +266,8 @@ pub struct MeltMethodSettings {
/// Max Amount
#[serde(skip_serializing_if = "Option::is_none")]
pub max_amount: Option<Amount>,
/// Amountless
pub amount_less: bool,
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
}

impl Settings {
Expand All @@ -288,6 +292,9 @@ impl Settings {
}
}

/// meting with an amount_less invoice
// pub amount_less: Option<Amount>,

/// Melt Settings
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "swagger", derive(utoipa::ToSchema), schema(as = nut05::Settings))]
Expand All @@ -305,6 +312,7 @@ impl Default for Settings {
unit: CurrencyUnit::Sat,
min_amount: Some(Amount::from(1)),
max_amount: Some(Amount::from(1000000)),
amount_less: false,
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
};

Settings {
Expand Down
1 change: 1 addition & 0 deletions crates/cdk/src/wallet/melt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ impl Wallet {
request: Bolt11Invoice::from_str(&request)?,
unit: self.unit.clone(),
options,
amount: Some(amount),
};

let quote_res = self
Expand Down
Loading