xrpl_api/api/
tx.rs

1//! The `tx` method retrieves information on a single transaction, by its
2//! identifying hash.
3//!
4//! <https://xrpl.org/tx.html>
5
6use crate::{types::Transaction, Request};
7use serde::{Deserialize, Serialize};
8
9#[derive(Default, Debug, Clone, Serialize)]
10pub struct TxRequest {
11    /// The 256-bit hash of the transaction, as hex.
12    transaction: String,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    binary: Option<bool>,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    min_ledger: Option<u32>,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    max_ledger: Option<u32>,
19}
20
21impl Request for TxRequest {
22    type Response = TxResponse;
23
24    fn method(&self) -> String {
25        "tx".to_owned()
26    }
27}
28
29impl TxRequest {
30    pub fn new(transaction: &str) -> Self {
31        Self {
32            transaction: transaction.to_owned(),
33            ..Default::default()
34        }
35    }
36
37    pub fn binary(self, binary: bool) -> Self {
38        Self {
39            binary: Some(binary),
40            ..self
41        }
42    }
43}
44
45#[derive(Debug, Deserialize)]
46pub struct TxResponse {
47    #[serde(flatten)]
48    pub tx: Transaction,
49}
50
51#[cfg(test)]
52mod test {
53    use super::*;
54
55    #[test]
56    fn test_deserialize_tx_response() {
57        let json = r#"
58{
59    "Account": "rhhh49pFH96roGyuC4E5P4CHaNjS1k8gzM",
60    "Fee": "12",
61    "Flags": 0,
62    "LastLedgerSequence": 56865248,
63    "OfferSequence": 5037708,
64    "Sequence": 5037710,
65    "SigningPubKey": "03B51A3EDF70E4098DA7FB053A01C5A6A0A163A30ED1445F14F87C7C3295FCB3BE",
66    "TakerGets": "15000000000",
67    "TakerPays": {
68      "currency": "CNY",
69      "issuer": "rKiCet8SdvWxPXnAgYarFUXMh1zCPz432Y",
70      "value": "20160.75"
71    },
72    "TransactionType": "OfferCreate",
73    "TxnSignature": "3045022100A5023A0E64923616FCDB6D664F569644C7C9D1895772F986CD6B981B515B02A00220530C973E9A8395BC6FE2484948D2751F6B030FC7FB8575D1BFB406368AD554D9",
74    "date": 648248020,
75    "hash": "C53ECF838647FA5A4C780377025FEC7999AB4182590510CA461444B207AB74A9",
76    "inLedger": 56865245,
77    "ledger_index": 56865245,
78    "meta": {
79      "AffectedNodes": [
80        {
81          "ModifiedNode": {
82            "FinalFields": {
83              "ExchangeRate": "4F04C66806CF7400",
84              "Flags": 0,
85              "RootIndex": "02BAAC1E67C1CE0E96F0FA2E8061020536CEDD043FEB0FF54F04C66806CF7400",
86              "TakerGetsCurrency": "0000000000000000000000000000000000000000",
87              "TakerGetsIssuer": "0000000000000000000000000000000000000000",
88              "TakerPaysCurrency": "000000000000000000000000434E590000000000",
89              "TakerPaysIssuer": "CED6E99370D5C00EF4EBF72567DA99F5661BFB3A"
90            },
91            "LedgerEntryType": "DirectoryNode",
92            "LedgerIndex": "02BAAC1E67C1CE0E96F0FA2E8061020536CEDD043FEB0FF54F04C66806CF7400"
93          }
94        },
95        {
96          "ModifiedNode": {
97            "FinalFields": {
98              "Account": "rhhh49pFH96roGyuC4E5P4CHaNjS1k8gzM",
99              "Balance": "10404767991",
100              "Flags": 0,
101              "OwnerCount": 3,
102              "Sequence": 5037711
103            },
104            "LedgerEntryType": "AccountRoot",
105            "LedgerIndex": "1DECD9844E95FFBA273F1B94BA0BF2564DDF69F2804497A6D7837B52050174A2",
106            "PreviousFields": {
107              "Balance": "10404768003",
108              "Sequence": 5037710
109            },
110            "PreviousTxnID": "4DC47B246B5EB9CCE92ABA8C482479E3BF1F946CABBEF74CA4DE36521D5F9008",
111            "PreviousTxnLgrSeq": 56865244
112          }
113        },
114        {
115          "DeletedNode": {
116            "FinalFields": {
117              "Account": "rhhh49pFH96roGyuC4E5P4CHaNjS1k8gzM",
118              "BookDirectory": "02BAAC1E67C1CE0E96F0FA2E8061020536CEDD043FEB0FF54F04C66806CF7400",
119              "BookNode": "0000000000000000",
120              "Flags": 0,
121              "OwnerNode": "0000000000000000",
122              "PreviousTxnID": "8F5FF57B404827F12BDA7561876A13C3E3B3095CBF75334DBFB5F227391A660C",
123              "PreviousTxnLgrSeq": 56865244,
124              "Sequence": 5037708,
125              "TakerGets": "15000000000",
126              "TakerPays": {
127                "currency": "CNY",
128                "issuer": "rKiCet8SdvWxPXnAgYarFUXMh1zCPz432Y",
129                "value": "20160.75"
130              }
131            },
132            "LedgerEntryType": "Offer",
133            "LedgerIndex": "26AAE6CA8D29E28A47C92ADF22D5D96A0216F0551E16936856DDC8CB1AAEE93B"
134          }
135        },
136        {
137          "ModifiedNode": {
138            "FinalFields": {
139              "Flags": 0,
140              "IndexNext": "0000000000000000",
141              "IndexPrevious": "0000000000000000",
142              "Owner": "rhhh49pFH96roGyuC4E5P4CHaNjS1k8gzM",
143              "RootIndex": "47FAF5D102D8CE655574F440CDB97AC67C5A11068BB3759E87C2B9745EE94548"
144            },
145            "LedgerEntryType": "DirectoryNode",
146            "LedgerIndex": "47FAF5D102D8CE655574F440CDB97AC67C5A11068BB3759E87C2B9745EE94548"
147          }
148        },
149        {
150          "CreatedNode": {
151            "LedgerEntryType": "Offer",
152            "LedgerIndex": "8BAEE3C7DE04A568E96007420FA11ABD0BC9AE44D35932BB5640E9C3FB46BC9B",
153            "NewFields": {
154              "Account": "rhhh49pFH96roGyuC4E5P4CHaNjS1k8gzM",
155              "BookDirectory": "02BAAC1E67C1CE0E96F0FA2E8061020536CEDD043FEB0FF54F04C66806CF7400",
156              "Sequence": 5037710,
157              "TakerGets": "15000000000",
158              "TakerPays": {
159                "currency": "CNY",
160                "issuer": "rKiCet8SdvWxPXnAgYarFUXMh1zCPz432Y",
161                "value": "20160.75"
162              }
163            }
164          }
165        }
166      ],
167      "TransactionIndex": 0,
168      "TransactionResult": "tesSUCCESS"
169    },
170    "validated": true
171}
172"#;
173
174        let _tx_response: TxResponse = serde_json::from_str(json).unwrap();
175    }
176}