]> Untitled Git - bdk/commitdiff
test(wallet): Add test_insert_tx_balance_and_utxos
authorvalued mammal <valuedmammal@protonmail.com>
Mon, 3 Jun 2024 20:41:00 +0000 (16:41 -0400)
committervalued mammal <valuedmammal@protonmail.com>
Sun, 30 Jun 2024 14:08:54 +0000 (10:08 -0400)
crates/wallet/tests/wallet.rs

index 0870375bb4b955d853020c28dbcd3c2f58f94ca1..5937e05be08e9c7920846ccfc90406288fe66050 100644 (file)
@@ -4069,3 +4069,45 @@ fn test_thread_safety() {
     fn thread_safe<T: Send + Sync>() {}
     thread_safe::<Wallet>(); // compiles only if true
 }
+
+#[test]
+fn test_insert_tx_balance_and_utxos() {
+    // creating many txs has no effect on the wallet's available utxos
+    let (mut wallet, _) = get_funded_wallet(get_test_tr_single_sig_xprv());
+    let addr = Address::from_str("bcrt1qc6fweuf4xjvz4x3gx3t9e0fh4hvqyu2qw4wvxm")
+        .unwrap()
+        .assume_checked();
+
+    let unspent: Vec<_> = wallet.list_unspent().collect();
+    assert!(!unspent.is_empty());
+
+    let balance = wallet.balance().total();
+    let fee = Amount::from_sat(143);
+    let amt = balance - fee;
+
+    for _ in 0..3 {
+        let mut builder = wallet.build_tx();
+        builder.add_recipient(addr.script_pubkey(), amt);
+        let mut psbt = builder.finish().unwrap();
+        assert!(wallet.sign(&mut psbt, SignOptions::default()).unwrap());
+        let tx = psbt.extract_tx().unwrap();
+        let _ = wallet.insert_tx(tx);
+    }
+    assert_eq!(wallet.list_unspent().collect::<Vec<_>>(), unspent);
+    assert_eq!(wallet.balance().confirmed, balance);
+
+    // manually setting a tx last_seen will consume the wallet's available utxos
+    let addr = Address::from_str("bcrt1qfjg5lv3dvc9az8patec8fjddrs4aqtauadnagr")
+        .unwrap()
+        .assume_checked();
+    let mut builder = wallet.build_tx();
+    builder.add_recipient(addr.script_pubkey(), amt);
+    let mut psbt = builder.finish().unwrap();
+    assert!(wallet.sign(&mut psbt, SignOptions::default()).unwrap());
+    let tx = psbt.extract_tx().unwrap();
+    let txid = tx.compute_txid();
+    let _ = wallet.insert_tx(tx);
+    wallet.insert_seen_at(txid, 2);
+    assert!(wallet.list_unspent().next().is_none());
+    assert_eq!(wallet.balance().total().to_sat(), 0);
+}