Skip to main content

xrpl_common_stdlib/objects/generated/
check.rs

1// GENERATED -- do not hand-edit. Run scripts/generate-ledger-objects.sh to regenerate.
2
3use crate::host::Result;
4use crate::objects::traits::CurrentLedgerObjectCommonFields;
5use crate::objects::traits::LedgerObjectCommonFields;
6use crate::objects::{current_ledger_object, ledger_object};
7use crate::sfield;
8use crate::types::account_id::AccountID;
9use crate::types::amount::Amount;
10use crate::types::uint::Hash256;
11
12/// Trait providing access to fields specific to Check objects in any ledger.
13pub trait CheckFields: LedgerObjectCommonFields {
14    /// The sender of the Check. Cashing the Check debits this address's balance.
15    fn account(&self) -> Result<AccountID> {
16        ledger_object::get_field(self.get_slot_num(), sfield::Account)
17    }
18
19    /// The intended recipient of the Check. Only this address can cash the Check, using a CheckCash
20    /// transaction.
21    fn destination(&self) -> Result<AccountID> {
22        ledger_object::get_field(self.get_slot_num(), sfield::Destination)
23    }
24
25    /// The maximum amount of currency this Check can debit the sender. If the Check is successfully
26    /// cashed, the destination is credited in the same currency for up to this amount.
27    fn send_max(&self) -> Result<Amount> {
28        ledger_object::get_field(self.get_slot_num(), sfield::SendMax)
29    }
30
31    /// The sequence number of the CheckCreate transaction that created this check.
32    fn sequence(&self) -> Result<u32> {
33        ledger_object::get_field(self.get_slot_num(), sfield::Sequence)
34    }
35
36    /// A hint indicating which page of the sender's owner directory links to this object, in case
37    /// the directory consists of multiple pages.
38    fn owner_node(&self) -> Result<u64> {
39        ledger_object::get_field(self.get_slot_num(), sfield::OwnerNode)
40    }
41
42    /// A hint indicating which page of the destination's owner directory links to this object, in
43    /// case the directory consists of multiple pages.
44    fn destination_node(&self) -> Result<u64> {
45        ledger_object::get_field(self.get_slot_num(), sfield::DestinationNode)
46    }
47
48    /// Indicates the time after which this Check is considered expired. See Specifying Time for
49    /// details.
50    fn expiration(&self) -> Result<Option<u32>> {
51        ledger_object::get_field_optional(self.get_slot_num(), sfield::Expiration)
52    }
53
54    /// Arbitrary 256-bit hash provided by the sender as a specific reason or identifier for this
55    /// Check.
56    fn invoice_id(&self) -> Result<Option<Hash256>> {
57        ledger_object::get_field_optional(self.get_slot_num(), sfield::InvoiceID)
58    }
59
60    /// An arbitrary tag to further specify the source for this Check, such as a hosted recipient at
61    /// the sender's address.
62    fn source_tag(&self) -> Result<Option<u32>> {
63        ledger_object::get_field_optional(self.get_slot_num(), sfield::SourceTag)
64    }
65
66    /// An arbitrary tag to further specify the destination for this Check, such as a hosted
67    /// recipient at the destination address.
68    fn destination_tag(&self) -> Result<Option<u32>> {
69        ledger_object::get_field_optional(self.get_slot_num(), sfield::DestinationTag)
70    }
71
72    /// The identifying hash of the transaction that most recently modified this object.
73    fn previous_txn_id(&self) -> Result<Hash256> {
74        ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnID)
75    }
76
77    /// The index of the ledger that contains the transaction that most recently modified this
78    /// object.
79    fn previous_txn_lgr_seq(&self) -> Result<u32> {
80        ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnLgrSeq)
81    }
82}
83
84/// Trait providing access to fields specific to the current Check object.
85pub trait CurrentCheckFields: CurrentLedgerObjectCommonFields {
86    /// The sender of the Check. Cashing the Check debits this address's balance.
87    fn account(&self) -> Result<AccountID> {
88        current_ledger_object::get_field(sfield::Account)
89    }
90
91    /// The intended recipient of the Check. Only this address can cash the Check, using a CheckCash
92    /// transaction.
93    fn destination(&self) -> Result<AccountID> {
94        current_ledger_object::get_field(sfield::Destination)
95    }
96
97    /// The maximum amount of currency this Check can debit the sender. If the Check is successfully
98    /// cashed, the destination is credited in the same currency for up to this amount.
99    fn send_max(&self) -> Result<Amount> {
100        current_ledger_object::get_field(sfield::SendMax)
101    }
102
103    /// The sequence number of the CheckCreate transaction that created this check.
104    fn sequence(&self) -> Result<u32> {
105        current_ledger_object::get_field(sfield::Sequence)
106    }
107
108    /// A hint indicating which page of the sender's owner directory links to this object, in case
109    /// the directory consists of multiple pages.
110    fn owner_node(&self) -> Result<u64> {
111        current_ledger_object::get_field(sfield::OwnerNode)
112    }
113
114    /// A hint indicating which page of the destination's owner directory links to this object, in
115    /// case the directory consists of multiple pages.
116    fn destination_node(&self) -> Result<u64> {
117        current_ledger_object::get_field(sfield::DestinationNode)
118    }
119
120    /// Indicates the time after which this Check is considered expired. See Specifying Time for
121    /// details.
122    fn expiration(&self) -> Result<Option<u32>> {
123        current_ledger_object::get_field_optional(sfield::Expiration)
124    }
125
126    /// Arbitrary 256-bit hash provided by the sender as a specific reason or identifier for this
127    /// Check.
128    fn invoice_id(&self) -> Result<Option<Hash256>> {
129        current_ledger_object::get_field_optional(sfield::InvoiceID)
130    }
131
132    /// An arbitrary tag to further specify the source for this Check, such as a hosted recipient at
133    /// the sender's address.
134    fn source_tag(&self) -> Result<Option<u32>> {
135        current_ledger_object::get_field_optional(sfield::SourceTag)
136    }
137
138    /// An arbitrary tag to further specify the destination for this Check, such as a hosted
139    /// recipient at the destination address.
140    fn destination_tag(&self) -> Result<Option<u32>> {
141        current_ledger_object::get_field_optional(sfield::DestinationTag)
142    }
143
144    /// The identifying hash of the transaction that most recently modified this object.
145    fn previous_txn_id(&self) -> Result<Hash256> {
146        current_ledger_object::get_field(sfield::PreviousTxnID)
147    }
148
149    /// The index of the ledger that contains the transaction that most recently modified this
150    /// object.
151    fn previous_txn_lgr_seq(&self) -> Result<u32> {
152        current_ledger_object::get_field(sfield::PreviousTxnLgrSeq)
153    }
154}
155
156#[derive(Debug, Clone, Copy, Eq, PartialEq)]
157pub struct Check {
158    pub(crate) slot_num: i32,
159}
160
161impl Check {
162    /// Binds this handle to a host-managed slot holding a Check ledger object.
163    pub fn new(slot_num: i32) -> Self {
164        Self { slot_num }
165    }
166}
167
168impl LedgerObjectCommonFields for Check {
169    fn get_slot_num(&self) -> i32 {
170        self.slot_num
171    }
172}
173
174impl CheckFields for Check {}
175
176#[cfg(test)]
177mod tests {
178    use super::*;
179    use crate::host::host_bindings_trait::MockHostBindings;
180    use crate::host::setup_mock;
181    use crate::objects::test_utils::*;
182
183    #[test]
184    fn read_all_fields() {
185        let mut mock = MockHostBindings::new();
186        mock_all_fields_present(&mut mock);
187        let _guard = setup_mock(mock);
188
189        let obj = Check::new(0);
190
191        assert!(obj.account().is_ok());
192        assert!(obj.destination().is_ok());
193        assert!(obj.send_max().is_ok());
194        assert!(obj.sequence().is_ok());
195        assert!(obj.owner_node().is_ok());
196        assert!(obj.destination_node().is_ok());
197        assert!(obj.previous_txn_id().is_ok());
198        assert!(obj.previous_txn_lgr_seq().is_ok());
199        assert!(obj.expiration().is_ok());
200        assert!(obj.invoice_id().is_ok());
201        assert!(obj.source_tag().is_ok());
202        assert!(obj.destination_tag().is_ok());
203    }
204
205    #[test]
206    fn optional_fields_none() {
207        let mut mock = MockHostBindings::new();
208        mock_all_fields_not_found(&mut mock);
209        let _guard = setup_mock(mock);
210
211        let obj = Check::new(0);
212
213        assert!(obj.expiration().unwrap().is_none());
214        assert!(obj.invoice_id().unwrap().is_none());
215        assert!(obj.source_tag().unwrap().is_none());
216        assert!(obj.destination_tag().unwrap().is_none());
217    }
218}