Skip to content

Commit

Permalink
test(wallet): add test to verify wallet::get_tx TransactionDetails am…
Browse files Browse the repository at this point in the history
…ounts"
  • Loading branch information
notmandatory committed Jul 27, 2023
1 parent 8f38e96 commit 51fe7f9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 11 deletions.
57 changes: 50 additions & 7 deletions crates/bdk/tests/common.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,53 @@
#![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(
descriptor: &str,
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
Expand All @@ -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) {
Expand Down
37 changes: 33 additions & 4 deletions crates/bdk/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<TransactionDetails> = 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();
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 51fe7f9

Please sign in to comment.