Skip to content

Commit

Permalink
domain type (TransactionSummary): combine into a sub-message
Browse files Browse the repository at this point in the history
  • Loading branch information
TalDerei committed Nov 30, 2024
1 parent 425e549 commit 95aacc0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 40 deletions.
61 changes: 24 additions & 37 deletions crates/core/transaction/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ pub struct TransactionBody {
#[derive(Clone, Default, Serialize, Deserialize)]
#[serde(try_from = "pbt::TransactionSummary", into = "pbt::TransactionSummary")]
pub struct TransactionSummary {
pub effects: Vec<Effects>,
pub effects: Vec<TransactionEffect>,
}

/// Represents an individual effect of a transaction.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(try_from = "pbt::Effects", into = "pbt::Effects")]
pub struct Effects {
pub struct TransactionEffect {
pub address: AddressView,
pub balance: Balance,
}
Expand Down Expand Up @@ -612,9 +611,16 @@ impl DomainType for TransactionSummary {
}

impl From<TransactionSummary> for pbt::TransactionSummary {
fn from(pbt: TransactionSummary) -> Self {
fn from(summary: TransactionSummary) -> Self {
pbt::TransactionSummary {
effects: pbt.effects.into_iter().map(Into::into).collect(),
effects: summary
.effects
.into_iter()
.map(|effect| pbt::transaction_summary::Effects {
address: Some(effect.address.into()),
balance: Some(effect.balance.into()),
})
.collect(),
}
}
}
Expand All @@ -626,43 +632,24 @@ impl TryFrom<pbt::TransactionSummary> for TransactionSummary {
let effects = pbt
.effects
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<_>, _>>()?;
.map(|effect| {
Ok(TransactionEffect {
address: effect
.address
.ok_or_else(|| anyhow::anyhow!("missing address field"))?
.try_into()?,
balance: effect
.balance
.ok_or_else(|| anyhow::anyhow!("missing balance field"))?
.try_into()?,
})
})
.collect::<Result<Vec<TransactionEffect>, anyhow::Error>>()?;

Ok(Self { effects })
}
}

impl DomainType for Effects {
type Proto = pbt::Effects;
}

impl From<Effects> for pbt::Effects {
fn from(effect: Effects) -> Self {
pbt::Effects {
address: Some(effect.address.into()),
balance: Some(effect.balance.into()),
}
}
}

impl TryFrom<pbt::Effects> for Effects {
type Error = anyhow::Error;

fn try_from(pbt: pbt::Effects) -> Result<Self, Self::Error> {
Ok(Self {
address: pbt
.address
.ok_or_else(|| anyhow::anyhow!("missing address field"))?
.try_into()?,
balance: pbt
.balance
.ok_or_else(|| anyhow::anyhow!("missing balance field"))?
.try_into()?,
})
}
}

impl DomainType for TransactionBody {
type Proto = pbt::TransactionBody;
}
Expand Down
6 changes: 3 additions & 3 deletions crates/core/transaction/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use transaction_perspective::TransactionPerspective;

use crate::{
memo::MemoCiphertext,
transaction::{Effects, TransactionSummary},
transaction::{TransactionEffect, TransactionSummary},
Action, DetectionData, Transaction, TransactionBody, TransactionParameters,
};

Expand Down Expand Up @@ -111,7 +111,7 @@ impl TransactionView {
address: note.address(),
};

effects.push(Effects { address, balance });
effects.push(TransactionEffect { address, balance });
}
SpendView::Opaque { spend: _ } => continue,
},
Expand All @@ -127,7 +127,7 @@ impl TransactionView {
address: note.address(),
};

effects.push(Effects { address, balance });
effects.push(TransactionEffect { address, balance });
}
OutputView::Opaque { output: _ } => continue,
},
Expand Down

0 comments on commit 95aacc0

Please sign in to comment.