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
use crate::error::{Error, Result};
use crate::model::{AssetId, ByteString};
use crate::util::JsonDeserializer;
use crate::waves_proto::UpdateAssetInfoTransactionData;
use serde_json::{Map, Value};

const TYPE: u8 = 17;

#[derive(Clone, Eq, PartialEq, Debug)]
pub struct UpdateAssetInfoTransactionInfo {
    asset_id: AssetId,
    name: String,
    description: String,
}

impl UpdateAssetInfoTransactionInfo {
    pub fn new(asset_id: AssetId, name: String, description: String) -> Self {
        Self {
            asset_id,
            name,
            description,
        }
    }

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

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

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

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

    fn try_from(value: &Value) -> Result<Self> {
        let asset_id = JsonDeserializer::safe_to_string_from_field(value, "assetId")?;
        let name = JsonDeserializer::safe_to_string_from_field(value, "name")?;
        let description = JsonDeserializer::safe_to_string_from_field(value, "description")?;

        Ok(UpdateAssetInfoTransactionInfo {
            asset_id: AssetId::from_string(&asset_id)?,
            name,
            description,
        })
    }
}

#[derive(Clone, Eq, PartialEq, Debug)]
pub struct UpdateAssetInfoTransaction {
    asset_id: AssetId,
    name: String,
    description: String,
}

impl UpdateAssetInfoTransaction {
    pub fn new(asset_id: AssetId, name: String, description: String) -> Self {
        Self {
            asset_id,
            name,
            description,
        }
    }

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

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

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

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

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

    fn try_from(value: &Value) -> Result<Self> {
        let asset_id = JsonDeserializer::safe_to_string_from_field(value, "assetId")?;
        let name = JsonDeserializer::safe_to_string_from_field(value, "name")?;
        let description = JsonDeserializer::safe_to_string_from_field(value, "description")?;

        Ok(UpdateAssetInfoTransaction {
            asset_id: AssetId::from_string(&asset_id)?,
            name,
            description,
        })
    }
}

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

    fn try_from(value: &UpdateAssetInfoTransaction) -> Result<Self> {
        let mut update_asset_info_tx_json = Map::new();
        update_asset_info_tx_json.insert("assetId".to_owned(), value.asset_id().encoded().into());
        update_asset_info_tx_json.insert("name".to_owned(), value.name().into());
        update_asset_info_tx_json.insert("description".to_owned(), value.description().into());
        Ok(update_asset_info_tx_json)
    }
}

impl TryFrom<&UpdateAssetInfoTransaction> for UpdateAssetInfoTransactionData {
    type Error = Error;

    fn try_from(value: &UpdateAssetInfoTransaction) -> Result<Self> {
        Ok(UpdateAssetInfoTransactionData {
            asset_id: value.asset_id().bytes(),
            name: value.name(),
            description: value.description(),
        })
    }
}

#[cfg(test)]
mod tests {
    use crate::error::Result;
    use crate::model::{
        AssetId, ByteString, UpdateAssetInfoTransaction, UpdateAssetInfoTransactionInfo,
    };
    use crate::waves_proto::UpdateAssetInfoTransactionData;
    use serde_json::{json, Map, Value};
    use std::borrow::Borrow;
    use std::fs;

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

        let update_asset_info_from_json: UpdateAssetInfoTransactionInfo =
            json.borrow().try_into().unwrap();

        assert_eq!(
            "7qhc24Cq53DiaHUzmcaYMUKq8kidaVW8ZAvKrTtADozG",
            update_asset_info_from_json.asset_id().encoded()
        );
        assert_eq!("UpdatedAsset", update_asset_info_from_json.name());
        assert_eq!(
            "updated description",
            update_asset_info_from_json.description()
        );
    }

    #[test]
    fn test_update_asset_to_proto() -> Result<()> {
        let update_asset_tx = &UpdateAssetInfoTransaction::new(
            AssetId::from_string("7qhc24Cq53DiaHUzmcaYMUKq8kidaVW8ZAvKrTtADozG")?,
            "name".to_owned(),
            "description".to_owned(),
        );
        let proto: UpdateAssetInfoTransactionData = update_asset_tx.try_into()?;

        assert_eq!(proto.asset_id, update_asset_tx.asset_id().bytes());
        assert_eq!(proto.name, update_asset_tx.name());
        assert_eq!(proto.description, update_asset_tx.description());

        Ok(())
    }

    #[test]
    fn test_update_asset_to_json() -> Result<()> {
        let update_asset_tx = &UpdateAssetInfoTransaction::new(
            AssetId::from_string("7qhc24Cq53DiaHUzmcaYMUKq8kidaVW8ZAvKrTtADozG")?,
            "name".to_owned(),
            "description".to_owned(),
        );
        let map: Map<String, Value> = update_asset_tx.try_into()?;
        let json: Value = map.into();
        let expected_json = json!({
             "assetId": "7qhc24Cq53DiaHUzmcaYMUKq8kidaVW8ZAvKrTtADozG",
             "name": "name",
             "description": "description",
        });
        assert_eq!(expected_json, json);
        Ok(())
    }
}