Bladeren bron

fix: lnbits payment check and units (#981)

* fix: lnbits payment check and units

---------

Co-authored-by: asmo <asmogo@protonmail.com>
thesimplekid 1 maand geleden
bovenliggende
commit
aa624d3afd
5 gewijzigde bestanden met toevoegingen van 68 en 20 verwijderingen
  1. 1 0
      crates/cdk-lnbits/Cargo.toml
  2. 24 13
      crates/cdk-lnbits/src/lib.rs
  3. 16 2
      crates/cdk/src/mint/issue/mod.rs
  4. 10 2
      crates/cdk/src/mint/ln.rs
  5. 17 3
      crates/cdk/src/mint/mod.rs

+ 1 - 0
crates/cdk-lnbits/Cargo.toml

@@ -23,3 +23,4 @@ tracing.workspace = true
 thiserror.workspace = true
 lnbits-rs = "0.6.0"
 serde_json.workspace = true
+rustls.workspace = true

+ 24 - 13
crates/cdk-lnbits/src/lib.rs

@@ -72,6 +72,9 @@ impl LNbits {
 
     /// Subscribe to lnbits ws
     pub async fn subscribe_ws(&self) -> Result<(), Error> {
+        if rustls::crypto::CryptoProvider::get_default().is_none() {
+            let _ = rustls::crypto::ring::default_provider().install_default();
+        }
         self.lnbits_api
             .subscribe_to_websocket()
             .await
@@ -134,7 +137,7 @@ impl MintPayment for LNbits {
                                                             let response = WaitPaymentResponse {
                                                                 payment_identifier: PaymentIdentifier::PaymentHash(hash),
                                                                 payment_amount: Amount::from(payment.details.amount as u64),
-                                                                unit: CurrencyUnit::Sat,
+                                                                unit: CurrencyUnit::Msat,
                                                                 payment_id: msg.clone()
                                                             };
                                                             Some((response, (api, cancel_token, is_active)))
@@ -247,9 +250,9 @@ impl MintPayment for LNbits {
                     })?;
 
                 let status = if invoice_info.paid {
-                    MeltQuoteState::Unpaid
-                } else {
                     MeltQuoteState::Paid
+                } else {
+                    MeltQuoteState::Unpaid
                 };
 
                 let total_spent = Amount::from(
@@ -271,7 +274,7 @@ impl MintPayment for LNbits {
                     payment_proof: Some(invoice_info.details.payment_hash),
                     status,
                     total_spent,
-                    unit: CurrencyUnit::Sat,
+                    unit: CurrencyUnit::Msat,
                 })
             }
             OutgoingPaymentOptions::Bolt12(_) => {
@@ -352,12 +355,21 @@ impl MintPayment for LNbits {
                 Self::Err::Anyhow(anyhow!("Could not check invoice status"))
             })?;
 
-        Ok(vec![WaitPaymentResponse {
-            payment_identifier: payment_identifier.clone(),
-            payment_amount: Amount::from(payment.details.amount as u64),
-            unit: CurrencyUnit::Sat,
-            payment_id: payment.details.payment_hash,
-        }])
+        let amount = payment.details.amount;
+
+        if amount == i64::MIN {
+            return Err(Error::AmountOverflow.into());
+        }
+
+        match payment.paid {
+            true => Ok(vec![WaitPaymentResponse {
+                payment_identifier: payment_identifier.clone(),
+                payment_amount: Amount::from(amount.unsigned_abs()),
+                unit: CurrencyUnit::Msat,
+                payment_id: payment.details.payment_hash,
+            }]),
+            false => Ok(vec![]),
+        }
     }
 
     async fn check_outgoing_payment(
@@ -379,10 +391,9 @@ impl MintPayment for LNbits {
             payment_proof: payment.preimage,
             status: lnbits_to_melt_status(&payment.details.status, payment.details.pending),
             total_spent: Amount::from(
-                payment.details.amount.unsigned_abs()
-                    + payment.details.fee.unsigned_abs() / MSAT_IN_SAT,
+                payment.details.amount.unsigned_abs() + payment.details.fee.unsigned_abs(),
             ),
-            unit: self.settings.unit.clone(),
+            unit: CurrencyUnit::Msat,
         };
 
         Ok(pay_response)

+ 16 - 2
crates/cdk/src/mint/issue/mod.rs

@@ -1,3 +1,4 @@
+use cdk_common::amount::to_unit;
 use cdk_common::mint::MintQuote;
 use cdk_common::payment::{
     Bolt11IncomingPaymentOptions, Bolt11Settings, Bolt12IncomingPaymentOptions,
@@ -410,8 +411,9 @@ impl Mint {
         wait_payment_response: WaitPaymentResponse,
     ) -> Result<(), Error> {
         tracing::debug!(
-            "Received payment notification of {} for mint quote {} with payment id {}",
+            "Received payment notification of {} {} for mint quote {} with payment id {}",
             wait_payment_response.payment_amount,
+            wait_payment_response.unit,
             mint_quote.id,
             wait_payment_response.payment_id.to_string()
         );
@@ -426,9 +428,21 @@ impl Mint {
             {
                 tracing::info!("Received payment notification for already seen payment.");
             } else {
+                let payment_amount_quote_unit = to_unit(
+                    wait_payment_response.payment_amount,
+                    &wait_payment_response.unit,
+                    &mint_quote.unit,
+                )?;
+
+                tracing::debug!(
+                    "Payment received amount in quote unit {} {}",
+                    mint_quote.unit,
+                    payment_amount_quote_unit
+                );
+
                 tx.increment_mint_quote_amount_paid(
                     &mint_quote.id,
-                    wait_payment_response.payment_amount,
+                    payment_amount_quote_unit,
                     wait_payment_response.payment_id,
                 )
                 .await?;

+ 10 - 2
crates/cdk/src/mint/ln.rs

@@ -46,8 +46,16 @@ impl Mint {
         let mut tx = self.localstore.begin_transaction().await?;
 
         for payment in ln_status {
-            if !quote.payment_ids().contains(&&payment.payment_id) {
-                tracing::debug!("Found payment for quote {} when checking.", quote.id);
+            if !quote.payment_ids().contains(&&payment.payment_id)
+                && payment.payment_amount > Amount::ZERO
+            {
+                tracing::debug!(
+                    "Found payment of {} {} for quote {} when checking.",
+                    payment.payment_amount,
+                    payment.unit,
+                    quote.id
+                );
+
                 let amount_paid = to_unit(payment.payment_amount, &payment.unit, &quote.unit)?;
 
                 quote.increment_amount_paid(amount_paid)?;

+ 17 - 3
crates/cdk/src/mint/mod.rs

@@ -5,6 +5,7 @@ use std::sync::Arc;
 use std::time::Duration;
 
 use arc_swap::ArcSwap;
+use cdk_common::amount::to_unit;
 use cdk_common::common::{PaymentProcessorKey, QuoteTTL};
 #[cfg(feature = "auth")]
 use cdk_common::database::MintAuthDatabase;
@@ -543,10 +544,11 @@ impl Mint {
         pubsub_manager: &Arc<PubSubManager>,
     ) -> Result<(), Error> {
         tracing::debug!(
-            "Received payment notification of {} for mint quote {} with payment id {}",
+            "Received payment notification of {} {} for mint quote {} with payment id {}",
             wait_payment_response.payment_amount,
+            wait_payment_response.unit,
             mint_quote.id,
-            wait_payment_response.payment_id
+            wait_payment_response.payment_id.to_string()
         );
 
         let quote_state = mint_quote.state();
@@ -559,9 +561,21 @@ impl Mint {
             {
                 tracing::info!("Received payment notification for already issued quote.");
             } else {
+                let payment_amount_quote_unit = to_unit(
+                    wait_payment_response.payment_amount,
+                    &wait_payment_response.unit,
+                    &mint_quote.unit,
+                )?;
+
+                tracing::debug!(
+                    "Payment received amount in quote unit {} {}",
+                    mint_quote.unit,
+                    payment_amount_quote_unit
+                );
+
                 tx.increment_mint_quote_amount_paid(
                     &mint_quote.id,
-                    wait_payment_response.payment_amount,
+                    payment_amount_quote_unit,
                     wait_payment_response.payment_id,
                 )
                 .await?;