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 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
10 changes: 9 additions & 1 deletion crates/cdk-cln/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,20 @@ impl MintLightning for Cln {
.amount_milli_satoshis()
.ok_or(Error::UnknownInvoiceAmount)?;

let amount = to_unit(
let mut amount = to_unit(
invoice_amount_msat,
&CurrencyUnit::Msat,
&melt_quote_request.unit,
)?;

if melt_quote_request.amount {
amount = to_unit(
melt_quote_request.amount,
&CurrencyUnit::Msat,
&melt_quote_request.unit,
)?;

}
let relative_fee_reserve =
(self.fee_reserve.percent_fee_reserve * u64::from(amount) as f32) as u64;

Expand Down
9 changes: 8 additions & 1 deletion crates/cdk-fake-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,19 @@ impl MintLightning for FakeWallet {
.amount_milli_satoshis()
.ok_or(Error::UnknownInvoiceAmount)?;

let amount = to_unit(
let mut amount = to_unit(
invoice_amount_msat,
&CurrencyUnit::Msat,
&melt_quote_request.unit,
)?;

if melt_quote_request.amount {
amount = to_unit(
melt_quote_request.amount,
&CurrencyUnit::Msat,
&melt_quote_request.unit,
)?;

let relative_fee_reserve =
(self.fee_reserve.percent_fee_reserve * u64::from(amount) as f32) as u64;

Expand Down
9 changes: 8 additions & 1 deletion crates/cdk-lnbits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,19 @@ impl MintLightning for LNbits {
.amount_milli_satoshis()
.ok_or(Error::UnknownInvoiceAmount)?;

let amount = to_unit(
let mut amount = to_unit(
invoice_amount_msat,
&CurrencyUnit::Msat,
&melt_quote_request.unit,
)?;

if melt_quote_request.amount {
amount = to_unit(
melt_quote_request.amount,
&CurrencyUnit::Msat,
&melt_quote_request.unit,
)?;

let relative_fee_reserve =
(self.fee_reserve.percent_fee_reserve * u64::from(amount) as f32) as u64;

Expand Down
9 changes: 8 additions & 1 deletion crates/cdk-lnd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,19 @@ impl MintLightning for Lnd {
.amount_milli_satoshis()
.ok_or(Error::UnknownInvoiceAmount)?;

let amount = to_unit(
let mut amount = to_unit(
invoice_amount_msat,
&CurrencyUnit::Msat,
&melt_quote_request.unit,
)?;

if melt_quote_request.amount {
amount = to_unit(
melt_quote_request.amount,
&CurrencyUnit::Msat,
&melt_quote_request.unit,
)?;

let relative_fee_reserve =
(self.fee_reserve.percent_fee_reserve * u64::from(amount) as f32) as u64;

Expand Down
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(true),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Changing it here just singles support for it but it doesn't actually add support for it. The ln backends need to be modified to support it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

can you explain further on "The ln backends need to be modified to support it." @thesimplekid

cln.clone(),
);
}
Expand All @@ -167,6 +168,7 @@ async fn main() -> anyhow::Result<()> {
unit,
PaymentMethod::Bolt11,
mint_melt_limits,
Some(true),
Arc::new(strike),
);
}
Expand All @@ -181,6 +183,7 @@ async fn main() -> anyhow::Result<()> {
CurrencyUnit::Sat,
PaymentMethod::Bolt11,
mint_melt_limits,
Some(true),
Arc::new(lnbits),
);
}
Expand All @@ -194,6 +197,7 @@ async fn main() -> anyhow::Result<()> {
CurrencyUnit::Sat,
PaymentMethod::Bolt11,
mint_melt_limits,
Some(true),
Arc::new(phd),
);
}
Expand All @@ -207,6 +211,7 @@ async fn main() -> anyhow::Result<()> {
CurrencyUnit::Sat,
PaymentMethod::Bolt11,
mint_melt_limits,
Some(true),
Arc::new(lnd),
);
}
Expand All @@ -224,6 +229,7 @@ async fn main() -> anyhow::Result<()> {
unit.clone(),
PaymentMethod::Bolt11,
mint_melt_limits,
Some(true),
fake.clone(),
);
}
Expand Down
9 changes: 8 additions & 1 deletion crates/cdk-phoenixd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,19 @@ impl MintLightning for Phoenixd {
.amount_milli_satoshis()
.ok_or(Error::UnknownInvoiceAmount)?;

let amount = to_unit(
let mut amount = to_unit(
invoice_amount_msat,
&CurrencyUnit::Msat,
&melt_quote_request.unit,
)?;

if melt_quote_request.amount {
amount = to_unit(
melt_quote_request.amount,
&CurrencyUnit::Msat,
&melt_quote_request.unit,
)?;

let relative_fee_reserve =
(self.fee_reserve.percent_fee_reserve * u64::from(amount) as f32) as u64;

Expand Down
8 changes: 7 additions & 1 deletion crates/cdk-strike/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,15 @@ impl MintLightning for Strike {

let fee = from_strike_amount(quote.lightning_network_fee, &melt_quote_request.unit)?;

let mut amount = from_strike_amount(quote.amount, &melt_quote_request.unit)?.into()?;

if melt_quote_request.amount {
amount = from_strike_amount(melt_quote_request.amount, &melt_quote_request.unit)?.into()
}

Ok(PaymentQuoteResponse {
request_lookup_id: quote.payment_quote_id,
amount: from_strike_amount(quote.amount, &melt_quote_request.unit)?.into(),
amount,
fee: fee.into(),
state: MeltQuoteState::Unpaid,
})
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_amountless: Option<bool>,
ln_backend: Arc<dyn MintLightning<Err = cdk_lightning::Error> + Send + Sync>,
) -> Self {
let ln_key = LnKey {
unit: unit.clone(),
method,
};
let support_amountless = support_amountless.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),
amountless: Some(support_amountless),
};
self.mint_info.nuts.nut05.methods.push(melt_method_settings);
self.mint_info.nuts.nut05.disabled = false;
Expand Down
5 changes: 5 additions & 0 deletions crates/cdk/src/mint/melt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ impl Mint {
.get_settings(&unit, &method)
.ok_or(Error::UnitUnsupported)?;

if !settings.amountless.unwrap_or_default() {
return Err(Error::AmountLessNotAllowed);
}
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 {
Expand All @@ -58,6 +61,7 @@ impl Mint {
request,
unit,
options: _,
..
} = melt_request;

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.

Expand Down Expand Up @@ -93,6 +97,7 @@ impl Mint {
Error::UnitUnsupported
})?;


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 amountless: Option<bool>,
}

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)),
amountless: Some(false),
};

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