waves_rust/model/transaction/
lease_cancel_transaction.rs

1use crate::error::{Error, Result};
2use crate::model::{ByteString, Id, LeaseInfo};
3use crate::util::JsonDeserializer;
4use crate::waves_proto::LeaseCancelTransactionData;
5use serde_json::{Map, Value};
6use std::borrow::Borrow;
7
8const TYPE: u8 = 9;
9
10#[derive(Clone, Eq, PartialEq, Debug)]
11pub struct LeaseCancelTransactionInfo {
12    lease_id: Id,
13    lease_info: LeaseInfo,
14}
15
16impl LeaseCancelTransactionInfo {
17    pub fn new(lease_id: Id, lease_info: LeaseInfo) -> Self {
18        Self {
19            lease_id,
20            lease_info,
21        }
22    }
23
24    pub fn lease_id(&self) -> Id {
25        self.lease_id.clone()
26    }
27
28    pub fn lease_info(&self) -> LeaseInfo {
29        self.lease_info.clone()
30    }
31}
32
33impl TryFrom<&Value> for LeaseCancelTransactionInfo {
34    type Error = Error;
35
36    fn try_from(value: &Value) -> Result<Self> {
37        let lease_id = JsonDeserializer::safe_to_string_from_field(value, "leaseId")?;
38
39        let lease_info: LeaseInfo = value["lease"].borrow().try_into()?;
40        Ok(LeaseCancelTransactionInfo {
41            lease_id: Id::from_string(&lease_id)?,
42            lease_info,
43        })
44    }
45}
46
47#[derive(Clone, Eq, PartialEq, Debug)]
48pub struct LeaseCancelTransaction {
49    lease_id: Id,
50}
51
52impl LeaseCancelTransaction {
53    pub fn new(lease_id: Id) -> Self {
54        Self { lease_id }
55    }
56
57    pub fn lease_id(&self) -> Id {
58        self.lease_id.clone()
59    }
60
61    pub fn tx_type() -> u8 {
62        TYPE
63    }
64}
65
66impl TryFrom<&Value> for LeaseCancelTransaction {
67    type Error = Error;
68
69    fn try_from(value: &Value) -> Result<Self> {
70        let lease_id = JsonDeserializer::safe_to_string_from_field(value, "leaseId")?;
71
72        Ok(LeaseCancelTransaction {
73            lease_id: Id::from_string(&lease_id)?,
74        })
75    }
76}
77
78impl TryFrom<&LeaseCancelTransaction> for Map<String, Value> {
79    type Error = Error;
80
81    fn try_from(value: &LeaseCancelTransaction) -> Result<Self> {
82        let mut lease_cancel_tx_json = Map::new();
83        lease_cancel_tx_json.insert("leaseId".to_owned(), value.lease_id.encoded().into());
84        Ok(lease_cancel_tx_json)
85    }
86}
87
88impl TryFrom<&LeaseCancelTransaction> for LeaseCancelTransactionData {
89    type Error = Error;
90
91    fn try_from(value: &LeaseCancelTransaction) -> Result<Self> {
92        Ok(LeaseCancelTransactionData {
93            lease_id: value.lease_id.bytes(),
94        })
95    }
96}
97
98#[cfg(test)]
99mod tests {
100    use crate::error::Result;
101    use crate::model::{
102        ByteString, Id, LeaseCancelTransaction, LeaseCancelTransactionInfo, LeaseStatus,
103    };
104    use crate::waves_proto::LeaseCancelTransactionData;
105    use serde_json::{json, Map, Value};
106    use std::borrow::Borrow;
107    use std::fs;
108
109    #[test]
110    fn test_json_to_lease_cancel_transaction() -> Result<()> {
111        let data = fs::read_to_string("./tests/resources/lease_cancel_rs.json")
112            .expect("Unable to read file");
113        let json: Value = serde_json::from_str(&data).expect("failed to generate json from str");
114
115        let lease_cancel_from_json: LeaseCancelTransactionInfo = json.borrow().try_into().unwrap();
116
117        assert_eq!(
118            "5EWudZk4xXaqRezrh26zqjbNeAzvEzDATjs4paKdyhGy",
119            lease_cancel_from_json.lease_id().encoded()
120        );
121
122        let lease_info_from_json = lease_cancel_from_json.lease_info();
123        assert_eq!(
124            "5EWudZk4xXaqRezrh26zqjbNeAzvEzDATjs4paKdyhGy",
125            lease_info_from_json.id().encoded()
126        );
127        assert_eq!(
128            "5EWudZk4xXaqRezrh26zqjbNeAzvEzDATjs4paKdyhGy",
129            lease_info_from_json.origin_transaction_id().encoded()
130        );
131        assert_eq!(
132            "3Mq3pueXcAgLcuWvJzJ4ndRHfqYgjUZvL7q",
133            lease_info_from_json.sender().encoded()
134        );
135        assert_eq!(
136            "3MxtrLkrbcG28uTvmbKmhrwGrR65ooHVYvK",
137            lease_info_from_json.recipient().encoded()
138        );
139        assert_eq!(100, lease_info_from_json.amount());
140        assert_eq!(2218886, lease_info_from_json.height());
141        assert_eq!(LeaseStatus::Canceled, lease_info_from_json.status());
142        assert_eq!(Some(2218925), lease_info_from_json.cancel_height());
143        assert_eq!(
144            Some("FoPVrSqzK74bwt8hgCDsEb48HJv7g2nvjeCW5wBoWpXb".to_owned()),
145            lease_info_from_json
146                .cancel_transaction_id()
147                .map(|it| it.encoded())
148        );
149        Ok(())
150    }
151
152    #[test]
153    fn test_lease_cancel_to_proto() -> Result<()> {
154        let lease_cancel_tx = &LeaseCancelTransaction::new(Id::from_string(
155            "5EWudZk4xXaqRezrh26zqjbNeAzvEzDATjs4paKdyhGy",
156        )?);
157        let proto: LeaseCancelTransactionData = lease_cancel_tx.try_into()?;
158
159        assert_eq!(proto.lease_id, lease_cancel_tx.lease_id().bytes());
160        Ok(())
161    }
162
163    #[test]
164    fn test_lease_cancel_to_json() -> Result<()> {
165        let lease_cancel_tx = &LeaseCancelTransaction::new(Id::from_string(
166            "5EWudZk4xXaqRezrh26zqjbNeAzvEzDATjs4paKdyhGy",
167        )?);
168
169        let map: Map<String, Value> = lease_cancel_tx.try_into()?;
170        let json: Value = map.into();
171        let expected_json = json!({
172            "leaseId": "5EWudZk4xXaqRezrh26zqjbNeAzvEzDATjs4paKdyhGy",
173        });
174        assert_eq!(expected_json, json);
175        Ok(())
176    }
177}