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
use crate::error::{Error, Result};
use crate::model::{ApplicationStatus, Id};
use crate::util::JsonDeserializer;
use serde_json::Value;

#[derive(Clone, Eq, PartialEq, Debug)]
pub struct TransactionStatus {
    id: Id,
    status: Status,
    app_status: Option<ApplicationStatus>,
    height: u32,
    confirmation: u32,
}

impl TransactionStatus {
    pub fn new(
        id: Id,
        status: Status,
        app_status: Option<ApplicationStatus>,
        height: u32,
        confirmation: u32,
    ) -> Self {
        Self {
            id,
            status,
            app_status,
            height,
            confirmation,
        }
    }

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

    pub fn status(&self) -> Status {
        self.status
    }

    pub fn app_status(&self) -> Option<ApplicationStatus> {
        self.app_status
    }

    pub fn height(&self) -> u32 {
        self.height
    }

    pub fn confirmation(&self) -> u32 {
        self.confirmation
    }
}

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

    fn try_from(value: &Value) -> Result<Self> {
        let status = JsonDeserializer::safe_to_string_from_field(value, "status")?;
        let tx_status = match status.as_str() {
            "not_found" => Status::NotFound,
            "unconfirmed" => Status::Unconfirmed,
            "confirmed" => Status::Confirmed,
            _ => Status::Unknown,
        };

        let id = JsonDeserializer::safe_to_string_from_field(value, "id")?;

        let application_status = match value["applicationStatus"].as_str() {
            Some(status) => match status {
                "succeeded" => Some(ApplicationStatus::Succeed),
                "script_execution_failed" => Some(ApplicationStatus::ScriptExecutionFailed),
                &_ => Some(ApplicationStatus::Unknown),
            },
            None => None,
        };

        let height = JsonDeserializer::safe_to_int_from_field(value, "height")?;
        let confirmations = JsonDeserializer::safe_to_int_from_field(value, "confirmations")?;

        Ok(TransactionStatus {
            id: Id::from_string(&id)?,
            status: tx_status,
            app_status: application_status,
            height: height as u32,
            confirmation: confirmations as u32,
        })
    }
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Status {
    NotFound,
    Unconfirmed,
    Confirmed,
    Unknown,
}

#[cfg(test)]
mod tests {
    use crate::error::Result;
    use crate::model::{ApplicationStatus, ByteString, Status, TransactionStatus};

    use serde_json::Value;
    use std::borrow::Borrow;
    use std::fs;

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

        let transaction_status: TransactionStatus = json.borrow().try_into()?;

        assert_eq!(transaction_status.status(), Status::Confirmed);
        assert_eq!(transaction_status.height(), 2217333);
        assert_eq!(transaction_status.confirmation(), 14051);
        assert_eq!(
            transaction_status.app_status().unwrap(),
            ApplicationStatus::Succeed
        );
        assert_eq!(
            transaction_status.id().encoded(),
            "4XFVLLMBjBMPwGivgyLhw374kViANoToLAYUdEXWLsBJ"
        );
        Ok(())
    }
}