Skip to content

Commit

Permalink
gui: run auto-selection without button
Browse files Browse the repository at this point in the history
  • Loading branch information
jp1ac4 committed Jul 3, 2023
1 parent 203eeda commit 454535b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 48 deletions.
76 changes: 40 additions & 36 deletions gui/src/app/state/spend/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub struct DefineSpend {
balance_available: Amount,
recipients: Vec<Recipient>,
is_valid_for_coin_selection: bool,
is_manual_coin_selection: bool,
is_valid: bool,
is_duplicate: bool,

Expand Down Expand Up @@ -91,6 +92,7 @@ impl DefineSpend {
coins,
recipients: vec![Recipient::default()],
is_valid_for_coin_selection: false,
is_manual_coin_selection: false,
is_valid: false,
is_duplicate: false,
feerate: form::Value::default(),
Expand Down Expand Up @@ -149,6 +151,37 @@ impl DefineSpend {
self.is_valid = false;
}
}
fn auto_select_coins(&mut self, daemon: Arc<dyn Daemon + Sync + Send>) {
// Leave inputs empty for auto-selection.
let inputs: Vec<OutPoint> = Vec::new();
// Set other values in the same way as for manual coin selection.
let mut outputs: HashMap<Address, u64> = HashMap::new();
for recipient in &self.recipients {
outputs.insert(
Address::from_str(&recipient.address.value).expect("Checked before"),
recipient.amount().expect("Checked before"),
);
}
let feerate_vb = self.feerate.value.parse::<u64>().unwrap_or(0);
match daemon.create_spend_tx(&inputs, &outputs, feerate_vb) {
Ok(spend) => {
self.warning = None;
let selected_coins: Vec<OutPoint> = spend
.psbt
.unsigned_tx
.input
.iter()
.map(|c| c.previous_output)
.collect();

for (coin, selected) in &mut self.coins {
*selected = selected_coins.contains(&coin.outpoint);
}
// TODO: Do we need to call `self.amount_left_to_select()`? Auto-selection makes sure we have selected enough.
}
Err(e) => self.warning = Some(e.into()),
}
}
fn amount_left_to_select(&mut self) {
// We need the feerate in order to compute the required amount of BTC to
// select. Return early if we don't to not do unnecessary computation.
Expand Down Expand Up @@ -277,50 +310,22 @@ impl Step for DefineSpend {
Message::Psbt,
);
}
view::CreateSpendMessage::AutoSelectCoins => {
// Leave inputs empty for auto-selection.
let inputs: Vec<OutPoint> = Vec::new();
// Set other values in the same way as for manual coin selection.
let mut outputs: HashMap<Address, u64> = HashMap::new();
for recipient in &self.recipients {
outputs.insert(
Address::from_str(&recipient.address.value).expect("Checked before"),
recipient.amount().expect("Checked before"),
);
}
let feerate_vb = self.feerate.value.parse::<u64>().unwrap_or(0);
match daemon.create_spend_tx(&inputs, &outputs, feerate_vb) {
Ok(spend) => {
self.warning = None;
let selected_coins: Vec<OutPoint> = spend
.psbt
.unsigned_tx
.input
.iter()
.map(|c| c.previous_output)
.collect();

for coin in self.coins.iter_mut() {
if selected_coins.contains(&coin.0.outpoint) {
coin.1 = true;
} else {
coin.1 = false;
}
}
self.amount_left_to_select();
}
Err(e) => self.warning = Some(e.into()),
}
}
view::CreateSpendMessage::SelectCoin(i) => {
if let Some(coin) = self.coins.get_mut(i) {
coin.1 = !coin.1;
self.is_manual_coin_selection = true;
self.amount_left_to_select();
}
}
_ => {}
}
self.check_valid();
if self.is_valid_for_coin_selection
&& !self.is_manual_coin_selection
&& !self.recipients.is_empty() // not a self-send
{
self.auto_select_coins(daemon);
}
Command::none()
} else {
if let Message::Psbt(res) = message {
Expand Down Expand Up @@ -360,7 +365,6 @@ impl Step for DefineSpend {
.map(|r| r.amount().unwrap_or(0_u64))
.sum(),
),
self.is_valid_for_coin_selection,
self.is_valid,
self.is_duplicate,
self.timelock,
Expand Down
1 change: 0 additions & 1 deletion gui/src/app/view/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub enum CreateSpendMessage {
AddRecipient,
DeleteRecipient(usize),
SelectCoin(usize),
AutoSelectCoins,
RecipientEdited(usize, &'static str, String),
FeerateEdited(String),
SelectPath(usize),
Expand Down
11 changes: 0 additions & 11 deletions gui/src/app/view/spend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ pub fn create_spend_tx<'a>(
balance_available: &'a Amount,
recipients: Vec<Element<'a, Message>>,
total_amount: Amount,
is_valid_for_coin_selection: bool,
is_valid: bool,
duplicate: bool,
timelock: u16,
Expand Down Expand Up @@ -153,16 +152,6 @@ pub fn create_spend_tx<'a>(
)
.push(Space::with_width(Length::FillPortion(1))),
)
.push_maybe(if is_self_send {
None
} else {
let button = button::primary(None, "Auto-select coins").width(Length::Fixed(200.0));
Some(Row::new().push(if is_valid_for_coin_selection {
button.on_press(Message::CreateSpend(CreateSpendMessage::AutoSelectCoins))
} else {
button
}))
})
.push(
Container::new(
Column::new()
Expand Down

0 comments on commit 454535b

Please sign in to comment.