Skip to main content

miden_client/transaction/
result.rs

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