Skip to content
Open
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
1,000 changes: 814 additions & 186 deletions crates/cashu/src/amount.rs

Large diffs are not rendered by default.

66 changes: 33 additions & 33 deletions crates/cdk-cln/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::time::Duration;

use async_trait::async_trait;
use bitcoin::hashes::sha256::Hash;
use cdk_common::amount::{to_unit, Amount};
use cdk_common::amount::Amount;
use cdk_common::common::FeeReserve;
use cdk_common::database::DynKVStore;
use cdk_common::nuts::{CurrencyUnit, MeltOptions, MeltQuoteState};
Expand Down Expand Up @@ -267,9 +267,8 @@ impl MintPayment for Cln {

let response = WaitPaymentResponse {
payment_identifier: request_lookup_id,
payment_amount: amount_msats.msat().into(),
unit: CurrencyUnit::Msat,
payment_id: payment_hash.to_string()
payment_amount: Amount::new(amount_msats.msat(), CurrencyUnit::Msat),
payment_id: payment_hash.to_string(),
};
tracing::info!("CLN: Created WaitPaymentResponse with amount {} msats", amount_msats.msat());
let event = Event::PaymentReceived(response);
Expand Down Expand Up @@ -327,11 +326,12 @@ impl MintPayment for Cln {
.into()
};
// Convert to target unit
let amount = to_unit(amount_msat, &CurrencyUnit::Msat, unit)?;
let amount =
Amount::new(amount_msat.into(), CurrencyUnit::Msat).convert_to(unit)?;

// Calculate fee
let relative_fee_reserve =
(self.fee_reserve.percent_fee_reserve * u64::from(amount) as f32) as u64;
(self.fee_reserve.percent_fee_reserve * amount.value() as f32) as u64;
let absolute_fee_reserve: u64 = self.fee_reserve.min_fee_reserve.into();
let fee = max(relative_fee_reserve, absolute_fee_reserve);

Expand All @@ -340,9 +340,8 @@ impl MintPayment for Cln {
*bolt11_options.bolt11.payment_hash().as_ref(),
)),
amount,
fee: fee.into(),
fee: Amount::new(fee, unit.clone()),
state: MeltQuoteState::Unpaid,
unit: unit.clone(),
})
}
OutgoingPaymentOptions::Bolt12(bolt12_options) => {
Expand All @@ -361,20 +360,19 @@ impl MintPayment for Cln {
};

// Convert to target unit
let amount = to_unit(amount_msat, &CurrencyUnit::Msat, unit)?;
let amount = Amount::new(amount_msat, CurrencyUnit::Msat).convert_to(unit)?;

// Calculate fee
let relative_fee_reserve =
(self.fee_reserve.percent_fee_reserve * u64::from(amount) as f32) as u64;
(self.fee_reserve.percent_fee_reserve * amount.value() as f32) as u64;
let absolute_fee_reserve: u64 = self.fee_reserve.min_fee_reserve.into();
let fee = max(relative_fee_reserve, absolute_fee_reserve);

Ok(PaymentQuoteResponse {
request_lookup_id: None,
amount,
fee: fee.into(),
fee: Amount::new(fee, unit.clone()),
state: MeltQuoteState::Unpaid,
unit: unit.clone(),
})
}
}
Expand Down Expand Up @@ -505,15 +503,14 @@ impl MintPayment for Cln {
};

MakePaymentResponse {
payment_proof: Some(hex::encode(pay_response.payment_preimage.to_vec())),
payment_lookup_id: payment_identifier,
payment_proof: Some(hex::encode(pay_response.payment_preimage.to_vec())),
status,
total_spent: to_unit(
total_spent: Amount::new(
pay_response.amount_sent_msat.msat(),
&CurrencyUnit::Msat,
unit,
)?,
unit: unit.clone(),
CurrencyUnit::Msat,
)
.convert_to(unit)?,
}
}
Err(err) => {
Expand Down Expand Up @@ -543,8 +540,10 @@ impl MintPayment for Cln {

let label = Uuid::new_v4().to_string();

let amount = to_unit(amount, unit, &CurrencyUnit::Msat)?;
let amount_msat = AmountOrAny::Amount(CLN_Amount::from_msat(amount.into()));
let amount_converted =
Amount::new(amount.into(), unit.clone()).convert_to(&CurrencyUnit::Msat)?;
let amount_msat =
AmountOrAny::Amount(CLN_Amount::from_msat(amount_converted.value()));

let invoice_response = cln_client
.call_typed(&InvoiceRequest {
Expand Down Expand Up @@ -584,9 +583,10 @@ impl MintPayment for Cln {
// Match like this until we change to option
let amount = match amount {
Some(amount) => {
let amount = to_unit(amount, unit, &CurrencyUnit::Msat)?;
let amount = Amount::new(amount.into(), unit.clone())
.convert_to(&CurrencyUnit::Msat)?;

amount.to_string()
amount.value().to_string()
}
None => "any".to_string(),
};
Expand Down Expand Up @@ -690,13 +690,13 @@ impl MintPayment for Cln {
.filter(|p| p.amount_msat.is_some()) // Filter out invoices without an amount
.map(|p| WaitPaymentResponse {
payment_identifier: payment_identifier.clone(),
payment_amount: p
.amount_msat
// Safe to expect since we filtered for Some
.expect("We have filter out those without amounts")
.msat()
.into(),
unit: CurrencyUnit::Msat,
payment_amount: Amount::new(
p.amount_msat
// Safe to expect since we filtered for Some
.expect("We have filter out those without amounts")
.msat(),
CurrencyUnit::Msat,
),
payment_id: p.payment_hash.to_string(),
})
.collect())
Expand Down Expand Up @@ -740,16 +740,16 @@ impl MintPayment for Cln {
status,
total_spent: pays_response
.amount_sent_msat
.map_or(Amount::ZERO, |a| a.msat().into()),
unit: CurrencyUnit::Msat,
.map_or(Amount::new(0, CurrencyUnit::Msat), |a| {
Amount::new(a.msat(), CurrencyUnit::Msat)
}),
})
}
None => Ok(MakePaymentResponse {
payment_lookup_id: payment_identifier.clone(),
payment_proof: None,
status: MeltQuoteState::Unknown,
total_spent: Amount::ZERO,
unit: CurrencyUnit::Msat,
total_spent: Amount::new(0, CurrencyUnit::Msat),
}),
}
}
Expand Down
10 changes: 8 additions & 2 deletions crates/cdk-common/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ impl Melted {
);

let fee_paid = proofs_amount
.checked_sub(quote_amount + change_amount)
.checked_sub(
quote_amount
.checked_add(change_amount)
.ok_or(Error::AmountOverflow)?,
)
.ok_or(Error::AmountOverflow)?;

Ok(Self {
Expand All @@ -63,7 +67,9 @@ impl Melted {

/// Total amount melted
pub fn total_amount(&self) -> Amount {
self.amount + self.fee_paid
self.amount
.checked_add(self.fee_paid)
.expect("We check when calc fee paid")
}
}

Expand Down
8 changes: 4 additions & 4 deletions crates/cdk-common/src/database/mint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ pub use super::kvstore::{
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MeltRequestInfo {
/// Total amount of all input proofs in the melt request
pub inputs_amount: Amount,
pub inputs_amount: Amount<CurrencyUnit>,
/// Fee amount associated with the input proofs
pub inputs_fee: Amount,
pub inputs_fee: Amount<CurrencyUnit>,
/// Blinded messages for change outputs
pub change_outputs: Vec<BlindedMessage>,
}
Expand Down Expand Up @@ -85,8 +85,8 @@ pub trait QuotesTransaction {
async fn add_melt_request(
&mut self,
quote_id: &QuoteId,
inputs_amount: Amount,
inputs_fee: Amount,
inputs_amount: Amount<CurrencyUnit>,
inputs_fee: Amount<CurrencyUnit>,
) -> Result<(), Self::Err>;

/// Add blinded_messages for a quote_id
Expand Down
Loading