Skip to content

Commit 4a3675f

Browse files
committed
Merge #1768: refactor(wallet): cleanup and remove unused code in create_tx
a2f7a8b refactor(wallet): cleanup and remove unused code in create_tx (Steve Myers) Pull request description: ### Description Cleanup and remove unused code in `Wallet::create_tx`, this was noticed during review of #1763. See: #1763 (comment) fixes #1710 ### Notes to the reviewers In addition to removing the unneeded assignments to `fee_amount` and `received` I also refactored creation of the change output to be an `if let` instead of `match` statement since it only needs to do something if there is `Excess::Change`. I should have done this cleanup as part of #1048. ### Changelog notice None, only internal cleanup. ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing ACKs for top commit: ValuedMammal: reACK a2f7a8b Tree-SHA512: 64e5895ff3dc11f71c48b6c436d5c812504d0a24e92f1fdf451936f372d95ccdd8d89e5ac634a041bdee0a4836182f05127864ed744d560c9f8ec560e092c559
2 parents a208144 + a2f7a8b commit 4a3675f

File tree

1 file changed

+13
-29
lines changed
  • crates/wallet/src/wallet

1 file changed

+13
-29
lines changed

crates/wallet/src/wallet/mod.rs

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,19 +1399,13 @@ impl Wallet {
13991399
}
14001400

14011401
let mut outgoing = Amount::ZERO;
1402-
let mut received = Amount::ZERO;
1403-
14041402
let recipients = params.recipients.iter().map(|(r, v)| (r, *v));
14051403

14061404
for (index, (script_pubkey, value)) in recipients.enumerate() {
14071405
if !params.allow_dust && value.is_dust(script_pubkey) && !script_pubkey.is_op_return() {
14081406
return Err(CreateTxError::OutputBelowDustLimit(index));
14091407
}
14101408

1411-
if self.is_mine(script_pubkey.clone()) {
1412-
received += value;
1413-
}
1414-
14151409
let new_out = TxOut {
14161410
script_pubkey: script_pubkey.clone(),
14171411
value,
@@ -1467,9 +1461,8 @@ impl Wallet {
14671461
rng,
14681462
)
14691463
.map_err(CreateTxError::CoinSelection)?;
1470-
fee_amount += coin_selection.fee_amount;
1471-
let excess = &coin_selection.excess;
14721464

1465+
let excess = &coin_selection.excess;
14731466
tx.input = coin_selection
14741467
.selected
14751468
.iter()
@@ -1508,28 +1501,19 @@ impl Wallet {
15081501
}
15091502
}
15101503

1511-
match excess {
1512-
Excess::NoChange {
1513-
remaining_amount, ..
1514-
} => fee_amount += *remaining_amount,
1515-
Excess::Change { amount, fee } => {
1516-
if self.is_mine(drain_script.clone()) {
1517-
received += *amount;
1518-
}
1519-
fee_amount += *fee;
1520-
1521-
// create drain output
1522-
let drain_output = TxOut {
1523-
value: *amount,
1524-
script_pubkey: drain_script,
1525-
};
1504+
// if there's change, create and add a change output
1505+
if let Excess::Change { amount, .. } = excess {
1506+
// create drain output
1507+
let drain_output = TxOut {
1508+
value: *amount,
1509+
script_pubkey: drain_script,
1510+
};
15261511

1527-
// TODO: We should pay attention when adding a new output: this might increase
1528-
// the length of the "number of vouts" parameter by 2 bytes, potentially making
1529-
// our feerate too low
1530-
tx.output.push(drain_output);
1531-
}
1532-
};
1512+
// TODO: We should pay attention when adding a new output: this might increase
1513+
// the length of the "number of vouts" parameter by 2 bytes, potentially making
1514+
// our feerate too low
1515+
tx.output.push(drain_output);
1516+
}
15331517

15341518
// sort input/outputs according to the chosen algorithm
15351519
params.ordering.sort_tx_with_aux_rand(&mut tx, rng);

0 commit comments

Comments
 (0)