xrpl_api/objects/
offer.rs

1use crate::Amount;
2use enumflags2::{bitflags, BitFlags};
3use serde::{Deserialize, Serialize};
4use xrpl_types::LedgerTimestamp;
5
6/// The object was placed as a passive offer
7pub const LSF_PASSIVE: u32 = 0x00010000;
8
9/// The object was placed as a sell offer
10pub const LSF_SELL: u32 = 0x00020000;
11
12/// An offer in the ledger.
13///
14/// <https://xrpl.org/offer.html>
15///
16/// {
17///     "Account": "rBqb89MRQJnMPq8wTwEbtz4kvxrEDfcYvt",
18///     "BookDirectory": "ACC27DE91DBA86FC509069EAF4BC511D73128B780F2E54BF5E07A369E2446000",
19///     "BookNode": "0000000000000000",
20///     "Flags": 131072,
21///     "LedgerEntryType": "Offer",
22///     "OwnerNode": "0000000000000000",
23///     "PreviousTxnID": "F0AB71E777B2DA54B86231E19B82554EF1F8211F92ECA473121C655BFC5329BF",
24///     "PreviousTxnLgrSeq": 14524914,
25///     "Sequence": 866,
26///     "TakerGets": {
27///         "currency": "XAG",
28///         "issuer": "r9Dr5xwkeLegBeXq6ujinjSBLQzQ1zQGjH",
29///         "value": "37"
30///     },
31///     "TakerPays": "79550000000",
32///     "index": "96F76F27D8A327FC48753167EC04A46AA0E382E6F57F32FD12274144D00F1797"
33/// }
34#[derive(Debug, Clone, Deserialize, Serialize)]
35#[serde(rename_all = "PascalCase")]
36pub struct Offer {
37    pub account: String,
38    pub book_directory: String,
39    pub book_node: Option<String>,
40    pub expiration: Option<LedgerTimestamp>,
41    pub flags: BitFlags<OfferFlags>,
42    pub owner_node: String,
43    pub sequence: u32,
44    pub taker_gets: Amount,
45    pub taker_pays: Amount,
46    /// Declared optional since it is not part of transaction metadata fields <https://xrpl.org/transaction-metadata.html#modifiednode-fields>
47    #[serde(rename = "PreviousTxnID")]
48    pub previous_txn_id: Option<String>,
49    /// Declared optional since it is not part of transaction metadata fields <https://xrpl.org/transaction-metadata.html#modifiednode-fields>
50    pub previous_txn_lgr_seq: Option<u32>,
51    #[serde(rename = "index")]
52    pub index: Option<String>,
53
54    /// `owner_funds` is present in offers returned by `book_offers` method, see
55    /// <https://xrpl.org/book_offers.html#response-format>.
56    #[serde(rename = "owner_funds")]
57    pub owner_funds: Option<String>,
58    /// `taker_gets_funded` may be present in offers returned by `book_offers` method, see
59    /// <https://xrpl.org/book_offers.html#response-format>.
60    #[serde(rename = "taker_gets_funded")]
61    pub taker_gets_funded: Option<Amount>,
62    /// taker_pays_funded may be present in offers returned by `book_offers` method, see
63    /// <https://xrpl.org/book_offers.html#response-format>.
64    #[serde(rename = "taker_pays_funded")]
65    pub taker_pays_funded: Option<Amount>,
66    /// `quality` is present in offers returned by `book_offers` method, see
67    /// <https://xrpl.org/book_offers.html#response-format>.
68    #[serde(rename = "quality")]
69    pub quality: Option<String>,
70}
71
72#[bitflags]
73#[repr(u32)]
74#[derive(Debug, Copy, Clone, Eq, PartialEq)]
75pub enum OfferFlags {
76    Passive = 0x00010000,
77    Sell = 0x00020000,
78}
79
80#[cfg(test)]
81mod test {
82    use super::*;
83
84    #[test]
85    fn test_deserialize_offer() {
86        let json = r#"
87{
88    "Account": "rBqb89MRQJnMPq8wTwEbtz4kvxrEDfcYvt",
89    "BookDirectory": "ACC27DE91DBA86FC509069EAF4BC511D73128B780F2E54BF5E07A369E2446000",
90    "BookNode": "0000000000000000",
91    "Flags": 131072,
92    "LedgerEntryType": "Offer",
93    "OwnerNode": "0000000000000000",
94    "PreviousTxnID": "F0AB71E777B2DA54B86231E19B82554EF1F8211F92ECA473121C655BFC5329BF",
95    "PreviousTxnLgrSeq": 14524914,
96    "Sequence": 866,
97    "TakerGets": {
98        "currency": "XAG",
99        "issuer": "r9Dr5xwkeLegBeXq6ujinjSBLQzQ1zQGjH",
100        "value": "37"
101    },
102    "TakerPays": "79550000000",
103    "index": "96F76F27D8A327FC48753167EC04A46AA0E382E6F57F32FD12274144D00F1797"
104}
105"#;
106
107        let _offer: Offer = serde_json::from_str(json).unwrap();
108    }
109}