waves_rust/model/asset/
asset_details.rs

1use crate::error::{Error, Result};
2use crate::model::{Address, AssetId, Base64String, Id, PublicKey};
3use crate::util::JsonDeserializer;
4use serde_json::Value;
5use std::borrow::Borrow;
6
7#[derive(Eq, PartialEq, Clone, Debug)]
8pub struct AssetDetails {
9    asset_id: AssetId,
10    issue_height: u32,
11    issue_timestamp: u64,
12    issuer: Address,
13    issuer_public_key: PublicKey,
14    name: String,
15    description: String,
16    decimals: u32,
17    reissuable: bool,
18    quantity: u64,
19    scripted: bool,
20    min_sponsored_asset_fee: u64,
21    origin_transaction_id: Id,
22    script_details: ScriptDetails,
23}
24
25#[allow(clippy::too_many_arguments)]
26impl AssetDetails {
27    pub fn new(
28        asset_id: AssetId,
29        issue_height: u32,
30        issue_timestamp: u64,
31        issuer: Address,
32        issuer_public_key: PublicKey,
33        name: String,
34        description: String,
35        decimals: u32,
36        reissuable: bool,
37        quantity: u64,
38        scripted: bool,
39        min_sponsored_asset_fee: u64,
40        origin_transaction_id: Id,
41        script_details: ScriptDetails,
42    ) -> Self {
43        Self {
44            asset_id,
45            issue_height,
46            issue_timestamp,
47            issuer,
48            issuer_public_key,
49            name,
50            description,
51            decimals,
52            reissuable,
53            quantity,
54            scripted,
55            min_sponsored_asset_fee,
56            origin_transaction_id,
57            script_details,
58        }
59    }
60
61    pub fn asset_id(&self) -> AssetId {
62        self.asset_id.clone()
63    }
64
65    pub fn issue_height(&self) -> u32 {
66        self.issue_height
67    }
68
69    pub fn issue_timestamp(&self) -> u64 {
70        self.issue_timestamp
71    }
72
73    pub fn issuer(&self) -> Address {
74        self.issuer.clone()
75    }
76
77    pub fn issuer_public_key(&self) -> PublicKey {
78        self.issuer_public_key.clone()
79    }
80
81    pub fn name(&self) -> String {
82        self.name.clone()
83    }
84
85    pub fn description(&self) -> String {
86        self.description.clone()
87    }
88
89    pub fn decimals(&self) -> u32 {
90        self.decimals
91    }
92
93    pub fn reissuable(&self) -> bool {
94        self.reissuable
95    }
96
97    pub fn quantity(&self) -> u64 {
98        self.quantity
99    }
100
101    pub fn scripted(&self) -> bool {
102        self.scripted
103    }
104
105    pub fn min_sponsored_asset_fee(&self) -> u64 {
106        self.min_sponsored_asset_fee
107    }
108
109    pub fn origin_transaction_id(&self) -> Id {
110        self.origin_transaction_id.clone()
111    }
112
113    pub fn script_details(&self) -> ScriptDetails {
114        self.script_details.clone()
115    }
116}
117
118impl TryFrom<&Value> for AssetDetails {
119    type Error = Error;
120
121    fn try_from(value: &Value) -> Result<Self> {
122        let asset_id = JsonDeserializer::safe_to_string_from_field(value, "assetId")?;
123        let issue_height = JsonDeserializer::safe_to_int_from_field(value, "issueHeight")?;
124        let issue_timestamp = JsonDeserializer::safe_to_int_from_field(value, "issueTimestamp")?;
125        let issuer = JsonDeserializer::safe_to_string_from_field(value, "issuer")?;
126        let issuer_public_key =
127            JsonDeserializer::safe_to_string_from_field(value, "issuerPublicKey")?;
128        let name = JsonDeserializer::safe_to_string_from_field(value, "name")?;
129        let description = JsonDeserializer::safe_to_string_from_field(value, "description")?;
130        let decimals = JsonDeserializer::safe_to_int_from_field(value, "decimals")?;
131        let reissuable = JsonDeserializer::safe_to_boolean_from_field(value, "reissuable")?;
132        let quantity = JsonDeserializer::safe_to_int_from_field(value, "quantity")?;
133        let scripted = JsonDeserializer::safe_to_boolean_from_field(value, "scripted")?;
134        let min_sponsored_asset_fee =
135            JsonDeserializer::safe_to_int_from_field(value, "minSponsoredAssetFee").unwrap_or(0);
136        let origin_transaction_id =
137            JsonDeserializer::safe_to_string_from_field(value, "originTransactionId")?;
138        let script_details: ScriptDetails = value["scriptDetails"].borrow().try_into()?;
139
140        Ok(AssetDetails {
141            asset_id: AssetId::from_string(&asset_id)?,
142            issue_height: issue_height as u32,
143            issue_timestamp: issue_timestamp as u64,
144            issuer: Address::from_string(&issuer)?,
145            issuer_public_key: PublicKey::from_string(&issuer_public_key)?,
146            name,
147            description,
148            decimals: decimals as u32,
149            reissuable,
150            quantity: quantity as u64,
151            scripted,
152            min_sponsored_asset_fee: min_sponsored_asset_fee as u64,
153            origin_transaction_id: Id::from_string(&origin_transaction_id)?,
154            script_details,
155        })
156    }
157}
158
159#[derive(Eq, PartialEq, Clone, Debug)]
160pub struct ScriptDetails {
161    script: Base64String,
162    complexity: u32,
163}
164
165impl ScriptDetails {
166    pub fn new(script: Base64String, complexity: u32) -> Self {
167        Self { script, complexity }
168    }
169
170    pub fn script(&self) -> Base64String {
171        self.script.clone()
172    }
173
174    pub fn complexity(&self) -> u32 {
175        self.complexity
176    }
177}
178
179impl TryFrom<&Value> for ScriptDetails {
180    type Error = Error;
181
182    fn try_from(value: &Value) -> Result<Self> {
183        let script = match value["script"].as_str() {
184            Some(script) => script,
185            None => {
186                return Ok(ScriptDetails {
187                    script: Base64String::empty(),
188                    complexity: 0,
189                })
190            }
191        };
192        let complexity = JsonDeserializer::safe_to_int_from_field(value, "scriptComplexity")?;
193        Ok(ScriptDetails {
194            script: Base64String::from_string(script)?,
195            complexity: complexity as u32,
196        })
197    }
198}
199
200#[cfg(test)]
201mod tests {
202    use crate::error::Result;
203    use crate::model::asset::asset_details::{AssetDetails, ScriptDetails};
204    use crate::model::{Base64String, ByteString};
205    use serde_json::{json, Value};
206    use std::borrow::Borrow;
207    use std::fs;
208
209    #[test]
210    fn test_json_to_asset_details() -> Result<()> {
211        let data = fs::read_to_string("./tests/resources/assets/asset_details_rs.json")
212            .expect("Unable to read file");
213        let json: &Value = &serde_json::from_str(&data).expect("failed to convert");
214
215        let asset_details: AssetDetails = json.try_into()?;
216
217        assert_eq!(
218            "CVwsbXjXmdYF2q4RCPuQKf7sLGpzhk7BNnYsxGZZJMym",
219            asset_details.asset_id().encoded()
220        );
221        assert_eq!(2221593, asset_details.issue_height());
222        assert_eq!(1662728397110, asset_details.issue_timestamp());
223        assert_eq!(
224            "3Ms6jp75u5qnfmAgWpxbt9xHv7znBp7RHnq",
225            asset_details.issuer().encoded()
226        );
227        assert_eq!(
228            "ASA4fMdz5FirDREfB34PPi67QxLHMt8tvzRQDT64juiM",
229            asset_details.issuer_public_key().encoded()
230        );
231        assert_eq!("AssetWithScript", asset_details.name());
232        assert_eq!("", asset_details.description());
233        assert_eq!(0, asset_details.decimals());
234        assert_eq!(true, asset_details.reissuable());
235        assert_eq!(10000, asset_details.quantity());
236        assert_eq!(true, asset_details.scripted());
237        assert_eq!(0, asset_details.min_sponsored_asset_fee());
238        assert_eq!(
239            "CVwsbXjXmdYF2q4RCPuQKf7sLGpzhk7BNnYsxGZZJMym",
240            asset_details.origin_transaction_id().encoded()
241        );
242        assert_eq!(127, asset_details.script_details().complexity());
243
244        let expected_script = "AgQAAAAHbWFzdGVyMQkBAAAAEWFkZHJlc3NGcm9tU3RyaW5nAAAAAQIAAAAQMzMzbWFzdGVyQWRkcmVzcwQAAAAHJG1hdGNoMAUAAAACdHgDCQAAAQAAAAIFAAAAByRtYXRjaDACAAAAE1RyYW5zZmVyVHJhbnNhY3Rpb24EAAAAAXQFAAAAByRtYXRjaDADCQAAAAAAAAIIBQAAAAF0AAAABnNlbmRlcgUAAAAHbWFzdGVyMQYJAAAAAAAAAggFAAAAAXQAAAAJcmVjaXBpZW50BQAAAAdtYXN0ZXIxAwkAAAEAAAACBQAAAAckbWF0Y2gwAgAAABdNYXNzVHJhbnNmZXJUcmFuc2FjdGlvbgQAAAACbXQFAAAAByRtYXRjaDAJAAAAAAAAAggFAAAAAm10AAAABnNlbmRlcgUAAAAHbWFzdGVyMQMJAAABAAAAAgUAAAAHJG1hdGNoMAIAAAATRXhjaGFuZ2VUcmFuc2FjdGlvbgcGFLbwIw==";
245        assert_eq!(
246            expected_script,
247            asset_details.script_details().script().encoded()
248        );
249        Ok(())
250    }
251
252    #[test]
253    fn test_if_script_details_null() -> Result<()> {
254        let json = json!({ "scriptDetails": null });
255        let script_details: ScriptDetails = json["scriptDetails"].borrow().try_into()?;
256        assert_eq!(script_details.script(), Base64String::empty());
257        assert_eq!(script_details.complexity(), 0);
258        Ok(())
259    }
260}