fuel_streams_core/transactions/
subjects.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#![allow(unused)]
use fuel_core_types::fuel_tx::UniqueIdentifier;
use fuel_streams_macros::subject::{IntoSubject, Subject, SubjectBuildable};

use crate::{blocks::types::BlockHeight, types::*};

/// Represents a subject for querying transactions by their identifier in the Fuel ecosystem.
///
/// This struct is used to create and parse subjects related to transactions identified by
/// various types of IDs, which can be used for subscribing to or publishing events
/// about specific transactions.
///
/// # Examples
///
/// Creating and parsing a subject:
///
/// ```
/// # use fuel_streams_core::transactions::subjects::TransactionsByIdSubject;
/// # use fuel_streams_core::types::*;
/// # use fuel_streams_macros::subject::*;
/// let subject = TransactionsByIdSubject {
///     tx_id: Some([1u8; 32].into()),
///     index: Some(0),
///     id_kind: Some(IdentifierKind::ContractID),
///     id_value: Some([2u8; 32].into()),
/// };
/// assert_eq!(
///     subject.parse(),
///     "by_id.transactions.0x0101010101010101010101010101010101010101010101010101010101010101.0.contract_id.0x0202020202020202020202020202020202020202020202020202020202020202"
/// );
/// ```
///
/// All transactions by ID wildcard:
///
/// ```
/// # use fuel_streams_core::transactions::subjects::TransactionsByIdSubject;
/// # use fuel_streams_macros::subject::*;
/// assert_eq!(TransactionsByIdSubject::WILDCARD, "by_id.transactions.>");
/// ```
///
/// Creating a subject query using the `wildcard` method:
///
/// ```
/// # use fuel_streams_core::transactions::subjects::TransactionsByIdSubject;
/// # use fuel_streams_core::types::*;
/// # use fuel_streams_macros::subject::*;
/// let wildcard = TransactionsByIdSubject::wildcard(Some([1u8; 32].into()), Some(0), Some(IdentifierKind::ContractID), None);
/// assert_eq!(wildcard, "by_id.transactions.0x0101010101010101010101010101010101010101010101010101010101010101.0.contract_id.*");
/// ```
///
/// Using the builder pattern:
///
/// ```
/// # use fuel_streams_core::transactions::subjects::TransactionsByIdSubject;
/// # use fuel_streams_core::types::*;
/// # use fuel_streams_macros::subject::*;
/// let subject = TransactionsByIdSubject::new()
///     .with_tx_id(Some([1u8; 32].into()))
///     .with_index(Some(0))
///     .with_id_kind(Some(IdentifierKind::ContractID))
///     .with_id_value(Some([2u8; 32].into()));
/// assert_eq!(subject.parse(), "by_id.transactions.0x0101010101010101010101010101010101010101010101010101010101010101.0.contract_id.0x0202020202020202020202020202020202020202020202020202020202020202");
/// ```
#[derive(Subject, Debug, Clone, Default)]
#[subject_wildcard = "by_id.transactions.>"]
#[subject_format = "by_id.transactions.{tx_id}.{index}.{id_kind}.{id_value}"]
pub struct TransactionsByIdSubject {
    pub tx_id: Option<Bytes32>,
    pub index: Option<u8>,
    pub id_kind: Option<IdentifierKind>,
    pub id_value: Option<Bytes32>,
}

/// Represents a subject for publishing transactions that happen in the Fuel network.
///
/// This subject format allows for efficient querying and filtering of transactions
/// based on their height, index, ID, status, and kind.
///
/// # Examples
///
/// Creating a subject for a specific transaction:
///
/// ```
/// # use fuel_streams_core::transactions::TransactionsSubject;
/// # use fuel_streams_core::types::*;
/// # use fuel_streams_macros::subject::IntoSubject;
/// let subject = TransactionsSubject {
///     block_height: Some(23.into()),
///     index: Some(1),
///     tx_id: Some(Bytes32::zeroed()),
///     status: Some(TransactionStatus::Success),
///     kind: Some(TransactionKind::Script),
/// };
/// assert_eq!(
///     subject.parse(),
///     "transactions.23.1.0x0000000000000000000000000000000000000000000000000000000000000000.success.script"
/// );
/// ```
///
/// All transactions wildcard:
///
/// ```
/// # use fuel_streams_core::transactions::TransactionsSubject;
/// assert_eq!(TransactionsSubject::WILDCARD, "transactions.>");
/// ```
///
/// Creating a subject query using the `wildcard` method:
///
/// ```
/// # use fuel_streams_core::transactions::TransactionsSubject;
/// # use fuel_streams_core::types::*;
/// let wildcard = TransactionsSubject::wildcard(None, None, Some(Bytes32::zeroed()), None, None);
/// assert_eq!(wildcard, "transactions.*.*.0x0000000000000000000000000000000000000000000000000000000000000000.*.*");
/// ```
///
/// Using the builder pattern:
///
/// ```
/// # use fuel_streams_core::transactions::TransactionsSubject;
/// # use fuel_streams_core::types::*;
/// # use fuel_streams_macros::subject::*;
/// let subject = TransactionsSubject::new()
///     .with_block_height(Some(23.into()))
///     .with_index(Some(1))
///     .with_tx_id(Some(Bytes32::zeroed()))
///     .with_status(Some(TransactionStatus::Success))
///     .with_kind(Some(TransactionKind::Script));
/// assert_eq!(subject.parse(), "transactions.23.1.0x0000000000000000000000000000000000000000000000000000000000000000.success.script");
/// ```
#[derive(Subject, Debug, Clone, Default)]
#[subject_wildcard = "transactions.>"]
#[subject_format = "transactions.{block_height}.{index}.{tx_id}.{status}.{kind}"]
pub struct TransactionsSubject {
    pub block_height: Option<BlockHeight>,
    pub index: Option<usize>,
    pub tx_id: Option<Bytes32>,
    pub status: Option<TransactionStatus>,
    pub kind: Option<TransactionKind>,
}

impl From<&Transaction> for TransactionsSubject {
    fn from(value: &Transaction) -> Self {
        let subject = TransactionsSubject::new();
        let tx_id = value.cached_id().unwrap();
        let kind = TransactionKind::from(value.to_owned());
        subject.with_tx_id(Some(tx_id.into())).with_kind(Some(kind))
    }
}

#[cfg(test)]
mod test {
    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn transactions_subjects_from_transaction() {
        let mock_tx = MockTransaction::build();
        let subject = TransactionsSubject::from(&mock_tx);
        assert!(subject.block_height.is_none());
        assert!(subject.index.is_none());
        assert!(subject.status.is_none());
        assert!(subject.kind.is_some());
        assert_eq!(
            subject.tx_id.unwrap(),
            mock_tx.to_owned().cached_id().unwrap().into()
        );
    }
}