waves_rust/model/
lease_info.rs1use crate::error::Error::UnsupportedOperation;
2use crate::error::{Error, Result};
3use crate::model::{Address, Id};
4use crate::util::JsonDeserializer;
5use serde_json::Value;
6
7#[derive(Eq, PartialEq, Clone, Debug)]
8pub struct LeaseInfo {
9 id: Id,
10 origin_transaction_id: Id,
11 sender: Address,
12 recipient: Address,
13 amount: u64,
14 height: u32,
15 status: LeaseStatus,
16 cancel_height: Option<u32>,
17 cancel_transaction_id: Option<Id>,
18}
19
20#[allow(clippy::too_many_arguments)]
21impl LeaseInfo {
22 pub fn new(
23 id: Id,
24 origin_transaction_id: Id,
25 sender: Address,
26 recipient: Address,
27 amount: u64,
28 height: u32,
29 status: LeaseStatus,
30 cancel_height: Option<u32>,
31 cancel_transaction_id: Option<Id>,
32 ) -> LeaseInfo {
33 LeaseInfo {
34 id,
35 origin_transaction_id,
36 sender,
37 recipient,
38 amount,
39 height,
40 status,
41 cancel_height,
42 cancel_transaction_id,
43 }
44 }
45
46 pub fn id(&self) -> Id {
47 self.id.clone()
48 }
49
50 pub fn origin_transaction_id(&self) -> Id {
51 self.origin_transaction_id.clone()
52 }
53
54 pub fn sender(&self) -> Address {
55 self.sender.clone()
56 }
57
58 pub fn recipient(&self) -> Address {
59 self.recipient.clone()
60 }
61
62 pub fn amount(&self) -> u64 {
63 self.amount
64 }
65
66 pub fn height(&self) -> u32 {
67 self.height
68 }
69
70 pub fn status(&self) -> LeaseStatus {
71 self.status.clone()
72 }
73
74 pub fn cancel_height(&self) -> Option<u32> {
75 self.cancel_height
76 }
77
78 pub fn cancel_transaction_id(&self) -> Option<Id> {
79 self.cancel_transaction_id.clone()
80 }
81}
82
83#[derive(Eq, PartialEq, Clone, Debug)]
84pub enum LeaseStatus {
85 Active,
86 Canceled,
87}
88
89impl TryFrom<&Value> for LeaseInfo {
90 type Error = Error;
91
92 fn try_from(value: &Value) -> Result<Self> {
93 let id = JsonDeserializer::safe_to_string_from_field(value, "id")?;
94 let origin_tx_id =
95 JsonDeserializer::safe_to_string_from_field(value, "originTransactionId")?;
96 let sender = JsonDeserializer::safe_to_string_from_field(value, "sender")?;
97 let recipient = JsonDeserializer::safe_to_string_from_field(value, "recipient")?;
98 let amount = JsonDeserializer::safe_to_int_from_field(value, "amount")?;
99 let height = JsonDeserializer::safe_to_int_from_field(value, "height")?;
100 let status = match JsonDeserializer::safe_to_string_from_field(value, "status")?.as_str() {
101 "active" => LeaseStatus::Active,
102 "canceled" => LeaseStatus::Canceled,
103 _ => return Err(UnsupportedOperation("unknown lease status".to_owned())),
104 };
105 let cancel_height = value["cancelHeight"].as_i64();
106 let cancel_tx_id = match value["cancelTransactionId"].as_str() {
107 Some(id) => Some(Id::from_string(id)?),
108 None => None,
109 };
110 Ok(LeaseInfo {
111 id: Id::from_string(&id)?,
112 origin_transaction_id: Id::from_string(&origin_tx_id)?,
113 sender: Address::from_string(&sender)?,
114 recipient: Address::from_string(&recipient)?,
115 amount: amount as u64,
116 height: height as u32,
117 status,
118 cancel_height: cancel_height.map(|it| it as u32),
119 cancel_transaction_id: cancel_tx_id,
120 })
121 }
122}