use crate::models::ledger::objects::LedgerEntryType;
use crate::models::FlagCollection;
use crate::models::NoFlags;
use crate::models::{amount::Amount, Model};
use alloc::borrow::Cow;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use super::{CommonFields, LedgerObject};
#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct Escrow<'a> {
#[serde(flatten)]
pub common_fields: CommonFields<'a, NoFlags>,
pub account: Cow<'a, str>,
pub amount: Amount<'a>,
pub destination: Cow<'a, str>,
pub owner_node: Cow<'a, str>,
#[serde(rename = "PreviousTxnID")]
pub previous_txn_id: Cow<'a, str>,
pub previous_txn_lgr_seq: u32,
pub cancel_after: Option<u32>,
pub condition: Option<Cow<'a, str>>,
pub destination_node: Option<Cow<'a, str>>,
pub destination_tag: Option<u32>,
pub finish_after: Option<u32>,
pub source_tag: Option<u32>,
}
impl<'a> Model for Escrow<'a> {}
impl<'a> LedgerObject<NoFlags> for Escrow<'a> {
fn get_ledger_entry_type(&self) -> LedgerEntryType {
self.common_fields.get_ledger_entry_type()
}
}
impl<'a> Escrow<'a> {
pub fn new(
index: Option<Cow<'a, str>>,
ledger_index: Option<Cow<'a, str>>,
account: Cow<'a, str>,
amount: Amount<'a>,
destination: Cow<'a, str>,
owner_node: Cow<'a, str>,
previous_txn_id: Cow<'a, str>,
previous_txn_lgr_seq: u32,
cancel_after: Option<u32>,
condition: Option<Cow<'a, str>>,
destination_node: Option<Cow<'a, str>>,
destination_tag: Option<u32>,
finish_after: Option<u32>,
source_tag: Option<u32>,
) -> Self {
Self {
common_fields: CommonFields {
flags: FlagCollection::default(),
ledger_entry_type: LedgerEntryType::Escrow,
index,
ledger_index,
},
account,
amount,
destination,
owner_node,
previous_txn_id,
previous_txn_lgr_seq,
cancel_after,
condition,
destination_node,
destination_tag,
finish_after,
source_tag,
}
}
}
#[cfg(test)]
mod test_serde {
use super::*;
use alloc::borrow::Cow;
#[test]
fn test_serialize() {
let escrow = Escrow::new(
Some(Cow::from(
"DC5F3851D8A1AB622F957761E5963BC5BD439D5C24AC6AD7AC4523F0640244AC",
)),
None,
Cow::from("rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn"),
Amount::XRPAmount("10000".into()),
Cow::from("ra5nK24KXen9AHvsdFTKHSANinZseWnPcX"),
Cow::from("0000000000000000"),
Cow::from("C44F2EB84196B9AD820313DBEBA6316A15C9A2D35787579ED172B87A30131DA7"),
28991004,
Some(545440232),
Some(Cow::from(
"A0258020A82A88B2DF843A54F58772E4A3861866ECDB4157645DD9AE528C1D3AEEDABAB6810120",
)),
Some(Cow::from("0000000000000000")),
Some(23480),
Some(545354132),
Some(11747),
);
let serialized = serde_json::to_string(&escrow).unwrap();
let deserialized: Escrow = serde_json::from_str(&serialized).unwrap();
assert_eq!(escrow, deserialized);
}
}