miden_client/transaction/
result.rs

1use alloc::vec::Vec;
2
3use miden_objects::account::AccountDelta;
4use miden_objects::block::BlockNumber;
5use miden_objects::note::{NoteDetails, NoteTag};
6use miden_objects::transaction::{
7    ExecutedTransaction,
8    InputNote,
9    InputNotes,
10    OutputNotes,
11    TransactionArgs,
12    TransactionId,
13    TransactionInputs,
14};
15use miden_tx::utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable};
16
17use crate::ClientError;
18
19// TRANSACTION RESULT
20// ================================================================================================
21
22/// Represents the result of executing a transaction by the client.
23///
24/// It contains an [`ExecutedTransaction`], and a list of `future_notes` that we expect to receive
25/// in the future (you can check at swap notes for an example of this).
26#[derive(Clone, Debug, PartialEq)]
27pub struct TransactionResult {
28    transaction: ExecutedTransaction,
29    future_notes: Vec<(NoteDetails, NoteTag)>,
30}
31
32impl TransactionResult {
33    /// Screens the output notes to store and track the relevant ones, and instantiates a
34    /// [`TransactionResult`].
35    pub fn new(
36        transaction: ExecutedTransaction,
37        future_notes: Vec<(NoteDetails, NoteTag)>,
38    ) -> Result<Self, ClientError> {
39        Ok(Self { transaction, future_notes })
40    }
41
42    /// Returns a unique identifier of this transaction.
43    pub fn id(&self) -> TransactionId {
44        self.transaction.id()
45    }
46
47    /// Returns the [`ExecutedTransaction`].
48    pub fn executed_transaction(&self) -> &ExecutedTransaction {
49        &self.transaction
50    }
51
52    /// Returns the output notes that were generated as a result of the transaction execution.
53    pub fn created_notes(&self) -> &OutputNotes {
54        self.transaction.output_notes()
55    }
56
57    /// Returns the list of notes that might be created in the future as a result of the
58    /// transaction execution.
59    pub fn future_notes(&self) -> &[(NoteDetails, NoteTag)] {
60        &self.future_notes
61    }
62
63    /// Returns the block against which the transaction was executed.
64    pub fn block_num(&self) -> BlockNumber {
65        self.transaction.block_header().block_num()
66    }
67
68    /// Returns transaction's [`TransactionArgs`].
69    pub fn transaction_arguments(&self) -> &TransactionArgs {
70        self.transaction.tx_args()
71    }
72
73    /// Returns a reference to the [`TransactionInputs`].
74    pub fn tx_inputs(&self) -> &TransactionInputs {
75        self.transaction.tx_inputs()
76    }
77
78    /// Returns the [`AccountDelta`] that describes the change of state for the executing account.
79    pub fn account_delta(&self) -> &AccountDelta {
80        self.transaction.account_delta()
81    }
82
83    /// Returns input notes that were consumed as part of the transaction.
84    pub fn consumed_notes(&self) -> &InputNotes<InputNote> {
85        self.transaction.tx_inputs().input_notes()
86    }
87}
88
89impl From<&TransactionResult> for TransactionInputs {
90    fn from(value: &TransactionResult) -> Self {
91        value.executed_transaction().tx_inputs().clone()
92    }
93}
94
95impl From<TransactionResult> for TransactionInputs {
96    fn from(value: TransactionResult) -> Self {
97        let (inputs, ..) = value.transaction.into_parts();
98        inputs
99    }
100}
101
102impl From<TransactionResult> for ExecutedTransaction {
103    fn from(tx_result: TransactionResult) -> ExecutedTransaction {
104        tx_result.transaction
105    }
106}
107
108impl Serializable for TransactionResult {
109    fn write_into<W: ByteWriter>(&self, target: &mut W) {
110        self.transaction.write_into(target);
111        self.future_notes.write_into(target);
112    }
113}
114
115impl Deserializable for TransactionResult {
116    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
117        let transaction = ExecutedTransaction::read_from(source)?;
118        let future_notes = Vec::<(NoteDetails, NoteTag)>::read_from(source)?;
119
120        Ok(Self { transaction, future_notes })
121    }
122}