Skip to content

Commit 6250c36

Browse files
committed
feat: add new error variant, remove unwrap usage
1 parent fdd7f70 commit 6250c36

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
@@ -228,6 +228,14 @@ pub enum BuildFeeBumpError {
228228
IrreplaceableTransaction(Txid),
229229
/// Node doesn't have data to estimate a fee rate
230230
FeeRateUnavailable,
231+
/// Happens when the descriptor is impossible to satisfy
232+
MiniscriptError(miniscript::Error),
233+
}
234+
235+
impl From<miniscript::Error> for BuildFeeBumpError {
236+
fn from(v: miniscript::Error) -> Self {
237+
Self::MiniscriptError(v)
238+
}
231239
}
232240

233241
impl fmt::Display for BuildFeeBumpError {
@@ -252,6 +260,11 @@ impl fmt::Display for BuildFeeBumpError {
252260
write!(f, "Transaction can't be replaced with txid: {}", txid)
253261
}
254262
Self::FeeRateUnavailable => write!(f, "Fee rate unavailable"),
263+
Self::MiniscriptError(error) => write!(
264+
f,
265+
"It's not possible to satisfy the descriptor, miniscript error: {}",
266+
error
267+
),
255268
}
256269
}
257270
}

crates/wallet/src/wallet/mod.rs

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

16441644
let weighted_utxo = match txout_index.index_of_spk(&txout.script_pubkey) {
16451645
Some(&(keychain, derivation_index)) => {
1646-
// TODO: (@leonardo) remove unwrap() use here, use expect or proper error!
16471646
let satisfaction_weight = self
16481647
.get_descriptor_for_keychain(keychain)
1649-
.max_weight_to_satisfy()
1650-
.unwrap();
1648+
.max_weight_to_satisfy()?;
16511649
WeightedUtxo {
16521650
utxo: Utxo::Local(LocalOutput {
16531651
outpoint: txin.previous_output,
@@ -1979,7 +1977,7 @@ impl Wallet {
19791977
(utxo, {
19801978
self.get_descriptor_for_keychain(keychain)
19811979
.max_weight_to_satisfy()
1982-
.unwrap() // TODO: (@leonardo) remove unwrap() use here, use expect or proper error!
1980+
.unwrap()
19831981
})
19841982
})
19851983
.collect()

crates/wallet/src/wallet/tx_builder.rs

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

296296
for utxo in utxos {
297297
let descriptor = wallet.get_descriptor_for_keychain(utxo.keychain);
298-
// TODO: (@leonardo) remove unwrap() use here, use expect or proper error!
299-
let satisfaction_weight = descriptor.max_weight_to_satisfy().unwrap();
298+
let satisfaction_weight = descriptor.max_weight_to_satisfy()?;
300299
self.params.utxos.push(WeightedUtxo {
301300
satisfaction_weight,
302301
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)