Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/psbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub struct TransactionDetails {
pub total_with_self_send: Amount,
/// The total self send amount, including change and transfers.
pub total_self_send: Amount,
/// Non-change self-send, displayed as the total in case of self-sends
pub total_non_change_self_send: Amount,
/// The fee of this transaction.
pub fee: Amount,
/// The descriptors discovered in the PSBT.
Expand All @@ -52,6 +54,14 @@ impl TransactionDetails {
pub fn is_self_send(&self) -> bool {
self.total() == Amount::ZERO
}

pub fn display_total(&self) -> Amount {
if self.is_self_send() {
self.total_non_change_self_send
} else {
self.total()
}
}
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -86,6 +96,10 @@ impl PsbtOutput {
pub fn is_self_send(&self) -> bool {
self.kind.is_self_send()
}

pub fn is_non_change_self_send(&self) -> bool {
self.kind.is_non_change_self_send()
}
}

/// The recipient of funds.
Expand Down Expand Up @@ -152,6 +166,13 @@ impl OutputKind {
OutputKind::External(_) | OutputKind::OpReturn(_) => false,
}
}

pub fn is_non_change_self_send(&self) -> bool {
match self {
OutputKind::Transfer { .. } => true,
OutputKind::Change(_) | OutputKind::External(_) | OutputKind::OpReturn(_) | OutputKind::Suspicious(_) => false,
}
}
}

/// Parts of an OP_RETURN output type.
Expand Down Expand Up @@ -574,6 +595,7 @@ where

let mut total_with_self_send = Amount::ZERO;
let mut total_self_send = Amount::ZERO;
let mut total_non_change_self_send = Amount::ZERO;
for (i, output) in psbt.outputs.iter().enumerate() {
let Some(txout) = psbt.unsigned_tx.output.get(i) else {
return Err(Error::MissingOutput { index: i });
Expand Down Expand Up @@ -606,12 +628,17 @@ where
total_self_send += output_details.amount;
}

if output_details.is_non_change_self_send() {
total_non_change_self_send += output_details.amount;
}

outputs.push(output_details);
}

Ok(TransactionDetails {
total_with_self_send,
total_self_send,
total_non_change_self_send,
fee: psbt.fee()?,
descriptors,
inputs,
Expand Down