Skip to content

Commit 3bcb211

Browse files
committed
feat: add new error variant, remove unwrap usage
1 parent 8cbb2a3 commit 3bcb211

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

crates/wallet/src/wallet/error.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,14 @@ pub enum BuildFeeBumpError {
237237
IrreplaceableTransaction(Txid),
238238
/// Node doesn't have data to estimate a fee rate
239239
FeeRateUnavailable,
240+
/// Happens when the descriptor is impossible to satisfy
241+
MiniscriptError(miniscript::Error),
242+
}
243+
244+
impl From<miniscript::Error> for BuildFeeBumpError {
245+
fn from(v: miniscript::Error) -> Self {
246+
Self::MiniscriptError(v)
247+
}
240248
}
241249

242250
impl fmt::Display for BuildFeeBumpError {
@@ -261,6 +269,11 @@ impl fmt::Display for BuildFeeBumpError {
261269
write!(f, "Transaction can't be replaced with txid: {}", txid)
262270
}
263271
Self::FeeRateUnavailable => write!(f, "Fee rate unavailable"),
272+
Self::MiniscriptError(error) => write!(
273+
f,
274+
"It's not possible to satisfy the descriptor, miniscript error: {}",
275+
error
276+
),
264277
}
265278
}
266279
}

crates/wallet/src/wallet/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,11 +1703,9 @@ impl Wallet {
17031703

17041704
let weighted_utxo = match txout_index.index_of_spk(&txout.script_pubkey) {
17051705
Some((keychain, derivation_index)) => {
1706-
// TODO: (@leonardo) remove unwrap() use here, use expect or proper error!
17071706
let satisfaction_weight = self
17081707
.get_descriptor_for_keychain(keychain)
1709-
.max_weight_to_satisfy()
1710-
.unwrap();
1708+
.max_weight_to_satisfy()?;
17111709
WeightedUtxo {
17121710
utxo: Utxo::Local(LocalOutput {
17131711
outpoint: txin.previous_output,
@@ -2040,7 +2038,7 @@ impl Wallet {
20402038
(utxo, {
20412039
self.get_descriptor_for_keychain(keychain)
20422040
.max_weight_to_satisfy()
2043-
.unwrap() // TODO: (@leonardo) remove unwrap() use here, use expect or proper error!
2041+
.unwrap()
20442042
})
20452043
})
20462044
.collect()

crates/wallet/src/wallet/tx_builder.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,7 @@ impl<'a, Cs> TxBuilder<'a, Cs> {
297297

298298
for utxo in utxos {
299299
let descriptor = wallet.get_descriptor_for_keychain(utxo.keychain);
300-
// TODO: (@leonardo) remove unwrap() use here, use expect or proper error!
301-
let satisfaction_weight = descriptor.max_weight_to_satisfy().unwrap();
300+
let satisfaction_weight = descriptor.max_weight_to_satisfy()?;
302301
self.params.utxos.push(WeightedUtxo {
303302
satisfaction_weight,
304303
utxo: Utxo::Local(utxo),
@@ -688,6 +687,14 @@ impl<'a, Cs: CoinSelectionAlgorithm> TxBuilder<'a, Cs> {
688687
pub enum AddUtxoError {
689688
/// Happens when trying to spend an UTXO that is not in the internal database
690689
UnknownUtxo(OutPoint),
690+
/// Happens when the descriptor is impossible to satisfy
691+
MiniscriptError(miniscript::Error),
692+
}
693+
694+
impl From<miniscript::Error> for AddUtxoError {
695+
fn from(v: miniscript::Error) -> Self {
696+
Self::MiniscriptError(v)
697+
}
691698
}
692699

693700
impl fmt::Display for AddUtxoError {
@@ -698,6 +705,11 @@ impl fmt::Display for AddUtxoError {
698705
"UTXO not found in the internal database for txid: {} with vout: {}",
699706
outpoint.txid, outpoint.vout
700707
),
708+
Self::MiniscriptError(error) => write!(
709+
f,
710+
"It's not possible to satisfy the descriptor, miniscript error: {}",
711+
error
712+
),
701713
}
702714
}
703715
}

0 commit comments

Comments
 (0)