Skip to content

Commit 8c8f050

Browse files
committed
refactor(wallet)!: Remove CreateTxError::Bdk variant and unused bdk::Error variants
1 parent 5398d15 commit 8c8f050

File tree

5 files changed

+287
-167
lines changed

5 files changed

+287
-167
lines changed

crates/bdk/src/error.rs

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,6 @@ use core::fmt;
2020
pub enum Error {
2121
/// Generic error
2222
Generic(String),
23-
/// Cannot build a tx without recipients
24-
NoRecipients,
25-
/// `manually_selected_only` option is selected but no utxo has been passed
26-
NoUtxosSelected,
27-
/// Output created is under the dust limit, 546 satoshis
28-
OutputBelowDustLimit(usize),
29-
/// Wallet's UTXO set is not enough to cover recipient's requested plus fee
30-
InsufficientFunds {
31-
/// Sats needed for some transaction
32-
needed: u64,
33-
/// Sats available for spending
34-
available: u64,
35-
},
36-
/// Branch and bound coin selection possible attempts with sufficiently big UTXO set could grow
37-
/// exponentially, thus a limit is set, and when hit, this error is thrown
38-
BnBTotalTriesExceeded,
39-
/// Branch and bound coin selection tries to avoid needing a change by finding the right inputs for
40-
/// the desired outputs plus fee, if there is not such combination this error is thrown
41-
BnBNoExactMatch,
4223
/// Happens when trying to spend an UTXO that is not in the internal database
4324
UnknownUtxo,
4425
/// Thrown when a tx is not found in the internal database
@@ -47,32 +28,12 @@ pub enum Error {
4728
TransactionConfirmed,
4829
/// Trying to replace a tx that has a sequence >= `0xFFFFFFFE`
4930
IrreplaceableTransaction,
50-
/// When bumping a tx the fee rate requested is lower than required
51-
FeeRateTooLow {
52-
/// Required fee rate (satoshi/vbyte)
53-
required: crate::types::FeeRate,
54-
},
55-
/// When bumping a tx the absolute fee requested is lower than replaced tx absolute fee
56-
FeeTooLow {
57-
/// Required fee absolute value (satoshi)
58-
required: u64,
59-
},
6031
/// Node doesn't have data to estimate a fee rate
6132
FeeRateUnavailable,
62-
/// In order to use the [`TxBuilder::add_global_xpubs`] option every extended
63-
/// key in the descriptor must either be a master key itself (having depth = 0) or have an
64-
/// explicit origin provided
65-
///
66-
/// [`TxBuilder::add_global_xpubs`]: crate::wallet::tx_builder::TxBuilder::add_global_xpubs
67-
MissingKeyOrigin(String),
6833
/// Error while working with [`keys`](crate::keys)
6934
Key(crate::keys::KeyError),
7035
/// Descriptor checksum mismatch
7136
ChecksumMismatch,
72-
/// Spending policy is not compatible with this [`KeychainKind`](crate::types::KeychainKind)
73-
SpendingPolicyRequired(crate::types::KeychainKind),
74-
/// Error while extracting and manipulating policies
75-
InvalidPolicyPathError(crate::descriptor::policy::PolicyError),
7637
/// Signing error
7738
Signer(crate::wallet::signer::SignerError),
7839
/// Requested outpoint doesn't exist in the tx (vout greater than available outputs)
@@ -115,40 +76,15 @@ impl fmt::Display for Error {
11576
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11677
match self {
11778
Self::Generic(err) => write!(f, "Generic error: {}", err),
118-
Self::NoRecipients => write!(f, "Cannot build tx without recipients"),
119-
Self::NoUtxosSelected => write!(f, "No UTXO selected"),
120-
Self::OutputBelowDustLimit(limit) => {
121-
write!(f, "Output below the dust limit: {}", limit)
122-
}
123-
Self::InsufficientFunds { needed, available } => write!(
124-
f,
125-
"Insufficient funds: {} sat available of {} sat needed",
126-
available, needed
127-
),
128-
Self::BnBTotalTriesExceeded => {
129-
write!(f, "Branch and bound coin selection: total tries exceeded")
130-
}
131-
Self::BnBNoExactMatch => write!(f, "Branch and bound coin selection: not exact match"),
13279
Self::UnknownUtxo => write!(f, "UTXO not found in the internal database"),
13380
Self::TransactionNotFound => {
13481
write!(f, "Transaction not found in the internal database")
13582
}
13683
Self::TransactionConfirmed => write!(f, "Transaction already confirmed"),
13784
Self::IrreplaceableTransaction => write!(f, "Transaction can't be replaced"),
138-
Self::FeeRateTooLow { required } => write!(
139-
f,
140-
"Fee rate too low: required {} sat/vbyte",
141-
required.as_sat_per_vb()
142-
),
143-
Self::FeeTooLow { required } => write!(f, "Fee to low: required {} sat", required),
14485
Self::FeeRateUnavailable => write!(f, "Fee rate unavailable"),
145-
Self::MissingKeyOrigin(err) => write!(f, "Missing key origin: {}", err),
14686
Self::Key(err) => write!(f, "Key error: {}", err),
14787
Self::ChecksumMismatch => write!(f, "Descriptor checksum mismatch"),
148-
Self::SpendingPolicyRequired(keychain_kind) => {
149-
write!(f, "Spending policy required: {:?}", keychain_kind)
150-
}
151-
Self::InvalidPolicyPathError(err) => write!(f, "Invalid policy path: {}", err),
15288
Self::Signer(err) => write!(f, "Signer error: {}", err),
15389
Self::InvalidOutpoint(outpoint) => write!(
15490
f,
@@ -181,7 +117,6 @@ macro_rules! impl_error {
181117
}
182118

183119
impl_error!(descriptor::error::Error, Descriptor);
184-
impl_error!(descriptor::policy::PolicyError, InvalidPolicyPathError);
185120
impl_error!(wallet::signer::SignerError, Signer);
186121

187122
impl From<crate::keys::KeyError> for Error {

crates/bdk/src/wallet/coin_selection.rs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
//! ```
2727
//! # use std::str::FromStr;
2828
//! # use bitcoin::*;
29-
//! # use bdk::wallet::{self, ChangeSet, coin_selection::*, CreateTxError};
29+
//! # use bdk::wallet::{self, ChangeSet, coin_selection::*, coin_selection, CreateTxError};
3030
//! # use bdk_chain::PersistBackend;
3131
//! # use bdk::*;
3232
//! # use bdk::wallet::coin_selection::decide_change;
@@ -42,7 +42,7 @@
4242
//! fee_rate: bdk::FeeRate,
4343
//! target_amount: u64,
4444
//! drain_script: &Script,
45-
//! ) -> Result<CoinSelectionResult, bdk::Error> {
45+
//! ) -> Result<CoinSelectionResult, coin_selection::Error> {
4646
//! let mut selected_amount = 0;
4747
//! let mut additional_weight = Weight::ZERO;
4848
//! let all_utxos_selected = required_utxos
@@ -62,7 +62,7 @@
6262
//! let additional_fees = fee_rate.fee_wu(additional_weight);
6363
//! let amount_needed_with_fees = additional_fees + target_amount;
6464
//! if selected_amount < amount_needed_with_fees {
65-
//! return Err(bdk::Error::InsufficientFunds {
65+
//! return Err(coin_selection::Error::InsufficientFunds {
6666
//! needed: amount_needed_with_fees,
6767
//! available: selected_amount,
6868
//! });
@@ -100,14 +100,16 @@
100100
101101
use crate::types::FeeRate;
102102
use crate::wallet::utils::IsDust;
103+
use crate::Utxo;
103104
use crate::WeightedUtxo;
104-
use crate::{error::Error, Utxo};
105105

106106
use alloc::vec::Vec;
107107
use bitcoin::consensus::encode::serialize;
108108
use bitcoin::{Script, Weight};
109109

110110
use core::convert::TryInto;
111+
use core::fmt;
112+
use core::fmt::Formatter;
111113
use rand::seq::SliceRandom;
112114

113115
/// Default coin selection algorithm used by [`TxBuilder`](super::tx_builder::TxBuilder) if not
@@ -118,6 +120,43 @@ pub type DefaultCoinSelectionAlgorithm = BranchAndBoundCoinSelection;
118120
// prev_txid (32 bytes) + prev_vout (4 bytes) + sequence (4 bytes)
119121
pub(crate) const TXIN_BASE_WEIGHT: usize = (32 + 4 + 4) * 4;
120122

123+
/// Errors that can be thrown by the [`coin_selection`](crate::wallet::coin_selection) module
124+
#[derive(Debug)]
125+
pub enum Error {
126+
/// Wallet's UTXO set is not enough to cover recipient's requested plus fee
127+
InsufficientFunds {
128+
/// Sats needed for some transaction
129+
needed: u64,
130+
/// Sats available for spending
131+
available: u64,
132+
},
133+
/// Branch and bound coin selection tries to avoid needing a change by finding the right inputs for
134+
/// the desired outputs plus fee, if there is not such combination this error is thrown
135+
BnBNoExactMatch,
136+
/// Branch and bound coin selection possible attempts with sufficiently big UTXO set could grow
137+
/// exponentially, thus a limit is set, and when hit, this error is thrown
138+
BnBTotalTriesExceeded,
139+
}
140+
141+
impl fmt::Display for Error {
142+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
143+
match self {
144+
Self::InsufficientFunds { needed, available } => write!(
145+
f,
146+
"Insufficient funds: {} sat available of {} sat needed",
147+
available, needed
148+
),
149+
Self::BnBTotalTriesExceeded => {
150+
write!(f, "Branch and bound coin selection: total tries exceeded")
151+
}
152+
Self::BnBNoExactMatch => write!(f, "Branch and bound coin selection: not exact match"),
153+
}
154+
}
155+
}
156+
157+
#[cfg(feature = "std")]
158+
impl std::error::Error for Error {}
159+
121160
#[derive(Debug)]
122161
/// Remaining amount after performing coin selection
123162
pub enum Excess {

0 commit comments

Comments
 (0)