Skip to content

Commit

Permalink
refactor(wallet): cleanup and remove unused code in create_tx
Browse files Browse the repository at this point in the history
  • Loading branch information
notmandatory committed Dec 10, 2024
1 parent ab08b8c commit a0ba177
Showing 1 changed file with 15 additions and 31 deletions.
46 changes: 15 additions & 31 deletions crates/wallet/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -1393,19 +1393,13 @@ 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() {
if !params.allow_dust && value.is_dust(script_pubkey) && !script_pubkey.is_op_return() {
return Err(CreateTxError::OutputBelowDustLimit(index));
}

if self.is_mine(script_pubkey.clone()) {
received += value;
}

let new_out = TxOut {
script_pubkey: script_pubkey.clone(),
value,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -1500,36 +1493,27 @@ 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);

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
Expand Down

0 comments on commit a0ba177

Please sign in to comment.