Skip to main content

xrpl_common_stdlib/objects/generated/
amm.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::issue::Issue;
11use crate::types::uint::Hash256;
12
13/// Trait providing access to fields specific to AMM objects in any ledger.
14pub trait AMMFields: LedgerObjectCommonFields {
15    /// The address of the special account that holds this AMM's assets.
16    fn account(&self) -> Result<AccountID> {
17        ledger_object::get_field(self.get_slot_num(), sfield::Account)
18    }
19
20    /// The percentage fee to be charged for trades against this AMM instance, in units of
21    /// 1/100,000. The maximum value is 1000, for a 1% fee.
22    fn trading_fee(&self) -> Result<Option<u16>> {
23        ledger_object::get_field_optional(self.get_slot_num(), sfield::TradingFee)
24    }
25
26    /// The total outstanding balance of liquidity provider tokens from this AMM instance. The
27    /// holders of these tokens can vote on the AMM's trading fee in proportion to their holdings,
28    /// or redeem the tokens for a share of the AMM's assets which grows with the trading fees
29    /// collected.
30    fn lp_token_balance(&self) -> Result<Amount> {
31        ledger_object::get_field(self.get_slot_num(), sfield::LPTokenBalance)
32    }
33
34    /// The definition for one of the two assets this AMM holds. In JSON, this is an object with
35    /// `currency` and `issuer` fields.
36    fn asset(&self) -> Result<Issue> {
37        ledger_object::get_field(self.get_slot_num(), sfield::Asset)
38    }
39
40    /// The definition for the other asset this AMM holds. In JSON, this is an object with
41    /// `currency` and `issuer` fields.
42    fn asset2(&self) -> Result<Issue> {
43        ledger_object::get_field(self.get_slot_num(), sfield::Asset2)
44    }
45
46    /// The OwnerNode field (Required).
47    fn owner_node(&self) -> Result<u64> {
48        ledger_object::get_field(self.get_slot_num(), sfield::OwnerNode)
49    }
50
51    /// The identifying hash of the transaction that most recently modified this entry.
52    fn previous_txn_id(&self) -> Result<Option<Hash256>> {
53        ledger_object::get_field_optional(self.get_slot_num(), sfield::PreviousTxnID)
54    }
55
56    /// The index of the ledger that contains the transaction that most recently modified this
57    /// entry.
58    fn previous_txn_lgr_seq(&self) -> Result<Option<u32>> {
59        ledger_object::get_field_optional(self.get_slot_num(), sfield::PreviousTxnLgrSeq)
60    }
61}
62
63/// Trait providing access to fields specific to the current AMM object.
64pub trait CurrentAMMFields: CurrentLedgerObjectCommonFields {
65    /// The address of the special account that holds this AMM's assets.
66    fn account(&self) -> Result<AccountID> {
67        current_ledger_object::get_field(sfield::Account)
68    }
69
70    /// The percentage fee to be charged for trades against this AMM instance, in units of
71    /// 1/100,000. The maximum value is 1000, for a 1% fee.
72    fn trading_fee(&self) -> Result<Option<u16>> {
73        current_ledger_object::get_field_optional(sfield::TradingFee)
74    }
75
76    /// The total outstanding balance of liquidity provider tokens from this AMM instance. The
77    /// holders of these tokens can vote on the AMM's trading fee in proportion to their holdings,
78    /// or redeem the tokens for a share of the AMM's assets which grows with the trading fees
79    /// collected.
80    fn lp_token_balance(&self) -> Result<Amount> {
81        current_ledger_object::get_field(sfield::LPTokenBalance)
82    }
83
84    /// The definition for one of the two assets this AMM holds. In JSON, this is an object with
85    /// `currency` and `issuer` fields.
86    fn asset(&self) -> Result<Issue> {
87        current_ledger_object::get_field(sfield::Asset)
88    }
89
90    /// The definition for the other asset this AMM holds. In JSON, this is an object with
91    /// `currency` and `issuer` fields.
92    fn asset2(&self) -> Result<Issue> {
93        current_ledger_object::get_field(sfield::Asset2)
94    }
95
96    /// The OwnerNode field (Required).
97    fn owner_node(&self) -> Result<u64> {
98        current_ledger_object::get_field(sfield::OwnerNode)
99    }
100
101    /// The identifying hash of the transaction that most recently modified this entry.
102    fn previous_txn_id(&self) -> Result<Option<Hash256>> {
103        current_ledger_object::get_field_optional(sfield::PreviousTxnID)
104    }
105
106    /// The index of the ledger that contains the transaction that most recently modified this
107    /// entry.
108    fn previous_txn_lgr_seq(&self) -> Result<Option<u32>> {
109        current_ledger_object::get_field_optional(sfield::PreviousTxnLgrSeq)
110    }
111}
112
113#[derive(Debug, Clone, Copy, Eq, PartialEq)]
114pub struct AMM {
115    pub(crate) slot_num: i32,
116}
117
118impl AMM {
119    /// Binds this handle to a host-managed slot holding an AMM ledger object.
120    pub fn new(slot_num: i32) -> Self {
121        Self { slot_num }
122    }
123}
124
125impl LedgerObjectCommonFields for AMM {
126    fn get_slot_num(&self) -> i32 {
127        self.slot_num
128    }
129}
130
131impl AMMFields for AMM {}
132
133#[cfg(test)]
134mod tests {
135    use super::*;
136    use crate::host::host_bindings_trait::MockHostBindings;
137    use crate::host::setup_mock;
138    use crate::objects::test_utils::*;
139
140    #[test]
141    fn read_all_fields() {
142        let mut mock = MockHostBindings::new();
143        mock_all_fields_present(&mut mock);
144        let _guard = setup_mock(mock);
145
146        let obj = AMM::new(0);
147
148        assert!(obj.account().is_ok());
149        assert!(obj.lp_token_balance().is_ok());
150        assert!(obj.asset().is_ok());
151        assert!(obj.asset2().is_ok());
152        assert!(obj.owner_node().is_ok());
153        assert!(obj.trading_fee().is_ok());
154        assert!(obj.previous_txn_id().is_ok());
155        assert!(obj.previous_txn_lgr_seq().is_ok());
156    }
157
158    #[test]
159    fn optional_fields_none() {
160        let mut mock = MockHostBindings::new();
161        mock_all_fields_not_found(&mut mock);
162        let _guard = setup_mock(mock);
163
164        let obj = AMM::new(0);
165
166        assert!(obj.trading_fee().unwrap().is_none());
167        assert!(obj.previous_txn_id().unwrap().is_none());
168        assert!(obj.previous_txn_lgr_seq().unwrap().is_none());
169    }
170}