Skip to content

Commit

Permalink
refactor(bdk): display CreateTxError::FeeRateTooLow in sat/vb
Browse files Browse the repository at this point in the history
Also modify a unit test `test_bump_fee_low_fee_rate` to
additionally assert the expected error message
  • Loading branch information
ValuedMammal committed Jan 31, 2024
1 parent d928fd3 commit 697286c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
6 changes: 4 additions & 2 deletions crates/bdk/src/wallet/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,10 @@ where
CreateTxError::FeeRateTooLow { required } => {
write!(
f,
"Fee rate too low: required {} sat/kwu",
required.to_sat_per_kwu()
// Note: alternate fmt as sat/vb (ceil) available in bitcoin-0.31
//"Fee rate too low: required {required:#}"
"Fee rate too low: required {} sat/vb",
crate::floating_rate!(required)
)
}
CreateTxError::NoUtxosSelected => {
Expand Down
11 changes: 11 additions & 0 deletions crates/bdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2560,6 +2560,17 @@ fn create_signers<E: IntoWalletDescriptor>(
Ok((signers, change_signers))
}

/// Transforms a [`FeeRate`] to `f64` with unit of sat/vb.
#[macro_export]
#[doc(hidden)]
macro_rules! floating_rate {
($rate:expr) => {{
use $crate::bitcoin::blockdata::constants::WITNESS_SCALE_FACTOR;
// sat_kwu / 250.0 -> sat_vb
$rate.to_sat_per_kwu() as f64 / ((1000 / WITNESS_SCALE_FACTOR) as f64)
}};
}

#[macro_export]
#[doc(hidden)]
/// Macro for getting a wallet for use in a doctest
Expand Down
16 changes: 13 additions & 3 deletions crates/bdk/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1494,7 +1494,6 @@ fn test_bump_fee_confirmed_tx() {
}

#[test]
#[should_panic(expected = "FeeRateTooLow")]
fn test_bump_fee_low_fee_rate() {
let (mut wallet, _) = get_funded_wallet(get_test_wpkh());
let addr = wallet.get_address(New);
Expand All @@ -1503,6 +1502,7 @@ fn test_bump_fee_low_fee_rate() {
.add_recipient(addr.script_pubkey(), 25_000)
.enable_rbf();
let psbt = builder.finish().unwrap();
let feerate = psbt.fee_rate().unwrap();

let tx = psbt.extract_tx();
let txid = tx.txid();
Expand All @@ -1512,8 +1512,18 @@ fn test_bump_fee_low_fee_rate() {
.unwrap();

let mut builder = wallet.build_fee_bump(txid).unwrap();
builder.fee_rate(FeeRate::from_sat_per_vb_unchecked(1));
builder.finish().unwrap();
builder.fee_rate(FeeRate::BROADCAST_MIN);
let res = builder.finish();
assert_matches!(
res,
Err(CreateTxError::FeeRateTooLow { .. }),
"expected FeeRateTooLow error"
);

let required = feerate.to_sat_per_kwu() + 250; // +1 sat/vb
let sat_vb = required as f64 / 250.0;
let expect = format!("Fee rate too low: required {} sat/vb", sat_vb);
assert_eq!(res.unwrap_err().to_string(), expect);
}

#[test]
Expand Down

0 comments on commit 697286c

Please sign in to comment.