Skip to main content

xrpl_common_stdlib/objects/generated/
sponsorship.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 Sponsorship objects in any ledger.
13pub trait SponsorshipFields: LedgerObjectCommonFields {
14    /// The identifying hash of the transaction that most recently modified this entry.
15    fn previous_txn_id(&self) -> Result<Hash256> {
16        ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnID)
17    }
18
19    /// The ledger index of the ledger that contains the transaction that most recently modified
20    /// this entry.
21    fn previous_txn_lgr_seq(&self) -> Result<u32> {
22        ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnLgrSeq)
23    }
24
25    /// The address of the sponsor account. This account pays the reserve for this entry.
26    fn owner(&self) -> Result<AccountID> {
27        ledger_object::get_field(self.get_slot_num(), sfield::Owner)
28    }
29
30    /// The address of the sponsee account associated with this relationship.
31    fn sponsee(&self) -> Result<AccountID> {
32        ledger_object::get_field(self.get_slot_num(), sfield::Sponsee)
33    }
34
35    /// The remaining amount of XRP that the sponsor has provided for the sponsee to use for
36    /// transaction fees. The sponsor adjusts this amount using the `FeeAmountDelta` field of a
37    /// SponsorshipSet transaction.
38    fn fee_amount(&self) -> Result<Option<Amount>> {
39        ledger_object::get_field_optional(self.get_slot_num(), sfield::FeeAmount)
40    }
41
42    /// The maximum fee per transaction that the sponsor will cover. This field helps prevent
43    /// excessive draining of the pre-funded fee pool. If the sponsee submits a transaction with a
44    /// fee exceeding this value, the transaction fails.
45    fn max_fee(&self) -> Result<Option<Amount>> {
46        ledger_object::get_field_optional(self.get_slot_num(), sfield::MaxFee)
47    }
48
49    /// The remaining number of owner reserves the sponsor has pre-funded for the sponsee. The
50    /// sponsor adjusts this count using the `RemainingOwnerCountDelta` field of a SponsorshipSet
51    /// transaction.
52    fn remaining_owner_count(&self) -> Result<Option<u32>> {
53        ledger_object::get_field_optional(self.get_slot_num(), sfield::RemainingOwnerCount)
54    }
55
56    /// A hint indicating which page of the sponsor's owner directory links to this entry, in case
57    /// the directory consists of multiple pages.
58    fn owner_node(&self) -> Result<u64> {
59        ledger_object::get_field(self.get_slot_num(), sfield::OwnerNode)
60    }
61
62    /// A hint indicating which page of the sponsee's owner directory links to this entry, in case
63    /// the directory consists of multiple pages.
64    fn sponsee_node(&self) -> Result<u64> {
65        ledger_object::get_field(self.get_slot_num(), sfield::SponseeNode)
66    }
67}
68
69/// Trait providing access to fields specific to the current Sponsorship object.
70pub trait CurrentSponsorshipFields: CurrentLedgerObjectCommonFields {
71    /// The identifying hash of the transaction that most recently modified this entry.
72    fn previous_txn_id(&self) -> Result<Hash256> {
73        current_ledger_object::get_field(sfield::PreviousTxnID)
74    }
75
76    /// The ledger index of the ledger that contains the transaction that most recently modified
77    /// this entry.
78    fn previous_txn_lgr_seq(&self) -> Result<u32> {
79        current_ledger_object::get_field(sfield::PreviousTxnLgrSeq)
80    }
81
82    /// The address of the sponsor account. This account pays the reserve for this entry.
83    fn owner(&self) -> Result<AccountID> {
84        current_ledger_object::get_field(sfield::Owner)
85    }
86
87    /// The address of the sponsee account associated with this relationship.
88    fn sponsee(&self) -> Result<AccountID> {
89        current_ledger_object::get_field(sfield::Sponsee)
90    }
91
92    /// The remaining amount of XRP that the sponsor has provided for the sponsee to use for
93    /// transaction fees. The sponsor adjusts this amount using the `FeeAmountDelta` field of a
94    /// SponsorshipSet transaction.
95    fn fee_amount(&self) -> Result<Option<Amount>> {
96        current_ledger_object::get_field_optional(sfield::FeeAmount)
97    }
98
99    /// The maximum fee per transaction that the sponsor will cover. This field helps prevent
100    /// excessive draining of the pre-funded fee pool. If the sponsee submits a transaction with a
101    /// fee exceeding this value, the transaction fails.
102    fn max_fee(&self) -> Result<Option<Amount>> {
103        current_ledger_object::get_field_optional(sfield::MaxFee)
104    }
105
106    /// The remaining number of owner reserves the sponsor has pre-funded for the sponsee. The
107    /// sponsor adjusts this count using the `RemainingOwnerCountDelta` field of a SponsorshipSet
108    /// transaction.
109    fn remaining_owner_count(&self) -> Result<Option<u32>> {
110        current_ledger_object::get_field_optional(sfield::RemainingOwnerCount)
111    }
112
113    /// A hint indicating which page of the sponsor's owner directory links to this entry, in case
114    /// the directory consists of multiple pages.
115    fn owner_node(&self) -> Result<u64> {
116        current_ledger_object::get_field(sfield::OwnerNode)
117    }
118
119    /// A hint indicating which page of the sponsee's owner directory links to this entry, in case
120    /// the directory consists of multiple pages.
121    fn sponsee_node(&self) -> Result<u64> {
122        current_ledger_object::get_field(sfield::SponseeNode)
123    }
124}
125
126#[derive(Debug, Clone, Copy, Eq, PartialEq)]
127pub struct Sponsorship {
128    pub(crate) slot_num: i32,
129}
130
131impl Sponsorship {
132    /// Binds this handle to a host-managed slot holding a Sponsorship ledger object.
133    pub fn new(slot_num: i32) -> Self {
134        Self { slot_num }
135    }
136}
137
138impl LedgerObjectCommonFields for Sponsorship {
139    fn get_slot_num(&self) -> i32 {
140        self.slot_num
141    }
142}
143
144impl SponsorshipFields for Sponsorship {}
145
146#[cfg(test)]
147mod tests {
148    use super::*;
149    use crate::host::host_bindings_trait::MockHostBindings;
150    use crate::host::setup_mock;
151    use crate::objects::test_utils::*;
152
153    #[test]
154    fn read_all_fields() {
155        let mut mock = MockHostBindings::new();
156        mock_all_fields_present(&mut mock);
157        let _guard = setup_mock(mock);
158
159        let obj = Sponsorship::new(0);
160
161        assert!(obj.previous_txn_id().is_ok());
162        assert!(obj.previous_txn_lgr_seq().is_ok());
163        assert!(obj.owner().is_ok());
164        assert!(obj.sponsee().is_ok());
165        assert!(obj.owner_node().is_ok());
166        assert!(obj.sponsee_node().is_ok());
167        assert!(obj.fee_amount().is_ok());
168        assert!(obj.max_fee().is_ok());
169        assert!(obj.remaining_owner_count().is_ok());
170    }
171
172    #[test]
173    fn optional_fields_none() {
174        let mut mock = MockHostBindings::new();
175        mock_all_fields_not_found(&mut mock);
176        let _guard = setup_mock(mock);
177
178        let obj = Sponsorship::new(0);
179
180        assert!(obj.remaining_owner_count().unwrap().is_none());
181    }
182}