diff --git a/crates/bdk/tests/common.rs b/crates/bdk/tests/common.rs index de94670321..887e08bb54 100644 --- a/crates/bdk/tests/common.rs +++ b/crates/bdk/tests/common.rs @@ -1,8 +1,10 @@ #![allow(unused)] + use bdk::{wallet::AddressIndex, Wallet}; use bdk_chain::{BlockId, ConfirmationTime}; use bitcoin::hashes::Hash; -use bitcoin::{BlockHash, Network, Transaction, TxOut}; +use bitcoin::{Address, BlockHash, Network, OutPoint, Transaction, TxIn, TxOut}; +use std::str::FromStr; /// Return a fake wallet that appears to be funded for testing. pub fn get_funded_wallet_with_change( @@ -10,16 +12,42 @@ pub fn get_funded_wallet_with_change( change: Option<&str>, ) -> (Wallet, bitcoin::Txid) { let mut wallet = Wallet::new_no_persist(descriptor, change, Network::Regtest).unwrap(); - let address = wallet.get_address(AddressIndex::New).address; + let change_address = wallet.get_address(AddressIndex::New).address; + let sendto_address = + Address::from_str("tb1qeua3n9t076zntxj64cz7qywwtwxd0lwvmtcmtd").expect("address"); - let tx = Transaction { + let tx0 = Transaction { version: 1, lock_time: bitcoin::PackedLockTime(0), input: vec![], output: vec![TxOut { - value: 50_000, - script_pubkey: address.script_pubkey(), + value: 76_000, + script_pubkey: change_address.script_pubkey(), + }], + }; + + let tx1 = Transaction { + version: 1, + lock_time: bitcoin::PackedLockTime(0), + input: vec![TxIn { + previous_output: OutPoint { + txid: tx0.txid(), + vout: 0, + }, + script_sig: Default::default(), + sequence: Default::default(), + witness: Default::default(), }], + output: vec![ + TxOut { + value: 50_000, + script_pubkey: change_address.script_pubkey(), + }, + TxOut { + value: 25_000, + script_pubkey: sendto_address.script_pubkey(), + }, + ], }; wallet @@ -28,17 +56,32 @@ pub fn get_funded_wallet_with_change( hash: BlockHash::all_zeros(), }) .unwrap(); + wallet + .insert_checkpoint(BlockId { + height: 2_000, + hash: BlockHash::all_zeros(), + }) + .unwrap(); wallet .insert_tx( - tx.clone(), + tx0, ConfirmationTime::Confirmed { height: 1_000, time: 100, }, ) .unwrap(); + wallet + .insert_tx( + tx1.clone(), + ConfirmationTime::Confirmed { + height: 2_000, + time: 200, + }, + ) + .unwrap(); - (wallet, tx.txid()) + (wallet, tx1.txid()) } pub fn get_funded_wallet(descriptor: &str) -> (Wallet, bitcoin::Txid) { diff --git a/crates/bdk/tests/wallet.rs b/crates/bdk/tests/wallet.rs index 282a74fcb9..a228c5152e 100644 --- a/crates/bdk/tests/wallet.rs +++ b/crates/bdk/tests/wallet.rs @@ -4,12 +4,11 @@ use bdk::signer::{SignOptions, SignerError}; use bdk::wallet::coin_selection::LargestFirstCoinSelection; use bdk::wallet::AddressIndex::*; use bdk::wallet::{AddressIndex, AddressInfo, Balance, Wallet}; -use bdk::Error; use bdk::FeeRate; use bdk::KeychainKind; -use bdk_chain::BlockId; -use bdk_chain::ConfirmationTime; +use bdk::{Error, TransactionDetails}; use bdk_chain::COINBASE_MATURITY; +use bdk_chain::{BlockId, ConfirmationTime}; use bitcoin::hashes::Hash; use bitcoin::BlockHash; use bitcoin::Script; @@ -84,6 +83,36 @@ fn test_get_funded_wallet_balance() { assert_eq!(wallet.get_balance().confirmed, 50000); } +#[test] +fn test_get_funded_wallet_tx_details() { + let (wallet, _) = get_funded_wallet(get_test_wpkh()); + assert_eq!(wallet.get_balance().confirmed, 50000); + let mut tx_details: Vec = wallet + .transactions() + .map(|ct| { + wallet + .get_tx(ct.node.txid, false) + .expect("transaction details") + }) + .collect(); + tx_details.sort(); + + assert_eq!(tx_details.len(), 2); + assert_matches!( + tx_details.get(1), + Some(TransactionDetails { + received: 50_000, + sent: 76_000, + fee: Some(1000), + confirmation_time: ConfirmationTime::Confirmed { + height: 2000, + time: 200, + }, + .. + }) + ) +} + macro_rules! assert_fee_rate { ($psbt:expr, $fees:expr, $fee_rate:expr $( ,@dust_change $( $dust_change:expr )* )* $( ,@add_signature $( $add_signature:expr )* )* ) => ({ let psbt = $psbt.clone(); @@ -207,7 +236,7 @@ fn test_create_tx_default_locktime_is_last_sync_height() { // Since we never synced the wallet we don't have a last_sync_height // we could use to try to prevent fee sniping. We default to 0. - assert_eq!(psbt.unsigned_tx.lock_time.0, 1_000); + assert_eq!(psbt.unsigned_tx.lock_time.0, 2_000); } #[test]