Skip to main content

xrpl/models/requests/
account_objects.rs

1use alloc::borrow::Cow;
2use serde::{Deserialize, Serialize};
3use serde_with::skip_serializing_none;
4use strum_macros::{Display, EnumString};
5
6use crate::models::{requests::RequestMethod, Model};
7
8use super::{CommonFields, LedgerIndex, LookupByLedgerRequest, Marker, Request};
9
10/// Represents the object types that an AccountObjects
11/// Request can ask for.
12#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize, Display, EnumString)]
13#[strum(serialize_all = "snake_case")]
14#[serde(rename_all = "snake_case")]
15pub enum AccountObjectType {
16    Check,
17    #[serde(rename = "did")]
18    #[strum(serialize = "did")]
19    DID,
20    Credential,
21    DepositPreauth,
22    Escrow,
23    Offer,
24    Oracle,
25    PaymentChannel,
26    PermissionedDomain,
27    SignerList,
28    State,
29    Ticket,
30    /// Filter for MPTokenIssuance objects (MPT issuances created by this account).
31    MptIssuance,
32    /// Filter for MPToken objects (MPT holdings owned by this account).
33    Mptoken,
34    /// Filter for Vault ledger objects (XLS-65 SingleAssetVault).
35    Vault,
36}
37
38/// This request returns the raw ledger format for all objects
39/// owned by an account. For a higher-level view of an account's
40/// trust lines and balances, see AccountLines Request instead.
41///
42/// See Account Objects:
43/// `<https://xrpl.org/account_objects.html>`
44#[skip_serializing_none]
45#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
46pub struct AccountObjects<'a> {
47    /// The common fields shared by all requests.
48    #[serde(flatten)]
49    pub common_fields: CommonFields<'a>,
50    /// A unique identifier for the account, most commonly the
51    /// account's address.
52    pub account: Cow<'a, str>,
53    /// The unique identifier of a ledger.
54    #[serde(flatten)]
55    pub ledger_lookup: Option<LookupByLedgerRequest<'a>>,
56    /// If included, filter results to include only this type
57    /// of ledger object. The valid types are: check, deposit_preauth,
58    /// escrow, offer, payment_channel, signer_list, ticket,
59    /// and state (trust line).
60    pub r#type: Option<AccountObjectType>,
61    /// If true, the response only includes objects that would block
62    /// this account from being deleted. The default is false.
63    pub deletion_blockers_only: Option<bool>,
64    /// The maximum number of objects to include in the results.
65    /// Must be within the inclusive range 10 to 400 on non-admin
66    /// connections. The default is 200.
67    pub limit: Option<u16>,
68    /// Value from a previous paginated response. Resume retrieving
69    /// data where that response left off.
70    pub marker: Option<Marker<'a>>,
71}
72
73impl<'a> Model for AccountObjects<'a> {}
74
75impl<'a> Request<'a> for AccountObjects<'a> {
76    fn get_common_fields(&self) -> &CommonFields<'a> {
77        &self.common_fields
78    }
79
80    fn get_common_fields_mut(&mut self) -> &mut CommonFields<'a> {
81        &mut self.common_fields
82    }
83}
84
85impl<'a> AccountObjects<'a> {
86    pub fn new(
87        id: Option<Cow<'a, str>>,
88        account: Cow<'a, str>,
89        ledger_hash: Option<Cow<'a, str>>,
90        ledger_index: Option<LedgerIndex<'a>>,
91        r#type: Option<AccountObjectType>,
92        deletion_blockers_only: Option<bool>,
93        limit: Option<u16>,
94        marker: Option<Marker<'a>>,
95    ) -> Self {
96        Self {
97            common_fields: CommonFields {
98                command: RequestMethod::AccountObjects,
99                id,
100            },
101            account,
102            ledger_lookup: Some(LookupByLedgerRequest {
103                ledger_hash,
104                ledger_index,
105            }),
106            r#type,
107            deletion_blockers_only,
108            limit,
109            marker,
110        }
111    }
112}
113
114#[cfg(test)]
115mod tests {
116    use super::*;
117    use crate::utils::testing::test_constants::*;
118
119    #[test]
120    fn test_serde_round_trip() {
121        let req = AccountObjects::new(
122            Some("ao-1".into()),
123            ACCOUNT_GENESIS.into(),
124            None,
125            None,
126            Some(AccountObjectType::Escrow),
127            Some(true),
128            Some(20),
129            None,
130        );
131        let serialized = serde_json::to_string(&req).unwrap();
132        let deserialized: AccountObjects = serde_json::from_str(&serialized).unwrap();
133        assert_eq!(req, deserialized);
134        assert!(serialized.contains("\"command\":\"account_objects\""));
135        assert!(serialized.contains("\"type\":\"escrow\""));
136    }
137
138    #[test]
139    fn test_serde_mpt_variants() {
140        let req_issuance = AccountObjects {
141            common_fields: CommonFields {
142                command: RequestMethod::AccountObjects,
143                id: None,
144            },
145            account: ACCOUNT_GENESIS.into(),
146            ledger_lookup: None,
147            r#type: Some(AccountObjectType::MptIssuance),
148            deletion_blockers_only: None,
149            limit: None,
150            marker: None,
151        };
152        let serialized_issuance = serde_json::to_string(&req_issuance).unwrap();
153        assert!(serialized_issuance.contains("\"type\":\"mpt_issuance\""));
154
155        let req_token = AccountObjects {
156            common_fields: CommonFields {
157                command: RequestMethod::AccountObjects,
158                id: None,
159            },
160            account: ACCOUNT_GENESIS.into(),
161            ledger_lookup: None,
162            r#type: Some(AccountObjectType::Mptoken),
163            deletion_blockers_only: None,
164            limit: None,
165            marker: None,
166        };
167        let serialized_token = serde_json::to_string(&req_token).unwrap();
168        assert!(serialized_token.contains("\"type\":\"mptoken\""));
169    }
170}