Skip to main content

xrpl/models/requests/
transaction_entry.rs

1use alloc::borrow::Cow;
2use serde::{Deserialize, Serialize};
3use serde_with::skip_serializing_none;
4
5use crate::models::{requests::RequestMethod, Model};
6
7use super::{CommonFields, LedgerIndex, LookupByLedgerRequest, Request};
8
9/// The transaction_entry method retrieves information on a
10/// single transaction from a specific ledger version.
11/// (The tx method, by contrast, searches all ledgers for
12/// the specified transaction. We recommend using that
13/// method instead.)
14///
15/// See Transaction Entry:
16/// `<https://xrpl.org/transaction_entry.html>`
17#[skip_serializing_none]
18#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
19pub struct TransactionEntry<'a> {
20    /// The common fields shared by all requests.
21    #[serde(flatten)]
22    pub common_fields: CommonFields<'a>,
23    /// Unique hash of the transaction you are looking up.
24    pub tx_hash: Cow<'a, str>,
25    /// The unique identifier of a ledger.
26    #[serde(flatten)]
27    pub ledger_lookup: Option<LookupByLedgerRequest<'a>>,
28}
29
30impl<'a> Model for TransactionEntry<'a> {}
31
32impl<'a> Request<'a> for TransactionEntry<'a> {
33    fn get_common_fields(&self) -> &CommonFields<'a> {
34        &self.common_fields
35    }
36
37    fn get_common_fields_mut(&mut self) -> &mut CommonFields<'a> {
38        &mut self.common_fields
39    }
40}
41
42impl<'a> TransactionEntry<'a> {
43    pub fn new(
44        id: Option<Cow<'a, str>>,
45        tx_hash: Cow<'a, str>,
46        ledger_hash: Option<Cow<'a, str>>,
47        ledger_index: Option<LedgerIndex<'a>>,
48    ) -> Self {
49        Self {
50            common_fields: CommonFields {
51                command: RequestMethod::TransactionEntry,
52                id,
53            },
54            tx_hash,
55            ledger_lookup: Some(LookupByLedgerRequest {
56                ledger_hash,
57                ledger_index,
58            }),
59        }
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn test_serde_round_trip() {
69        let req = TransactionEntry::new(
70            Some("te-1".into()),
71            "C53ECF838647FA5A4C780377025FEC7999AB4182590510CA461444B207AB74A9".into(),
72            None,
73            Some(LedgerIndex::Int(56865245)),
74        );
75        let serialized = serde_json::to_string(&req).unwrap();
76        let deserialized: TransactionEntry = serde_json::from_str(&serialized).unwrap();
77        assert_eq!(req, deserialized);
78        assert!(serialized.contains("\"command\":\"transaction_entry\""));
79    }
80}