Skip to main content

xrpl_common_stdlib/objects/generated/
delegate.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::uint::Hash256;
10
11/// Trait providing access to fields specific to Delegate objects in any ledger.
12pub trait DelegateFields: LedgerObjectCommonFields {
13    /// The account delegating permissions to another, also called the _delegating account_.
14    fn account(&self) -> Result<AccountID> {
15        ledger_object::get_field(self.get_slot_num(), sfield::Account)
16    }
17
18    /// The account receiving permissions, also called the _delegate_.
19    fn authorize(&self) -> Result<AccountID> {
20        ledger_object::get_field(self.get_slot_num(), sfield::Authorize)
21    }
22
23    /// A hint indicating which page of the delegating account's owner directory links to this
24    /// object, in case the directory consists of multiple pages.
25    fn owner_node(&self) -> Result<u64> {
26        ledger_object::get_field(self.get_slot_num(), sfield::OwnerNode)
27    }
28
29    /// A hint indicating which page of the delegate's owner directory links to this object, in case
30    /// the directory consists of multiple pages.
31    fn destination_node(&self) -> Result<Option<u64>> {
32        ledger_object::get_field_optional(self.get_slot_num(), sfield::DestinationNode)
33    }
34
35    /// The identifying hash of the transaction that most recently modified this object.
36    fn previous_txn_id(&self) -> Result<Hash256> {
37        ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnID)
38    }
39
40    /// The index of the ledger that contains the transaction that most recently modified this
41    /// object.
42    fn previous_txn_lgr_seq(&self) -> Result<u32> {
43        ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnLgrSeq)
44    }
45}
46
47/// Trait providing access to fields specific to the current Delegate object.
48pub trait CurrentDelegateFields: CurrentLedgerObjectCommonFields {
49    /// The account delegating permissions to another, also called the _delegating account_.
50    fn account(&self) -> Result<AccountID> {
51        current_ledger_object::get_field(sfield::Account)
52    }
53
54    /// The account receiving permissions, also called the _delegate_.
55    fn authorize(&self) -> Result<AccountID> {
56        current_ledger_object::get_field(sfield::Authorize)
57    }
58
59    /// A hint indicating which page of the delegating account's owner directory links to this
60    /// object, in case the directory consists of multiple pages.
61    fn owner_node(&self) -> Result<u64> {
62        current_ledger_object::get_field(sfield::OwnerNode)
63    }
64
65    /// A hint indicating which page of the delegate's owner directory links to this object, in case
66    /// the directory consists of multiple pages.
67    fn destination_node(&self) -> Result<Option<u64>> {
68        current_ledger_object::get_field_optional(sfield::DestinationNode)
69    }
70
71    /// The identifying hash of the transaction that most recently modified this object.
72    fn previous_txn_id(&self) -> Result<Hash256> {
73        current_ledger_object::get_field(sfield::PreviousTxnID)
74    }
75
76    /// The index of the ledger that contains the transaction that most recently modified this
77    /// object.
78    fn previous_txn_lgr_seq(&self) -> Result<u32> {
79        current_ledger_object::get_field(sfield::PreviousTxnLgrSeq)
80    }
81}
82
83#[derive(Debug, Clone, Copy, Eq, PartialEq)]
84pub struct Delegate {
85    pub(crate) slot_num: i32,
86}
87
88impl Delegate {
89    /// Binds this handle to a host-managed slot holding a Delegate ledger object.
90    pub fn new(slot_num: i32) -> Self {
91        Self { slot_num }
92    }
93}
94
95impl LedgerObjectCommonFields for Delegate {
96    fn get_slot_num(&self) -> i32 {
97        self.slot_num
98    }
99}
100
101impl DelegateFields for Delegate {}
102
103#[cfg(test)]
104mod tests {
105    use super::*;
106    use crate::host::host_bindings_trait::MockHostBindings;
107    use crate::host::setup_mock;
108    use crate::objects::test_utils::*;
109
110    #[test]
111    fn read_all_fields() {
112        let mut mock = MockHostBindings::new();
113        mock_all_fields_present(&mut mock);
114        let _guard = setup_mock(mock);
115
116        let obj = Delegate::new(0);
117
118        assert!(obj.account().is_ok());
119        assert!(obj.authorize().is_ok());
120        assert!(obj.owner_node().is_ok());
121        assert!(obj.previous_txn_id().is_ok());
122        assert!(obj.previous_txn_lgr_seq().is_ok());
123        assert!(obj.destination_node().is_ok());
124    }
125
126    #[test]
127    fn optional_fields_none() {
128        let mut mock = MockHostBindings::new();
129        mock_all_fields_not_found(&mut mock);
130        let _guard = setup_mock(mock);
131
132        let obj = Delegate::new(0);
133
134        assert!(obj.destination_node().unwrap().is_none());
135    }
136}