Skip to content

chore: use bitcoin::Amount instead of u64 on tests #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions wallet/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ impl From<ConfirmationBlockTime> for ReceiveTo {
}

/// Receive a tx output with the given value in the latest block
pub fn receive_output_in_latest_block(wallet: &mut Wallet, value: u64) -> OutPoint {
pub fn receive_output_in_latest_block(wallet: &mut Wallet, value: Amount) -> OutPoint {
let latest_cp = wallet.latest_checkpoint();
let height = latest_cp.height();
assert!(height > 0, "cannot receive tx into genesis block");
Expand All @@ -263,7 +263,7 @@ pub fn receive_output_in_latest_block(wallet: &mut Wallet, value: u64) -> OutPoi
/// Receive a tx output with the given value and chain position
pub fn receive_output(
wallet: &mut Wallet,
value: u64,
value: Amount,
receive_to: impl Into<ReceiveTo>,
) -> OutPoint {
let addr = wallet.next_unused_address(KeychainKind::External).address;
Expand All @@ -274,7 +274,7 @@ pub fn receive_output(
pub fn receive_output_to_address(
wallet: &mut Wallet,
addr: Address,
value: u64,
value: Amount,
receive_to: impl Into<ReceiveTo>,
) -> OutPoint {
let tx = Transaction {
Expand All @@ -283,7 +283,7 @@ pub fn receive_output_to_address(
input: vec![],
output: vec![TxOut {
script_pubkey: addr.script_pubkey(),
value: Amount::from_sat(value),
value,
}],
};

Expand Down
3 changes: 2 additions & 1 deletion wallet/src/wallet/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ impl FullyNodedExport {
#[cfg(test)]
mod test {
use alloc::string::ToString;
use bitcoin::Amount;
use core::str::FromStr;

use bdk_chain::BlockId;
Expand All @@ -233,7 +234,7 @@ mod test {
hash: BlockHash::all_zeros(),
};
insert_checkpoint(&mut wallet, block);
receive_output_in_latest_block(&mut wallet, 10_000);
receive_output_in_latest_block(&mut wallet, Amount::from_sat(10_000));

wallet
}
Expand Down
2 changes: 1 addition & 1 deletion wallet/src/wallet/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ mod test {
},
);

receive_output_in_latest_block(&mut wallet, Amount::ONE_BTC.to_sat());
receive_output_in_latest_block(&mut wallet, Amount::ONE_BTC);

// tx1 sending 15k sat to a recipient
let recip = ScriptBuf::from_hex(
Expand Down
50 changes: 25 additions & 25 deletions wallet/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1427,12 +1427,12 @@ fn test_create_tx_increment_change_index() {
descriptor: &'static str,
change_descriptor: Option<&'static str>,
// amount to send
to_send: u64,
to_send: Amount,
// (derivation index, next unused index) of *change keychain*
expect: (Option<u32>, u32),
}
// total wallet funds
let amount = 10_000;
let amount = Amount::from_sat(10_000);
let recipient = Address::from_str("bcrt1q3qtze4ys45tgdvguj66zrk4fu6hq3a3v9pfly5")
.unwrap()
.assume_checked()
Expand All @@ -1443,55 +1443,55 @@ fn test_create_tx_increment_change_index() {
name: "two wildcard, builder error",
descriptor: desc,
change_descriptor: Some(change_desc),
to_send: amount + 1,
to_send: amount + Amount::from_sat(1),
// should not use or derive change index
expect: (None, 0),
},
TestCase {
name: "two wildcard, create change",
descriptor: desc,
change_descriptor: Some(change_desc),
to_send: 5_000,
to_send: Amount::from_sat(5_000),
// should use change index
expect: (Some(0), 1),
},
TestCase {
name: "two wildcard, no change",
descriptor: desc,
change_descriptor: Some(change_desc),
to_send: 9_850,
to_send: Amount::from_sat(9_850),
// should not use change index
expect: (None, 0),
},
TestCase {
name: "one wildcard, create change",
descriptor: desc,
change_descriptor: None,
to_send: 5_000,
to_send: Amount::from_sat(5_000),
// should use change index of external keychain
expect: (Some(1), 2),
},
TestCase {
name: "one wildcard, no change",
descriptor: desc,
change_descriptor: None,
to_send: 9_850,
to_send: Amount::from_sat(9_850),
// should not use change index
expect: (Some(0), 1),
},
TestCase {
name: "single key, create change",
descriptor: get_test_tr_single_sig(),
change_descriptor: None,
to_send: 5_000,
to_send: Amount::from_sat(5_000),
// single key only has one derivation index (0)
expect: (Some(0), 0),
},
TestCase {
name: "single key, no change",
descriptor: get_test_tr_single_sig(),
change_descriptor: None,
to_send: 9_850,
to_send: Amount::from_sat(9_850),
expect: (Some(0), 0),
},
]
Expand All @@ -1516,7 +1516,7 @@ fn test_create_tx_increment_change_index() {
receive_output(&mut wallet, amount, ReceiveTo::Mempool(0));
// create tx
let mut builder = wallet.build_tx();
builder.add_recipient(recipient.clone(), Amount::from_sat(test.to_send));
builder.add_recipient(recipient.clone(), test.to_send);
let res = builder.finish();
if !test.name.contains("error") {
assert!(res.is_ok());
Expand Down Expand Up @@ -2289,7 +2289,7 @@ fn test_bump_fee_add_input() {
#[test]
fn test_bump_fee_absolute_add_input() {
let (mut wallet, _) = get_funded_wallet_wpkh();
receive_output_in_latest_block(&mut wallet, 25_000);
receive_output_in_latest_block(&mut wallet, Amount::from_sat(25_000));
let addr = Address::from_str("2N1Ffz3WaNzbeLFBb51xyFMHYSEUXcbiSoX")
.unwrap()
.assume_checked();
Expand Down Expand Up @@ -2343,7 +2343,7 @@ fn test_bump_fee_absolute_add_input() {
#[test]
fn test_bump_fee_no_change_add_input_and_change() {
let (mut wallet, _) = get_funded_wallet_wpkh();
let op = receive_output_in_latest_block(&mut wallet, 25_000);
let op = receive_output_in_latest_block(&mut wallet, Amount::from_sat(25_000));

// initially make a tx without change by using `drain_to`
let addr = Address::from_str("2N1Ffz3WaNzbeLFBb51xyFMHYSEUXcbiSoX")
Expand Down Expand Up @@ -2409,7 +2409,7 @@ fn test_bump_fee_no_change_add_input_and_change() {
#[test]
fn test_bump_fee_add_input_change_dust() {
let (mut wallet, _) = get_funded_wallet_wpkh();
receive_output_in_latest_block(&mut wallet, 25_000);
receive_output_in_latest_block(&mut wallet, Amount::from_sat(25_000));
let addr = Address::from_str("2N1Ffz3WaNzbeLFBb51xyFMHYSEUXcbiSoX")
.unwrap()
.assume_checked();
Expand Down Expand Up @@ -2481,7 +2481,7 @@ fn test_bump_fee_add_input_change_dust() {
#[test]
fn test_bump_fee_force_add_input() {
let (mut wallet, _) = get_funded_wallet_wpkh();
let incoming_op = receive_output_in_latest_block(&mut wallet, 25_000);
let incoming_op = receive_output_in_latest_block(&mut wallet, Amount::from_sat(25_000));

let addr = Address::from_str("2N1Ffz3WaNzbeLFBb51xyFMHYSEUXcbiSoX")
.unwrap()
Expand Down Expand Up @@ -2543,7 +2543,7 @@ fn test_bump_fee_force_add_input() {
#[test]
fn test_bump_fee_absolute_force_add_input() {
let (mut wallet, _) = get_funded_wallet_wpkh();
let incoming_op = receive_output_in_latest_block(&mut wallet, 25_000);
let incoming_op = receive_output_in_latest_block(&mut wallet, Amount::from_sat(25_000));

let addr = Address::from_str("2N1Ffz3WaNzbeLFBb51xyFMHYSEUXcbiSoX")
.unwrap()
Expand Down Expand Up @@ -2622,7 +2622,7 @@ fn test_bump_fee_unconfirmed_inputs_only() {
let psbt = builder.finish().unwrap();
// Now we receive one transaction with 0 confirmations. We won't be able to use that for
// fee bumping, as it's still unconfirmed!
receive_output(&mut wallet, 25_000, ReceiveTo::Mempool(0));
receive_output(&mut wallet, Amount::from_sat(25_000), ReceiveTo::Mempool(0));
let mut tx = psbt.extract_tx().expect("failed to extract tx");
let txid = tx.compute_txid();
for txin in &mut tx.input {
Expand All @@ -2647,7 +2647,7 @@ fn test_bump_fee_unconfirmed_input() {
.assume_checked();
// We receive a tx with 0 confirmations, which will be used as an input
// in the drain tx.
receive_output(&mut wallet, 25_000, ReceiveTo::Mempool(0));
receive_output(&mut wallet, Amount::from_sat(25_000), ReceiveTo::Mempool(0));
let mut builder = wallet.build_tx();
builder.drain_wallet().drain_to(addr.script_pubkey());
let psbt = builder.finish().unwrap();
Expand Down Expand Up @@ -2685,7 +2685,7 @@ fn test_fee_amount_negative_drain_val() {
.unwrap()
.assume_checked();
let fee_rate = FeeRate::from_sat_per_kwu(500);
let incoming_op = receive_output_in_latest_block(&mut wallet, 8859);
let incoming_op = receive_output_in_latest_block(&mut wallet, Amount::from_sat(8859));

let mut builder = wallet.build_tx();
builder
Expand Down Expand Up @@ -3057,7 +3057,7 @@ fn test_next_unused_address() {
assert_eq!(next_unused_addr.index, 0);

// use the above address
receive_output(&mut wallet, 25_000, ReceiveTo::Mempool(0));
receive_output(&mut wallet, Amount::from_sat(25_000), ReceiveTo::Mempool(0));

assert_eq!(
wallet
Expand Down Expand Up @@ -4133,7 +4133,7 @@ fn test_keychains_with_overlapping_spks() {
},
confirmation_time: 0,
};
let _outpoint = receive_output_to_address(&mut wallet, addr, 8000, anchor);
let _outpoint = receive_output_to_address(&mut wallet, addr, Amount::from_sat(8000), anchor);
assert_eq!(wallet.balance().confirmed, Amount::from_sat(58000));
}

Expand Down Expand Up @@ -4222,14 +4222,14 @@ fn single_descriptor_wallet_can_create_tx_and_receive_change() {
.create_wallet_no_persist()
.unwrap();
assert_eq!(wallet.keychains().count(), 1);
let amt = Amount::from_sat(5_000);
receive_output(&mut wallet, 2 * amt.to_sat(), ReceiveTo::Mempool(2));
let amount = Amount::from_sat(5_000);
receive_output(&mut wallet, amount * 2, ReceiveTo::Mempool(2));
// create spend tx that produces a change output
let addr = Address::from_str("bcrt1qc6fweuf4xjvz4x3gx3t9e0fh4hvqyu2qw4wvxm")
.unwrap()
.assume_checked();
let mut builder = wallet.build_tx();
builder.add_recipient(addr.script_pubkey(), amt);
builder.add_recipient(addr.script_pubkey(), amount);
let mut psbt = builder.finish().unwrap();
assert!(wallet.sign(&mut psbt, SignOptions::default()).unwrap());
let tx = psbt.extract_tx().unwrap();
Expand All @@ -4238,7 +4238,7 @@ fn single_descriptor_wallet_can_create_tx_and_receive_change() {
let unspent: Vec<_> = wallet.list_unspent().collect();
assert_eq!(unspent.len(), 1);
let utxo = unspent.first().unwrap();
assert!(utxo.txout.value < amt);
assert!(utxo.txout.value < amount);
assert_eq!(
utxo.keychain,
KeychainKind::External,
Expand All @@ -4249,7 +4249,7 @@ fn single_descriptor_wallet_can_create_tx_and_receive_change() {
#[test]
fn test_transactions_sort_by() {
let (mut wallet, _txid) = get_funded_wallet_wpkh();
receive_output(&mut wallet, 25_000, ReceiveTo::Mempool(0));
receive_output(&mut wallet, Amount::from_sat(25_000), ReceiveTo::Mempool(0));

// sort by chain position, unconfirmed then confirmed by descending block height
let sorted_txs: Vec<WalletTx> =
Expand Down
Loading