Cesar Rodas пре 2 недеља
родитељ
комит
d5f2b23026
2 измењених фајлова са 22 додато и 8 уклоњено
  1. 8 8
      crates/cdk/src/wallet/melt.rs
  2. 14 0
      crates/cdk/src/wallet/proofs.rs

+ 8 - 8
crates/cdk/src/wallet/melt.rs

@@ -323,7 +323,7 @@ impl Wallet {
         )?;
 
         if let Some((proof, exact_amount)) = exchange.take() {
-            if let Ok(Some(new_proofs)) = self
+            let new_proofs = self
                 .swap(
                     Some(exact_amount),
                     SplitTarget::None,
@@ -331,13 +331,13 @@ impl Wallet {
                     None,
                     false,
                 )
-                .await
-            {
-                input_proofs.extend_from_slice(&new_proofs);
-            } else {
-                // swap failed, add it back to the original set of profos
-                input_proofs.push(proof);
-            }
+                .await?
+                .ok_or_else(|| {
+                    tracing::error!("Received empty proofs");
+                    Error::Internal
+                })?;
+
+            input_proofs.extend_from_slice(&new_proofs);
         }
 
         self.melt_proofs(quote_id, input_proofs).await

+ 14 - 0
crates/cdk/src/wallet/proofs.rs

@@ -570,6 +570,20 @@ mod tests {
     }
 
     #[test]
+    fn test_select_proof_change() {
+        let proofs = vec![proof(64), proof(4), proof(32)];
+        let (selected_proofs, exchange) =
+            Wallet::select_exact_proofs(97.into(), proofs, &vec![id()], &HashMap::new(), false)
+                .unwrap();
+        assert!(exchange.is_some());
+        let (proof_to_exchange, amount) = exchange.unwrap();
+
+        assert_eq!(selected_proofs.len(), 2);
+        assert_eq!(proof_to_exchange.amount, 64.into());
+        assert_eq!(amount, 61.into());
+    }
+
+    #[test]
     fn test_select_proofs_huge_proofs() {
         let proofs = (0..32)
             .flat_map(|i| (0..5).map(|_| proof(1 << i)).collect::<Vec<_>>())