Skip to content

Commit

Permalink
stub summary method in TransactionView
Browse files Browse the repository at this point in the history
  • Loading branch information
TalDerei committed Nov 25, 2024
1 parent 8382e6a commit ff86c9a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
2 changes: 1 addition & 1 deletion crates/core/asset/src/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl TryFrom<pb::Balance> for Balance {

impl From<Balance> for pb::Balance {
fn from(_v: Balance) -> Self {
todo!() // todo: implement fallible conversion
todo!() // todo: implement fallible conversion
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/core/transaction/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ pub struct TransactionBody {
#[derive(Clone, Default, Serialize, Deserialize)]
#[serde(try_from = "pbt::TransactionSummary", into = "pbt::TransactionSummary")]
pub struct TransactionSummary {
effects: Vec<Effects>,
pub effects: Vec<Effects>,
}

/// 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 {
address: AddressView,
balance: Balance,
pub address: AddressView,
pub balance: Balance,
}

impl EffectingData for TransactionBody {
Expand Down
49 changes: 47 additions & 2 deletions crates/core/transaction/src/view.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use anyhow::Context;
use decaf377_rdsa::{Binding, Signature};
use penumbra_asset::Balance;
use penumbra_keys::AddressView;
use penumbra_proto::{core::transaction::v1 as pbt, DomainType};

use penumbra_shielded_pool::{OutputView, SpendView};
use serde::{Deserialize, Serialize};

pub mod action_view;
Expand All @@ -13,8 +15,9 @@ use penumbra_tct as tct;
pub use transaction_perspective::TransactionPerspective;

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

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -94,6 +97,48 @@ impl TransactionView {
pub fn action_views(&self) -> impl Iterator<Item = &ActionView> {
self.body_view.action_views.iter()
}

pub fn summary(&self) -> TransactionSummary {
let mut effects = Vec::new();

for action_view in &self.body_view.action_views {
match action_view {
ActionView::Spend(spend_view) => match spend_view {
SpendView::Visible { spend: _, note } => {
let value = note.value.value();
let balance = Balance::from(value);
let address = AddressView::Opaque {
address: note.address(),
};

effects.push(Effects { address, balance });
}
SpendView::Opaque { spend: _ } => continue,
},
ActionView::Output(output_view) => match output_view {
OutputView::Visible {
output: _,
note,
payload_key: _,
} => {
let value = note.value.value();
let balance = -Balance::from(value);
let address = AddressView::Opaque {
address: note.address(),
};

effects.push(Effects { address, balance });
}
OutputView::Opaque { output: _ } => continue,
},
ActionView::Swap(_) => todo!(),
ActionView::SwapClaim(_) => todo!(),
_ => {}
}
}

TransactionSummary { effects }
}
}

impl DomainType for TransactionView {
Expand Down

0 comments on commit ff86c9a

Please sign in to comment.