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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
use crate::error::{Error, Result};
use crate::model::{Address, AssetId, Base64String, Id, PublicKey};
use crate::util::JsonDeserializer;
use serde_json::Value;
use std::borrow::Borrow;

#[derive(Eq, PartialEq, Clone, Debug)]
pub struct AssetDetails {
    asset_id: AssetId,
    issue_height: u32,
    issue_timestamp: u64,
    issuer: Address,
    issuer_public_key: PublicKey,
    name: String,
    description: String,
    decimals: u32,
    reissuable: bool,
    quantity: u64,
    scripted: bool,
    min_sponsored_asset_fee: u64,
    origin_transaction_id: Id,
    script_details: ScriptDetails,
}

#[allow(clippy::too_many_arguments)]
impl AssetDetails {
    pub fn new(
        asset_id: AssetId,
        issue_height: u32,
        issue_timestamp: u64,
        issuer: Address,
        issuer_public_key: PublicKey,
        name: String,
        description: String,
        decimals: u32,
        reissuable: bool,
        quantity: u64,
        scripted: bool,
        min_sponsored_asset_fee: u64,
        origin_transaction_id: Id,
        script_details: ScriptDetails,
    ) -> Self {
        Self {
            asset_id,
            issue_height,
            issue_timestamp,
            issuer,
            issuer_public_key,
            name,
            description,
            decimals,
            reissuable,
            quantity,
            scripted,
            min_sponsored_asset_fee,
            origin_transaction_id,
            script_details,
        }
    }

    pub fn asset_id(&self) -> AssetId {
        self.asset_id.clone()
    }

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

    pub fn issue_timestamp(&self) -> u64 {
        self.issue_timestamp
    }

    pub fn issuer(&self) -> Address {
        self.issuer.clone()
    }

    pub fn issuer_public_key(&self) -> PublicKey {
        self.issuer_public_key.clone()
    }

    pub fn name(&self) -> String {
        self.name.clone()
    }

    pub fn description(&self) -> String {
        self.description.clone()
    }

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

    pub fn reissuable(&self) -> bool {
        self.reissuable
    }

    pub fn quantity(&self) -> u64 {
        self.quantity
    }

    pub fn scripted(&self) -> bool {
        self.scripted
    }

    pub fn min_sponsored_asset_fee(&self) -> u64 {
        self.min_sponsored_asset_fee
    }

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

    pub fn script_details(&self) -> ScriptDetails {
        self.script_details.clone()
    }
}

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

    fn try_from(value: &Value) -> Result<Self> {
        let asset_id = JsonDeserializer::safe_to_string_from_field(value, "assetId")?;
        let issue_height = JsonDeserializer::safe_to_int_from_field(value, "issueHeight")?;
        let issue_timestamp = JsonDeserializer::safe_to_int_from_field(value, "issueTimestamp")?;
        let issuer = JsonDeserializer::safe_to_string_from_field(value, "issuer")?;
        let issuer_public_key =
            JsonDeserializer::safe_to_string_from_field(value, "issuerPublicKey")?;
        let name = JsonDeserializer::safe_to_string_from_field(value, "name")?;
        let description = JsonDeserializer::safe_to_string_from_field(value, "description")?;
        let decimals = JsonDeserializer::safe_to_int_from_field(value, "decimals")?;
        let reissuable = JsonDeserializer::safe_to_boolean_from_field(value, "reissuable")?;
        let quantity = JsonDeserializer::safe_to_int_from_field(value, "quantity")?;
        let scripted = JsonDeserializer::safe_to_boolean_from_field(value, "scripted")?;
        let min_sponsored_asset_fee =
            JsonDeserializer::safe_to_int_from_field(value, "minSponsoredAssetFee").unwrap_or(0);
        let origin_transaction_id =
            JsonDeserializer::safe_to_string_from_field(value, "originTransactionId")?;
        let script_details: ScriptDetails = value["scriptDetails"].borrow().try_into()?;

        Ok(AssetDetails {
            asset_id: AssetId::from_string(&asset_id)?,
            issue_height: issue_height as u32,
            issue_timestamp: issue_timestamp as u64,
            issuer: Address::from_string(&issuer)?,
            issuer_public_key: PublicKey::from_string(&issuer_public_key)?,
            name,
            description,
            decimals: decimals as u32,
            reissuable,
            quantity: quantity as u64,
            scripted,
            min_sponsored_asset_fee: min_sponsored_asset_fee as u64,
            origin_transaction_id: Id::from_string(&origin_transaction_id)?,
            script_details,
        })
    }
}

#[derive(Eq, PartialEq, Clone, Debug)]
pub struct ScriptDetails {
    script: Base64String,
    complexity: u32,
}

impl ScriptDetails {
    pub fn new(script: Base64String, complexity: u32) -> Self {
        Self { script, complexity }
    }

    pub fn script(&self) -> Base64String {
        self.script.clone()
    }

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

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

    fn try_from(value: &Value) -> Result<Self> {
        let script = match value["script"].as_str() {
            Some(script) => script,
            None => {
                return Ok(ScriptDetails {
                    script: Base64String::empty(),
                    complexity: 0,
                })
            }
        };
        let complexity = JsonDeserializer::safe_to_int_from_field(value, "scriptComplexity")?;
        Ok(ScriptDetails {
            script: Base64String::from_string(script)?,
            complexity: complexity as u32,
        })
    }
}

#[cfg(test)]
mod tests {
    use crate::error::Result;
    use crate::model::asset::asset_details::{AssetDetails, ScriptDetails};
    use crate::model::{Base64String, ByteString};
    use serde_json::{json, Value};
    use std::borrow::Borrow;
    use std::fs;

    #[test]
    fn test_json_to_asset_details() -> Result<()> {
        let data = fs::read_to_string("./tests/resources/assets/asset_details_rs.json")
            .expect("Unable to read file");
        let json: &Value = &serde_json::from_str(&data).expect("failed to convert");

        let asset_details: AssetDetails = json.try_into()?;

        assert_eq!(
            "CVwsbXjXmdYF2q4RCPuQKf7sLGpzhk7BNnYsxGZZJMym",
            asset_details.asset_id().encoded()
        );
        assert_eq!(2221593, asset_details.issue_height());
        assert_eq!(1662728397110, asset_details.issue_timestamp());
        assert_eq!(
            "3Ms6jp75u5qnfmAgWpxbt9xHv7znBp7RHnq",
            asset_details.issuer().encoded()
        );
        assert_eq!(
            "ASA4fMdz5FirDREfB34PPi67QxLHMt8tvzRQDT64juiM",
            asset_details.issuer_public_key().encoded()
        );
        assert_eq!("AssetWithScript", asset_details.name());
        assert_eq!("", asset_details.description());
        assert_eq!(0, asset_details.decimals());
        assert_eq!(true, asset_details.reissuable());
        assert_eq!(10000, asset_details.quantity());
        assert_eq!(true, asset_details.scripted());
        assert_eq!(0, asset_details.min_sponsored_asset_fee());
        assert_eq!(
            "CVwsbXjXmdYF2q4RCPuQKf7sLGpzhk7BNnYsxGZZJMym",
            asset_details.origin_transaction_id().encoded()
        );
        assert_eq!(127, asset_details.script_details().complexity());

        let expected_script = "AgQAAAAHbWFzdGVyMQkBAAAAEWFkZHJlc3NGcm9tU3RyaW5nAAAAAQIAAAAQMzMzbWFzdGVyQWRkcmVzcwQAAAAHJG1hdGNoMAUAAAACdHgDCQAAAQAAAAIFAAAAByRtYXRjaDACAAAAE1RyYW5zZmVyVHJhbnNhY3Rpb24EAAAAAXQFAAAAByRtYXRjaDADCQAAAAAAAAIIBQAAAAF0AAAABnNlbmRlcgUAAAAHbWFzdGVyMQYJAAAAAAAAAggFAAAAAXQAAAAJcmVjaXBpZW50BQAAAAdtYXN0ZXIxAwkAAAEAAAACBQAAAAckbWF0Y2gwAgAAABdNYXNzVHJhbnNmZXJUcmFuc2FjdGlvbgQAAAACbXQFAAAAByRtYXRjaDAJAAAAAAAAAggFAAAAAm10AAAABnNlbmRlcgUAAAAHbWFzdGVyMQMJAAABAAAAAgUAAAAHJG1hdGNoMAIAAAATRXhjaGFuZ2VUcmFuc2FjdGlvbgcGFLbwIw==";
        assert_eq!(
            expected_script,
            asset_details.script_details().script().encoded()
        );
        Ok(())
    }

    #[test]
    fn test_if_script_details_null() -> Result<()> {
        let json = json!({ "scriptDetails": null });
        let script_details: ScriptDetails = json["scriptDetails"].borrow().try_into()?;
        assert_eq!(script_details.script(), Base64String::empty());
        assert_eq!(script_details.complexity(), 0);
        Ok(())
    }
}