Jelajahi Sumber

fix: make fee needs to be converted in the payment backend (#1614)

tsk 1 Minggu lalu
induk
melakukan
1f084ed34d
3 mengubah file dengan 41 tambahan dan 11 penghapusan
  1. 19 2
      crates/cdk-cln/src/lib.rs
  2. 3 0
      crates/cdk-lnd/src/error.rs
  3. 19 9
      crates/cdk-lnd/src/lib.rs

+ 19 - 2
crates/cdk-cln/src/lib.rs

@@ -422,7 +422,14 @@ impl MintPayment for Cln {
                     }
                 }
 
-                max_fee_msat = bolt11_options.max_fee_amount.map(|a| a.into());
+                max_fee_msat = bolt11_options
+                    .max_fee_amount
+                    .map(|a| {
+                        Amount::new(a.into(), unit.clone())
+                            .convert_to(&CurrencyUnit::Msat)
+                            .map(|a| a.value())
+                    })
+                    .transpose()?;
 
                 bolt11_options.bolt11.to_string()
             }
@@ -477,7 +484,14 @@ impl MintPayment for Cln {
 
                 self.check_outgoing_unpaided(&payment_identifier).await?;
 
-                max_fee_msat = bolt12_options.max_fee_amount.map(|a| a.into());
+                max_fee_msat = bolt12_options
+                    .max_fee_amount
+                    .map(|a| {
+                        Amount::new(a.into(), unit.clone())
+                            .convert_to(&CurrencyUnit::Msat)
+                            .map(|a| a.value())
+                    })
+                    .transpose()?;
 
                 cln_response.invoice
             }
@@ -489,6 +503,9 @@ impl MintPayment for Cln {
         if invoice.is_empty() {
             return Err(Error::UnknownInvoice.into());
         }
+
+        tracing::debug!("Attempting payment with max fee: {:?}", max_fee_msat);
+
         let cln_response = cln_client
             .call_typed(&PayRequest {
                 bolt11: invoice,

+ 3 - 0
crates/cdk-lnd/src/error.rs

@@ -6,6 +6,9 @@ use tonic::Status;
 /// LND Error
 #[derive(Debug, Error)]
 pub enum Error {
+    /// Amount Error
+    #[error(transparent)]
+    Amount(#[from] cdk_common::amount::Error),
     /// Invoice amount not defined
     #[error("Unknown invoice amount")]
     UnknownInvoiceAmount,

+ 19 - 9
crates/cdk-lnd/src/lib.rs

@@ -398,7 +398,7 @@ impl MintPayment for Lnd {
     #[instrument(skip_all)]
     async fn make_payment(
         &self,
-        _unit: &CurrencyUnit,
+        unit: &CurrencyUnit,
         options: OutgoingPaymentOptions,
     ) -> Result<MakePaymentResponse, Self::Err> {
         match options {
@@ -446,10 +446,15 @@ impl MintPayment for Lnd {
                                 let route_req = lnrpc::QueryRoutesRequest {
                                     pub_key: hex::encode(pub_key.serialize()),
                                     amt_msat: u64::from(partial_amount_msat) as i64,
-                                    fee_limit: max_fee.map(|f| {
-                                        let limit = Limit::Fixed(u64::from(f) as i64);
-                                        FeeLimit { limit: Some(limit) }
-                                    }),
+                                    fee_limit: max_fee
+                                        .map(|f| {
+                                            let fee_msat = Amount::new(f.into(), unit.clone())
+                                                .convert_to(&CurrencyUnit::Msat)?
+                                                .value();
+                                            let limit = Limit::FixedMsat(fee_msat as i64);
+                                            Ok::<_, Error>(FeeLimit { limit: Some(limit) })
+                                        })
+                                        .transpose()?,
                                     use_mission_control: true,
                                     ..Default::default()
                                 };
@@ -542,10 +547,15 @@ impl MintPayment for Lnd {
 
                         let pay_req = lnrpc::SendRequest {
                             payment_request: bolt11.to_string(),
-                            fee_limit: max_fee.map(|f| {
-                                let limit = Limit::Fixed(u64::from(f) as i64);
-                                FeeLimit { limit: Some(limit) }
-                            }),
+                            fee_limit: max_fee
+                                .map(|f| {
+                                    let fee_msat = Amount::new(f.into(), unit.clone())
+                                        .convert_to(&CurrencyUnit::Msat)?
+                                        .value();
+                                    let limit = Limit::FixedMsat(fee_msat as i64);
+                                    Ok::<_, Error>(FeeLimit { limit: Some(limit) })
+                                })
+                                .transpose()?,
                             amt_msat: amount_msat as i64,
                             ..Default::default()
                         };