Skip to content

Commit 703f3db

Browse files
committed
test: Add test_create_psbt_utxo_filter
1 parent 83be977 commit 703f3db

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

tests/psbt.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,55 @@ fn test_replace_by_fee() {
314314
}
315315
}
316316

317+
#[test]
318+
fn test_create_psbt_utxo_filter() {
319+
let (desc, change_desc) = get_test_tr_single_sig_xprv_and_change_desc();
320+
let mut wallet = Wallet::create(desc, change_desc)
321+
.network(Network::Regtest)
322+
.create_wallet_no_persist()
323+
.unwrap();
324+
325+
let anchor = ConfirmationBlockTime {
326+
block_id: BlockId {
327+
height: 1000,
328+
hash: Hash::hash(b"1000"),
329+
},
330+
confirmation_time: 1234567,
331+
};
332+
insert_checkpoint(&mut wallet, anchor.block_id);
333+
334+
for value in [200, 300, 600, 1000] {
335+
let _ = receive_output(
336+
&mut wallet,
337+
Amount::from_sat(value),
338+
ReceiveTo::Block(anchor),
339+
);
340+
}
341+
assert_eq!(wallet.list_unspent().count(), 4);
342+
assert_eq!(wallet.balance().total().to_sat(), 2100);
343+
344+
let mut params = PsbtParams::default();
345+
params.feerate(FeeRate::ZERO);
346+
// Avoid selection of dust utxos
347+
params.filter_utxos(|txo| {
348+
let min_non_dust = txo.txout.script_pubkey.minimal_non_dust(); // 330
349+
txo.txout.value >= min_non_dust
350+
});
351+
params.change_descriptor({
352+
let internal_desc = wallet.public_descriptor(KeychainKind::Internal);
353+
internal_desc.at_derivation_index(0).unwrap()
354+
});
355+
params.drain_wallet();
356+
let (psbt, _) = wallet.create_psbt(params).unwrap();
357+
assert_eq!(psbt.unsigned_tx.input.len(), 2);
358+
assert_eq!(psbt.unsigned_tx.output.len(), 1);
359+
assert_eq!(
360+
psbt.unsigned_tx.output[0].value.to_sat(),
361+
1600,
362+
"We should have selected 2 non-dust utxos"
363+
);
364+
}
365+
317366
#[test]
318367
#[should_panic(expected = "InputIndexOutOfRange")]
319368
fn test_psbt_malformed_psbt_input_legacy() {

0 commit comments

Comments
 (0)