Эх сурвалжийг харах

Fixed .get_keyset_fees_and_amounts_by_id and other functions

Make them accept an optional ongoing transaction
Cesar Rodas 1 сар өмнө
parent
commit
ec23b8c8c7

+ 1 - 1
crates/cdk-ffi/src/wallet.rs

@@ -369,7 +369,7 @@ impl Wallet {
             .map_err(|e| FfiError::Generic { msg: e.to_string() })?;
         Ok(self
             .inner
-            .get_keyset_fees_and_amounts_by_id(id)
+            .get_keyset_fees_and_amounts_by_id(id, None)
             .await?
             .fee())
     }

+ 1 - 1
crates/cdk/src/wallet/issue/issue_bolt11.rs

@@ -232,7 +232,7 @@ impl Wallet {
 
         let active_keyset_id = self.fetch_active_keyset(Some(&mut tx)).await?.id;
         let fee_and_amounts = self
-            .get_keyset_fees_and_amounts_by_id(active_keyset_id)
+            .get_keyset_fees_and_amounts_by_id(active_keyset_id, Some(&mut tx))
             .await?;
 
         let premint_secrets = match &spending_conditions {

+ 1 - 1
crates/cdk/src/wallet/issue/issue_bolt12.rs

@@ -106,7 +106,7 @@ impl Wallet {
 
         let active_keyset_id = self.fetch_active_keyset(Some(&mut tx)).await?.id;
         let fee_and_amounts = self
-            .get_keyset_fees_and_amounts_by_id(active_keyset_id)
+            .get_keyset_fees_and_amounts_by_id(active_keyset_id, Some(&mut tx))
             .await?;
 
         let amount = match amount {

+ 29 - 12
crates/cdk/src/wallet/keysets.rs

@@ -190,12 +190,21 @@ impl Wallet {
     /// Returns a HashMap of keyset IDs to their input fee rates (per-proof-per-thousand)
     /// from cached keysets in the local database. This is an offline operation that does
     /// not contact the mint. If no keysets are found locally, returns an error.
-    pub async fn get_keyset_fees_and_amounts(&self) -> Result<KeysetFeeAndAmounts, Error> {
-        let keysets = self
-            .localstore
-            .get_mint_keysets(self.mint_url.clone())
-            .await?
-            .ok_or(Error::UnknownKeySet)?;
+    pub async fn get_keyset_fees_and_amounts(
+        &self,
+        tx: Option<&mut Tx<'_, '_>>,
+    ) -> Result<KeysetFeeAndAmounts, Error> {
+        let mut tx = tx;
+        let keysets = if let Some(tx) = tx.as_mut() {
+            tx.get_mint_keysets(self.mint_url.clone())
+                .await?
+                .ok_or(Error::UnknownKeySet)?
+        } else {
+            self.localstore
+                .get_mint_keysets(self.mint_url.clone())
+                .await?
+                .ok_or(Error::UnknownKeySet)?
+        };
 
         let mut fees = HashMap::new();
         for keyset in keysets {
@@ -203,11 +212,18 @@ impl Wallet {
                 keyset.id,
                 (
                     keyset.input_fee_ppk,
-                    self.load_keyset_keys(keyset.id, None)
-                        .await?
-                        .iter()
-                        .map(|(amount, _)| amount.to_u64())
-                        .collect::<Vec<_>>(),
+                    self.load_keyset_keys(
+                        keyset.id,
+                        if let Some(tx) = tx.as_mut() {
+                            Some(*tx)
+                        } else {
+                            None
+                        },
+                    )
+                    .await?
+                    .iter()
+                    .map(|(amount, _)| amount.to_u64())
+                    .collect::<Vec<_>>(),
                 )
                     .into(),
             );
@@ -224,8 +240,9 @@ impl Wallet {
     pub async fn get_keyset_fees_and_amounts_by_id(
         &self,
         keyset_id: Id,
+        tx: Option<&mut Tx<'_, '_>>,
     ) -> Result<FeeAndAmounts, Error> {
-        self.get_keyset_fees_and_amounts()
+        self.get_keyset_fees_and_amounts(tx)
             .await?
             .get(&keyset_id)
             .cloned()

+ 1 - 1
crates/cdk/src/wallet/melt/melt_bolt11.rs

@@ -387,7 +387,7 @@ impl Wallet {
             .into_iter()
             .map(|k| k.id)
             .collect();
-        let keyset_fees = self.get_keyset_fees_and_amounts().await?;
+        let keyset_fees = self.get_keyset_fees_and_amounts(Some(&mut tx)).await?;
         let (mut input_proofs, mut exchange) = Wallet::select_exact_proofs(
             inputs_needed_amount,
             available_proofs,

+ 3 - 3
crates/cdk/src/wallet/send.rs

@@ -39,7 +39,7 @@ impl Wallet {
         }
 
         // Get keyset fees from localstore
-        let keyset_fees = self.get_keyset_fees_and_amounts().await?;
+        let keyset_fees = self.get_keyset_fees_and_amounts(None).await?;
 
         // Get available proofs matching conditions
         let mut available_proofs = self
@@ -133,7 +133,7 @@ impl Wallet {
         // Split amount with fee if necessary
         let active_keyset_id = self.get_active_keyset().await?.id;
         let fee_and_amounts = self
-            .get_keyset_fees_and_amounts_by_id(active_keyset_id)
+            .get_keyset_fees_and_amounts_by_id(active_keyset_id, None)
             .await?;
         let (send_amounts, send_fee) = if opts.include_fee {
             tracing::debug!("Keyset fee per proof: {:?}", fee_and_amounts.fee());
@@ -275,7 +275,7 @@ impl PreparedSend {
         // Get keyset fees
         let keyset_fee_ppk = self
             .wallet
-            .get_keyset_fees_and_amounts_by_id(active_keyset_id)
+            .get_keyset_fees_and_amounts_by_id(active_keyset_id, Some(&mut tx))
             .await?;
         tracing::debug!("Keyset fees: {:?}", keyset_fee_ppk);
 

+ 3 - 3
crates/cdk/src/wallet/swap.rs

@@ -45,7 +45,7 @@ impl Wallet {
 
         let active_keyset_id = pre_swap.pre_mint_secrets.keyset_id;
         let fee_and_amounts = self
-            .get_keyset_fees_and_amounts_by_id(active_keyset_id)
+            .get_keyset_fees_and_amounts_by_id(active_keyset_id, Some(&mut tx))
             .await?;
 
         let active_keys = self
@@ -181,7 +181,7 @@ impl Wallet {
             .map(|k| k.id)
             .collect();
 
-        let keyset_fees = self.get_keyset_fees_and_amounts().await?;
+        let keyset_fees = self.get_keyset_fees_and_amounts(Some(&mut tx)).await?;
         let proofs = Wallet::select_proofs(
             amount,
             available_proofs,
@@ -238,7 +238,7 @@ impl Wallet {
             .ok_or(Error::InsufficientFunds)?;
 
         let fee_and_amounts = self
-            .get_keyset_fees_and_amounts_by_id(active_keyset_id)
+            .get_keyset_fees_and_amounts_by_id(active_keyset_id, Some(tx))
             .await?;
 
         let (send_amount, change_amount) = match include_fees {