Skip to main content

xrpl_common_stdlib/objects/generated/
offer.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 Offer objects in any ledger.
13pub trait OfferFields: LedgerObjectCommonFields {
14    /// The account that owns this offer.
15    fn account(&self) -> Result<AccountID> {
16        ledger_object::get_field(self.get_slot_num(), sfield::Account)
17    }
18
19    /// The `Sequence` value of the OfferCreate transaction that created this offer. Used in
20    /// combination with the `Account` to identify this offer.
21    fn sequence(&self) -> Result<u32> {
22        ledger_object::get_field(self.get_slot_num(), sfield::Sequence)
23    }
24
25    /// The remaining amount and type of currency requested by the offer creator.
26    fn taker_pays(&self) -> Result<Amount> {
27        ledger_object::get_field(self.get_slot_num(), sfield::TakerPays)
28    }
29
30    /// The remaining amount and type of currency being provided by the offer creator.
31    fn taker_gets(&self) -> Result<Amount> {
32        ledger_object::get_field(self.get_slot_num(), sfield::TakerGets)
33    }
34
35    /// The ID of the offer directory that links to this offer.
36    fn book_directory(&self) -> Result<Hash256> {
37        ledger_object::get_field(self.get_slot_num(), sfield::BookDirectory)
38    }
39
40    /// A hint indicating which page of the offer directory links to this entry, in case the
41    /// directory consists of multiple pages.
42    fn book_node(&self) -> Result<u64> {
43        ledger_object::get_field(self.get_slot_num(), sfield::BookNode)
44    }
45
46    /// A hint indicating which page of the owner directory links to this entry, in case the
47    /// directory consists of multiple pages.
48    fn owner_node(&self) -> Result<u64> {
49        ledger_object::get_field(self.get_slot_num(), sfield::OwnerNode)
50    }
51
52    /// The identifying hash of the transaction that most recently modified this entry.
53    fn previous_txn_id(&self) -> Result<Hash256> {
54        ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnID)
55    }
56
57    /// The index of the ledger that contains the transaction that most recently modified this
58    /// object.
59    fn previous_txn_lgr_seq(&self) -> Result<u32> {
60        ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnLgrSeq)
61    }
62
63    /// Indicates the time after which this offer is considered unfunded. See Specifying Time for
64    /// details.
65    fn expiration(&self) -> Result<Option<u32>> {
66        ledger_object::get_field_optional(self.get_slot_num(), sfield::Expiration)
67    }
68
69    /// The ledger entry ID of a permissioned domain. If present, this offer belongs to the
70    /// corresponding Permissioned DEX.
71    fn domain_id(&self) -> Result<Option<Hash256>> {
72        ledger_object::get_field_optional(self.get_slot_num(), sfield::DomainID)
73    }
74}
75
76/// Trait providing access to fields specific to the current Offer object.
77pub trait CurrentOfferFields: CurrentLedgerObjectCommonFields {
78    /// The account that owns this offer.
79    fn account(&self) -> Result<AccountID> {
80        current_ledger_object::get_field(sfield::Account)
81    }
82
83    /// The `Sequence` value of the OfferCreate transaction that created this offer. Used in
84    /// combination with the `Account` to identify this offer.
85    fn sequence(&self) -> Result<u32> {
86        current_ledger_object::get_field(sfield::Sequence)
87    }
88
89    /// The remaining amount and type of currency requested by the offer creator.
90    fn taker_pays(&self) -> Result<Amount> {
91        current_ledger_object::get_field(sfield::TakerPays)
92    }
93
94    /// The remaining amount and type of currency being provided by the offer creator.
95    fn taker_gets(&self) -> Result<Amount> {
96        current_ledger_object::get_field(sfield::TakerGets)
97    }
98
99    /// The ID of the offer directory that links to this offer.
100    fn book_directory(&self) -> Result<Hash256> {
101        current_ledger_object::get_field(sfield::BookDirectory)
102    }
103
104    /// A hint indicating which page of the offer directory links to this entry, in case the
105    /// directory consists of multiple pages.
106    fn book_node(&self) -> Result<u64> {
107        current_ledger_object::get_field(sfield::BookNode)
108    }
109
110    /// A hint indicating which page of the owner directory links to this entry, in case the
111    /// directory consists of multiple pages.
112    fn owner_node(&self) -> Result<u64> {
113        current_ledger_object::get_field(sfield::OwnerNode)
114    }
115
116    /// The identifying hash of the transaction that most recently modified this entry.
117    fn previous_txn_id(&self) -> Result<Hash256> {
118        current_ledger_object::get_field(sfield::PreviousTxnID)
119    }
120
121    /// The index of the ledger that contains the transaction that most recently modified this
122    /// object.
123    fn previous_txn_lgr_seq(&self) -> Result<u32> {
124        current_ledger_object::get_field(sfield::PreviousTxnLgrSeq)
125    }
126
127    /// Indicates the time after which this offer is considered unfunded. See Specifying Time for
128    /// details.
129    fn expiration(&self) -> Result<Option<u32>> {
130        current_ledger_object::get_field_optional(sfield::Expiration)
131    }
132
133    /// The ledger entry ID of a permissioned domain. If present, this offer belongs to the
134    /// corresponding Permissioned DEX.
135    fn domain_id(&self) -> Result<Option<Hash256>> {
136        current_ledger_object::get_field_optional(sfield::DomainID)
137    }
138}
139
140#[derive(Debug, Clone, Copy, Eq, PartialEq)]
141pub struct Offer {
142    pub(crate) slot_num: i32,
143}
144
145impl Offer {
146    /// Binds this handle to a host-managed slot holding an Offer ledger object.
147    pub fn new(slot_num: i32) -> Self {
148        Self { slot_num }
149    }
150}
151
152impl LedgerObjectCommonFields for Offer {
153    fn get_slot_num(&self) -> i32 {
154        self.slot_num
155    }
156}
157
158impl OfferFields for Offer {}
159
160#[cfg(test)]
161mod tests {
162    use super::*;
163    use crate::host::host_bindings_trait::MockHostBindings;
164    use crate::host::setup_mock;
165    use crate::objects::test_utils::*;
166
167    #[test]
168    fn read_all_fields() {
169        let mut mock = MockHostBindings::new();
170        mock_all_fields_present(&mut mock);
171        let _guard = setup_mock(mock);
172
173        let obj = Offer::new(0);
174
175        assert!(obj.account().is_ok());
176        assert!(obj.sequence().is_ok());
177        assert!(obj.taker_pays().is_ok());
178        assert!(obj.taker_gets().is_ok());
179        assert!(obj.book_directory().is_ok());
180        assert!(obj.book_node().is_ok());
181        assert!(obj.owner_node().is_ok());
182        assert!(obj.previous_txn_id().is_ok());
183        assert!(obj.previous_txn_lgr_seq().is_ok());
184        assert!(obj.expiration().is_ok());
185        assert!(obj.domain_id().is_ok());
186    }
187
188    #[test]
189    fn optional_fields_none() {
190        let mut mock = MockHostBindings::new();
191        mock_all_fields_not_found(&mut mock);
192        let _guard = setup_mock(mock);
193
194        let obj = Offer::new(0);
195
196        assert!(obj.expiration().unwrap().is_none());
197        assert!(obj.domain_id().unwrap().is_none());
198    }
199}