Skip to content

Commit

Permalink
Change get_remainder_address() to return a Result
Browse files Browse the repository at this point in the history
  • Loading branch information
Thoralf-M committed Sep 25, 2023
1 parent aea2d31 commit 07b2d00
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions sdk/src/client/api/block_builder/input_selection/core/remainder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{

impl InputSelection {
// Gets the remainder address from configuration of finds one from the inputs.
fn get_remainder_address(&self) -> Option<(Address, Option<Bip44>)> {
fn get_remainder_address(&self) -> Result<Option<(Address, Option<Bip44>)>, Error> {
if let Some(remainder_address) = self.remainder_address {
// Search in inputs for the Bip44 chain for the remainder address, so the ledger can regenerate it
for input in self.available_inputs.iter().chain(self.selected_inputs.iter()) {
Expand All @@ -31,16 +31,16 @@ impl InputSelection {
self.outputs.as_slice(),
self.burn.as_ref(),
);
let required_address =
let (required_address, _) =
input
.output
.required_and_unlocked_address(self.timestamp, input.output_id(), alias_transition);
.required_and_unlocked_address(self.timestamp, input.output_id(), alias_transition)?;

if matches!(required_address, Ok(a) if a.0 == remainder_address) {
return Some((remainder_address, input.chain));
if required_address == remainder_address {
return Ok(Some((remainder_address, input.chain)));
}
}
return Some((remainder_address, None));
return Ok(Some((remainder_address, None)));
}

for input in &self.selected_inputs {
Expand All @@ -50,19 +50,17 @@ impl InputSelection {
self.outputs.as_slice(),
self.burn.as_ref(),
);
// PANIC: safe to unwrap as outputs with no address have been filtered out already.
let required_address = input
.output
.required_and_unlocked_address(self.timestamp, input.output_id(), alias_transition)
.unwrap()
.required_and_unlocked_address(self.timestamp, input.output_id(), alias_transition)?
.0;

if required_address.is_ed25519() {
return Some((required_address, input.chain));
return Ok(Some((required_address, input.chain)));
}
}

None
Ok(None)
}

pub(crate) fn remainder_amount(&self) -> Result<(u64, bool), Error> {
Expand Down Expand Up @@ -142,7 +140,7 @@ impl InputSelection {
return Ok((None, storage_deposit_returns));
}

let Some((remainder_address, chain)) = self.get_remainder_address() else {
let Some((remainder_address, chain)) = self.get_remainder_address()? else {
return Err(Error::MissingInputWithEd25519Address);
};

Expand Down

0 comments on commit 07b2d00

Please sign in to comment.