From a0ba1770c6c34c3afa5148f0b80b2e40753cde02 Mon Sep 17 00:00:00 2001 From: Steve Myers Date: Mon, 9 Dec 2024 17:56:49 -0600 Subject: [PATCH] refactor(wallet): cleanup and remove unused code in create_tx --- crates/wallet/src/wallet/mod.rs | 46 +++++++++++---------------------- 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/crates/wallet/src/wallet/mod.rs b/crates/wallet/src/wallet/mod.rs index e2e905020..f9adf350d 100644 --- a/crates/wallet/src/wallet/mod.rs +++ b/crates/wallet/src/wallet/mod.rs @@ -70,7 +70,7 @@ use crate::types::*; use crate::wallet::{ coin_selection::{ DefaultCoinSelectionAlgorithm, - Excess::{self, Change, NoChange}, + Excess::{Change, NoChange}, InsufficientFunds, }, error::{BuildFeeBumpError, CreateTxError, MiniscriptPsbtError}, @@ -1393,8 +1393,6 @@ impl Wallet { } let mut outgoing = Amount::ZERO; - let mut received = Amount::ZERO; - let recipients = params.recipients.iter().map(|(r, v)| (r, *v)); for (index, (script_pubkey, value)) in recipients.enumerate() { @@ -1402,10 +1400,6 @@ impl Wallet { return Err(CreateTxError::OutputBelowDustLimit(index)); } - if self.is_mine(script_pubkey.clone()) { - received += value; - } - let new_out = TxOut { script_pubkey: script_pubkey.clone(), value, @@ -1461,9 +1455,8 @@ impl Wallet { rng, ) .map_err(CreateTxError::CoinSelection)?; - fee_amount += Amount::from_sat(coin_selection.fee_amount); - let excess = &coin_selection.excess; + let excess = &coin_selection.excess; tx.input = coin_selection .selected .iter() @@ -1500,28 +1493,19 @@ impl Wallet { } } - match excess { - NoChange { - remaining_amount, .. - } => fee_amount += Amount::from_sat(*remaining_amount), - Change { amount, fee } => { - if self.is_mine(drain_script.clone()) { - received += Amount::from_sat(*amount); - } - fee_amount += Amount::from_sat(*fee); - - // create drain output - let drain_output = TxOut { - value: Amount::from_sat(*amount), - script_pubkey: drain_script, - }; + // if there's change, create and add a change output + if let Change { amount, .. } = excess { + // create drain output + let drain_output = TxOut { + value: Amount::from_sat(*amount), + script_pubkey: drain_script, + }; - // TODO: We should pay attention when adding a new output: this might increase - // the length of the "number of vouts" parameter by 2 bytes, potentially making - // our feerate too low - tx.output.push(drain_output); - } - }; + // TODO: We should pay attention when adding a new output: this might increase + // the length of the "number of vouts" parameter by 2 bytes, potentially making + // our feerate too low + tx.output.push(drain_output); + } // sort input/outputs according to the chosen algorithm params.ordering.sort_tx_with_aux_rand(&mut tx, rng); @@ -1529,7 +1513,7 @@ impl Wallet { let psbt = self.complete_transaction(tx, coin_selection.selected, params)?; // recording changes to the change keychain - if let (Excess::Change { .. }, Some((keychain, index))) = (excess, drain_index) { + if let (Change { .. }, Some((keychain, index))) = (excess, drain_index) { let (_, index_changeset) = self .indexed_graph .index