Skip to main content

xrpl/models/transactions/
did_delete.rs

1use alloc::borrow::Cow;
2use alloc::vec::Vec;
3use serde::{Deserialize, Serialize};
4use serde_with::skip_serializing_none;
5
6use crate::models::amount::XRPAmount;
7use crate::models::{
8    transactions::{Memo, Signer, Transaction, TransactionType},
9    Model,
10};
11use crate::models::{FlagCollection, NoFlags};
12
13use super::{CommonFields, CommonTransactionBuilder};
14
15/// Delete the DID (Decentralized Identifier) associated with the
16/// sending account.
17///
18/// See DIDDelete:
19/// `<https://xrpl.org/docs/references/protocol/transactions/types/diddelete>`
20#[skip_serializing_none]
21#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq, Clone)]
22#[serde(rename_all = "PascalCase")]
23pub struct DIDDelete<'a> {
24    /// The base fields for all transaction models.
25    ///
26    /// See Transaction Common Fields:
27    /// `<https://xrpl.org/transaction-common-fields.html>`
28    #[serde(flatten)]
29    pub common_fields: CommonFields<'a, NoFlags>,
30}
31
32impl<'a> Model for DIDDelete<'a> {}
33
34impl<'a> Transaction<'a, NoFlags> for DIDDelete<'a> {
35    fn get_transaction_type(&self) -> &TransactionType {
36        self.common_fields.get_transaction_type()
37    }
38
39    fn get_common_fields(&self) -> &CommonFields<'_, NoFlags> {
40        self.common_fields.get_common_fields()
41    }
42
43    fn get_mut_common_fields(&mut self) -> &mut CommonFields<'a, NoFlags> {
44        self.common_fields.get_mut_common_fields()
45    }
46}
47
48impl<'a> CommonTransactionBuilder<'a, NoFlags> for DIDDelete<'a> {
49    fn get_mut_common_fields(&mut self) -> &mut CommonFields<'a, NoFlags> {
50        &mut self.common_fields
51    }
52
53    fn into_self(self) -> Self {
54        self
55    }
56}
57
58impl<'a> DIDDelete<'a> {
59    pub fn new(
60        account: Cow<'a, str>,
61        account_txn_id: Option<Cow<'a, str>>,
62        fee: Option<XRPAmount<'a>>,
63        last_ledger_sequence: Option<u32>,
64        memos: Option<Vec<Memo>>,
65        sequence: Option<u32>,
66        signers: Option<Vec<Signer>>,
67        source_tag: Option<u32>,
68        ticket_sequence: Option<u32>,
69    ) -> Self {
70        Self {
71            common_fields: CommonFields::new(
72                account,
73                TransactionType::DIDDelete,
74                account_txn_id,
75                fee,
76                Some(FlagCollection::default()),
77                last_ledger_sequence,
78                memos,
79                None,
80                sequence,
81                signers,
82                None,
83                source_tag,
84                ticket_sequence,
85                None,
86            ),
87        }
88    }
89}
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94
95    #[test]
96    fn test_valid() {
97        let tx = DIDDelete {
98            common_fields: CommonFields {
99                account: "rp4pqYgrTAtdPHuZd1ZQWxrzx45jxYcZex".into(),
100                transaction_type: TransactionType::DIDDelete,
101                ..Default::default()
102            },
103        };
104        assert!(tx.is_valid());
105    }
106
107    #[test]
108    fn test_serialize() {
109        let tx = DIDDelete {
110            common_fields: CommonFields {
111                account: "rp4pqYgrTAtdPHuZd1ZQWxrzx45jxYcZex".into(),
112                transaction_type: TransactionType::DIDDelete,
113                fee: Some("12".into()),
114                sequence: Some(391),
115                signing_pub_key: Some(
116                    "0293A815C095DBA82FAC597A6BB9D338674DB93168156D84D18417AD509FFF5904".into(),
117                ),
118                ..Default::default()
119            },
120        };
121
122        let expected_json = r#"{"Account":"rp4pqYgrTAtdPHuZd1ZQWxrzx45jxYcZex","TransactionType":"DIDDelete","Fee":"12","Flags":0,"Sequence":391,"SigningPubKey":"0293A815C095DBA82FAC597A6BB9D338674DB93168156D84D18417AD509FFF5904"}"#;
123
124        let serialized = serde_json::to_string(&tx).unwrap();
125        let expected_value = serde_json::to_value(expected_json).unwrap();
126        let serialized_value = serde_json::to_value(&serialized).unwrap();
127        assert_eq!(serialized_value, expected_value);
128
129        let deserialized: DIDDelete = serde_json::from_str(expected_json).unwrap();
130        assert_eq!(tx, deserialized);
131    }
132
133    #[test]
134    fn test_builder_pattern() {
135        let tx = DIDDelete {
136            common_fields: CommonFields {
137                account: "rp4pqYgrTAtdPHuZd1ZQWxrzx45jxYcZex".into(),
138                transaction_type: TransactionType::DIDDelete,
139                ..Default::default()
140            },
141        }
142        .with_fee("12".into())
143        .with_sequence(391)
144        .with_last_ledger_sequence(7108682);
145
146        assert_eq!(tx.common_fields.fee.as_ref().unwrap().0, "12");
147        assert_eq!(tx.common_fields.sequence, Some(391));
148        assert_eq!(tx.common_fields.last_ledger_sequence, Some(7108682));
149    }
150
151    #[test]
152    fn test_default() {
153        let tx = DIDDelete {
154            common_fields: CommonFields {
155                account: "rp4pqYgrTAtdPHuZd1ZQWxrzx45jxYcZex".into(),
156                transaction_type: TransactionType::DIDDelete,
157                ..Default::default()
158            },
159        };
160
161        assert_eq!(
162            tx.common_fields.account,
163            "rp4pqYgrTAtdPHuZd1ZQWxrzx45jxYcZex"
164        );
165        assert_eq!(
166            tx.common_fields.transaction_type,
167            TransactionType::DIDDelete
168        );
169        assert!(tx.common_fields.fee.is_none());
170        assert!(tx.common_fields.sequence.is_none());
171    }
172}