xrpl_api/api/
ledger.rs

1//! <https://xrpl.org/ledger.html>
2
3use crate::{types::Transaction, Request, RetrieveLedgerSpec, ReturnLedgerSpec, WithLedgerSpec};
4use serde::{Deserialize, Serialize};
5use xrpl_types::LedgerTimestamp;
6
7// #TODO refactor to make the two variants internal!
8// #TODO add tests
9
10#[derive(Default, Debug, Clone, Serialize)]
11pub struct ExpandLedgerRequest {
12    #[serde(flatten)]
13    pub ledger_request: LedgerRequest,
14}
15
16#[derive(Default, Debug, Clone, Serialize)]
17pub struct LedgerRequest {
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub full: Option<bool>,
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub accounts: Option<bool>,
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub transactions: Option<bool>,
24    #[serde(skip_serializing_if = "Option::is_none")]
25    expand: Option<bool>,
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub owner_funds: Option<bool>,
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub binary: Option<bool>,
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub queue: Option<bool>,
32    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
33    pub object_type: Option<ObjectType>,
34    #[serde(flatten)]
35    pub ledger_spec: RetrieveLedgerSpec,
36}
37
38#[derive(Clone, Debug, Copy, Eq, PartialEq, Serialize, Deserialize)]
39#[serde(rename_all = "snake_case")]
40pub enum ObjectType {
41    Account,
42    Amendments,
43    Amm,
44    Check,
45    DepositPreauth,
46    Directory,
47    Escrow,
48    Fee,
49    Hashes,
50    NftOffer,
51    NftPage,
52    Offer,
53    PaymentChannel,
54    SignerList,
55    State,
56    Ticket,
57}
58
59impl Request for LedgerRequest {
60    type Response = LedgerResponse<String>;
61
62    fn method(&self) -> String {
63        "ledger".to_owned()
64    }
65}
66
67impl WithLedgerSpec for LedgerRequest {
68    fn as_ledger_spec(&self) -> &crate::RetrieveLedgerSpec {
69        &self.ledger_spec
70    }
71
72    fn as_ledger_spec_mut(&mut self) -> &mut crate::RetrieveLedgerSpec {
73        &mut self.ledger_spec
74    }
75}
76
77impl LedgerRequest {
78    pub fn new() -> Self {
79        Self {
80            expand: Some(false),
81            ..Default::default()
82        }
83    }
84
85    pub fn transactions(self, transactions: bool) -> Self {
86        Self {
87            transactions: Some(transactions),
88            ..self
89        }
90    }
91
92    pub fn expanded(self) -> ExpandLedgerRequest {
93        ExpandLedgerRequest {
94            ledger_request: LedgerRequest {
95                expand: Some(true),
96                ..self
97            },
98        }
99    }
100}
101
102impl Request for ExpandLedgerRequest {
103    type Response = LedgerResponse<Transaction>;
104
105    fn method(&self) -> String {
106        "ledger".to_owned()
107    }
108}
109
110#[derive(Debug, Deserialize)]
111pub struct LedgerResponse<TransactionType> {
112    /// (Omitted unless requested with the queue parameter) Array of objects describing queued transactions, in the same order as the queue.
113    /// If the request specified expand as true, members contain full representations of the transactions, in either JSON or binary depending on whether the request specified binary as true.
114    // pub queue_data: Vec<???>,
115    /// The complete header data of this ledger.
116    pub ledger: Ledger<TransactionType>,
117    #[serde(flatten)]
118    pub ledger_spec: ReturnLedgerSpec,
119}
120
121#[derive(Debug, Deserialize)]
122pub struct Ledger<TransactionType> {
123    /// Hash of all account state information in this ledger, as hex.
124    pub account_hash: String,
125    pub close_flags: u64,
126    /// The time this ledger was closed, in seconds since the Ripple Epoch.
127    pub close_time: LedgerTimestamp,
128    /// The time this ledger was closed, in human-readable format. Always uses the UTC time zone.
129    pub close_time_human: String,
130    /// Ledger close times are rounded to within this many seconds.
131    pub close_time_resolution: u32,
132    /// Whether or not this ledger has been closed.
133    pub closed: bool,
134    /// Unique identifying hash of the entire ledger.
135    pub ledger_hash: String,
136    /// The Ledger Index of this ledger, as a quoted integer.
137    pub ledger_index: String,
138    /// The time at which the previous ledger was closed.
139    pub parent_close_time: LedgerTimestamp,
140    /// Unique identifying hash of the ledger that came immediately before this one.
141    pub parent_hash: String,
142    /// Total number of XRP drops in the network, as a quoted integer. (This decreases as transaction costs destroy XRP.)
143    pub total_coins: String,
144    /// Hash of the transaction information included in this ledger, as hex
145    pub transaction_hash: String,
146    /// (Omitted unless requested) Transactions applied in this ledger version.
147    /// By default, members are the transactions identifying Hash strings. If the request specified expand as true,
148    /// members are full representations of the transactions instead,
149    /// in either JSON or binary depending on whether the request specified binary as true.
150    pub transactions: Option<Vec<TransactionType>>,
151}