1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use crate::error::{Error, Result};
use crate::model::{ByteString, Id, LeaseInfo};
use crate::util::JsonDeserializer;
use crate::waves_proto::LeaseCancelTransactionData;
use serde_json::{Map, Value};
use std::borrow::Borrow;

const TYPE: u8 = 9;

#[derive(Clone, Eq, PartialEq, Debug)]
pub struct LeaseCancelTransactionInfo {
    lease_id: Id,
    lease_info: LeaseInfo,
}

impl LeaseCancelTransactionInfo {
    pub fn new(lease_id: Id, lease_info: LeaseInfo) -> Self {
        Self {
            lease_id,
            lease_info,
        }
    }

    pub fn lease_id(&self) -> Id {
        self.lease_id.clone()
    }

    pub fn lease_info(&self) -> LeaseInfo {
        self.lease_info.clone()
    }
}

impl TryFrom<&Value> for LeaseCancelTransactionInfo {
    type Error = Error;

    fn try_from(value: &Value) -> Result<Self> {
        let lease_id = JsonDeserializer::safe_to_string_from_field(value, "leaseId")?;

        let lease_info: LeaseInfo = value["lease"].borrow().try_into()?;
        Ok(LeaseCancelTransactionInfo {
            lease_id: Id::from_string(&lease_id)?,
            lease_info,
        })
    }
}

#[derive(Clone, Eq, PartialEq, Debug)]
pub struct LeaseCancelTransaction {
    lease_id: Id,
}

impl LeaseCancelTransaction {
    pub fn new(lease_id: Id) -> Self {
        Self { lease_id }
    }

    pub fn lease_id(&self) -> Id {
        self.lease_id.clone()
    }

    pub fn tx_type() -> u8 {
        TYPE
    }
}

impl TryFrom<&Value> for LeaseCancelTransaction {
    type Error = Error;

    fn try_from(value: &Value) -> Result<Self> {
        let lease_id = JsonDeserializer::safe_to_string_from_field(value, "leaseId")?;

        Ok(LeaseCancelTransaction {
            lease_id: Id::from_string(&lease_id)?,
        })
    }
}

impl TryFrom<&LeaseCancelTransaction> for Map<String, Value> {
    type Error = Error;

    fn try_from(value: &LeaseCancelTransaction) -> Result<Self> {
        let mut lease_cancel_tx_json = Map::new();
        lease_cancel_tx_json.insert("leaseId".to_owned(), value.lease_id.encoded().into());
        Ok(lease_cancel_tx_json)
    }
}

impl TryFrom<&LeaseCancelTransaction> for LeaseCancelTransactionData {
    type Error = Error;

    fn try_from(value: &LeaseCancelTransaction) -> Result<Self> {
        Ok(LeaseCancelTransactionData {
            lease_id: value.lease_id.bytes(),
        })
    }
}

#[cfg(test)]
mod tests {
    use crate::error::Result;
    use crate::model::{
        ByteString, Id, LeaseCancelTransaction, LeaseCancelTransactionInfo, LeaseStatus,
    };
    use crate::waves_proto::LeaseCancelTransactionData;
    use serde_json::{json, Map, Value};
    use std::borrow::Borrow;
    use std::fs;

    #[test]
    fn test_json_to_lease_cancel_transaction() -> Result<()> {
        let data = fs::read_to_string("./tests/resources/lease_cancel_rs.json")
            .expect("Unable to read file");
        let json: Value = serde_json::from_str(&data).expect("failed to generate json from str");

        let lease_cancel_from_json: LeaseCancelTransactionInfo = json.borrow().try_into().unwrap();

        assert_eq!(
            "5EWudZk4xXaqRezrh26zqjbNeAzvEzDATjs4paKdyhGy",
            lease_cancel_from_json.lease_id().encoded()
        );

        let lease_info_from_json = lease_cancel_from_json.lease_info();
        assert_eq!(
            "5EWudZk4xXaqRezrh26zqjbNeAzvEzDATjs4paKdyhGy",
            lease_info_from_json.id().encoded()
        );
        assert_eq!(
            "5EWudZk4xXaqRezrh26zqjbNeAzvEzDATjs4paKdyhGy",
            lease_info_from_json.origin_transaction_id().encoded()
        );
        assert_eq!(
            "3Mq3pueXcAgLcuWvJzJ4ndRHfqYgjUZvL7q",
            lease_info_from_json.sender().encoded()
        );
        assert_eq!(
            "3MxtrLkrbcG28uTvmbKmhrwGrR65ooHVYvK",
            lease_info_from_json.recipient().encoded()
        );
        assert_eq!(100, lease_info_from_json.amount());
        assert_eq!(2218886, lease_info_from_json.height());
        assert_eq!(LeaseStatus::Canceled, lease_info_from_json.status());
        assert_eq!(Some(2218925), lease_info_from_json.cancel_height());
        assert_eq!(
            Some("FoPVrSqzK74bwt8hgCDsEb48HJv7g2nvjeCW5wBoWpXb".to_owned()),
            lease_info_from_json
                .cancel_transaction_id()
                .map(|it| it.encoded())
        );
        Ok(())
    }

    #[test]
    fn test_lease_cancel_to_proto() -> Result<()> {
        let lease_cancel_tx = &LeaseCancelTransaction::new(Id::from_string(
            "5EWudZk4xXaqRezrh26zqjbNeAzvEzDATjs4paKdyhGy",
        )?);
        let proto: LeaseCancelTransactionData = lease_cancel_tx.try_into()?;

        assert_eq!(proto.lease_id, lease_cancel_tx.lease_id().bytes());
        Ok(())
    }

    #[test]
    fn test_lease_cancel_to_json() -> Result<()> {
        let lease_cancel_tx = &LeaseCancelTransaction::new(Id::from_string(
            "5EWudZk4xXaqRezrh26zqjbNeAzvEzDATjs4paKdyhGy",
        )?);

        let map: Map<String, Value> = lease_cancel_tx.try_into()?;
        let json: Value = map.into();
        let expected_json = json!({
            "leaseId": "5EWudZk4xXaqRezrh26zqjbNeAzvEzDATjs4paKdyhGy",
        });
        assert_eq!(expected_json, json);
        Ok(())
    }
}