Browse Source

fix: lnd check total total amount

thesimplekid 6 months ago
parent
commit
84bc9efbf2
2 changed files with 18 additions and 7 deletions
  1. 4 1
      crates/cdk-lnd/src/error.rs
  2. 14 6
      crates/cdk-lnd/src/lib.rs

+ 4 - 1
crates/cdk-lnd/src/error.rs

@@ -11,9 +11,12 @@ pub enum Error {
     /// Unknown invoice
     #[error("Unknown invoice")]
     UnknownInvoice,
-    /// Connection Error
+    /// Connection error
     #[error("LND connection error")]
     Connection,
+    /// Payment failed
+    #[error("LND payment failed")]
+    PaymentFailed,
 }
 
 impl From<Error> for cdk::cdk_lightning::Error {

+ 14 - 6
crates/cdk-lnd/src/lib.rs

@@ -189,19 +189,27 @@ impl MintLightning for Lnd {
             .lightning()
             .send_payment_sync(fedimint_tonic_lnd::tonic::Request::new(pay_req))
             .await
-            .unwrap()
+            .map_err(|_| Error::PaymentFailed)?
             .into_inner();
 
-        let total_spent = payment_response
+        let total_amount = payment_response
             .payment_route
-            .map_or(0, |route| route.total_fees_msat / MSAT_IN_SAT as i64)
+            .map_or(0, |route| route.total_amt_msat / MSAT_IN_SAT as i64)
             as u64;
 
+        let (status, payment_preimage) = match total_amount == 0 {
+            true => (MeltQuoteState::Unpaid, None),
+            false => (
+                MeltQuoteState::Paid,
+                Some(hex::encode(payment_response.payment_preimage)),
+            ),
+        };
+
         Ok(PayInvoiceResponse {
             payment_hash: hex::encode(payment_response.payment_hash),
-            payment_preimage: Some(hex::encode(payment_response.payment_preimage)),
-            status: MeltQuoteState::Paid,
-            total_spent: total_spent.into(),
+            payment_preimage,
+            status,
+            total_spent: total_amount.into(),
             unit: CurrencyUnit::Sat,
         })
     }