waves_rust/model/transaction/
update_asset_info_transaction.rs1use crate::error::{Error, Result};
2use crate::model::{AssetId, ByteString};
3use crate::util::JsonDeserializer;
4use crate::waves_proto::UpdateAssetInfoTransactionData;
5use serde_json::{Map, Value};
6
7const TYPE: u8 = 17;
8
9#[derive(Clone, Eq, PartialEq, Debug)]
10pub struct UpdateAssetInfoTransactionInfo {
11 asset_id: AssetId,
12 name: String,
13 description: String,
14}
15
16impl UpdateAssetInfoTransactionInfo {
17 pub fn new(asset_id: AssetId, name: String, description: String) -> Self {
18 Self {
19 asset_id,
20 name,
21 description,
22 }
23 }
24
25 pub fn asset_id(&self) -> AssetId {
26 self.asset_id.clone()
27 }
28
29 pub fn description(&self) -> String {
30 self.description.clone()
31 }
32
33 pub fn name(&self) -> String {
34 self.name.clone()
35 }
36}
37
38impl TryFrom<&Value> for UpdateAssetInfoTransactionInfo {
39 type Error = Error;
40
41 fn try_from(value: &Value) -> Result<Self> {
42 let asset_id = JsonDeserializer::safe_to_string_from_field(value, "assetId")?;
43 let name = JsonDeserializer::safe_to_string_from_field(value, "name")?;
44 let description = JsonDeserializer::safe_to_string_from_field(value, "description")?;
45
46 Ok(UpdateAssetInfoTransactionInfo {
47 asset_id: AssetId::from_string(&asset_id)?,
48 name,
49 description,
50 })
51 }
52}
53
54#[derive(Clone, Eq, PartialEq, Debug)]
55pub struct UpdateAssetInfoTransaction {
56 asset_id: AssetId,
57 name: String,
58 description: String,
59}
60
61impl UpdateAssetInfoTransaction {
62 pub fn new(asset_id: AssetId, name: String, description: String) -> Self {
63 Self {
64 asset_id,
65 name,
66 description,
67 }
68 }
69
70 pub fn asset_id(&self) -> AssetId {
71 self.asset_id.clone()
72 }
73
74 pub fn description(&self) -> String {
75 self.description.clone()
76 }
77
78 pub fn name(&self) -> String {
79 self.name.clone()
80 }
81
82 pub fn tx_type() -> u8 {
83 TYPE
84 }
85}
86
87impl TryFrom<&Value> for UpdateAssetInfoTransaction {
88 type Error = Error;
89
90 fn try_from(value: &Value) -> Result<Self> {
91 let asset_id = JsonDeserializer::safe_to_string_from_field(value, "assetId")?;
92 let name = JsonDeserializer::safe_to_string_from_field(value, "name")?;
93 let description = JsonDeserializer::safe_to_string_from_field(value, "description")?;
94
95 Ok(UpdateAssetInfoTransaction {
96 asset_id: AssetId::from_string(&asset_id)?,
97 name,
98 description,
99 })
100 }
101}
102
103impl TryFrom<&UpdateAssetInfoTransaction> for Map<String, Value> {
104 type Error = Error;
105
106 fn try_from(value: &UpdateAssetInfoTransaction) -> Result<Self> {
107 let mut update_asset_info_tx_json = Map::new();
108 update_asset_info_tx_json.insert("assetId".to_owned(), value.asset_id().encoded().into());
109 update_asset_info_tx_json.insert("name".to_owned(), value.name().into());
110 update_asset_info_tx_json.insert("description".to_owned(), value.description().into());
111 Ok(update_asset_info_tx_json)
112 }
113}
114
115impl TryFrom<&UpdateAssetInfoTransaction> for UpdateAssetInfoTransactionData {
116 type Error = Error;
117
118 fn try_from(value: &UpdateAssetInfoTransaction) -> Result<Self> {
119 Ok(UpdateAssetInfoTransactionData {
120 asset_id: value.asset_id().bytes(),
121 name: value.name(),
122 description: value.description(),
123 })
124 }
125}
126
127#[cfg(test)]
128mod tests {
129 use crate::error::Result;
130 use crate::model::{
131 AssetId, ByteString, UpdateAssetInfoTransaction, UpdateAssetInfoTransactionInfo,
132 };
133 use crate::waves_proto::UpdateAssetInfoTransactionData;
134 use serde_json::{json, Map, Value};
135 use std::borrow::Borrow;
136 use std::fs;
137
138 #[test]
139 fn test_json_to_sponsor_fee_transaction() {
140 let data = fs::read_to_string("./tests/resources/update_asset_info_rs.json")
141 .expect("Unable to read file");
142 let json: Value = serde_json::from_str(&data).expect("failed to generate json from str");
143
144 let update_asset_info_from_json: UpdateAssetInfoTransactionInfo =
145 json.borrow().try_into().unwrap();
146
147 assert_eq!(
148 "7qhc24Cq53DiaHUzmcaYMUKq8kidaVW8ZAvKrTtADozG",
149 update_asset_info_from_json.asset_id().encoded()
150 );
151 assert_eq!("UpdatedAsset", update_asset_info_from_json.name());
152 assert_eq!(
153 "updated description",
154 update_asset_info_from_json.description()
155 );
156 }
157
158 #[test]
159 fn test_update_asset_to_proto() -> Result<()> {
160 let update_asset_tx = &UpdateAssetInfoTransaction::new(
161 AssetId::from_string("7qhc24Cq53DiaHUzmcaYMUKq8kidaVW8ZAvKrTtADozG")?,
162 "name".to_owned(),
163 "description".to_owned(),
164 );
165 let proto: UpdateAssetInfoTransactionData = update_asset_tx.try_into()?;
166
167 assert_eq!(proto.asset_id, update_asset_tx.asset_id().bytes());
168 assert_eq!(proto.name, update_asset_tx.name());
169 assert_eq!(proto.description, update_asset_tx.description());
170
171 Ok(())
172 }
173
174 #[test]
175 fn test_update_asset_to_json() -> Result<()> {
176 let update_asset_tx = &UpdateAssetInfoTransaction::new(
177 AssetId::from_string("7qhc24Cq53DiaHUzmcaYMUKq8kidaVW8ZAvKrTtADozG")?,
178 "name".to_owned(),
179 "description".to_owned(),
180 );
181 let map: Map<String, Value> = update_asset_tx.try_into()?;
182 let json: Value = map.into();
183 let expected_json = json!({
184 "assetId": "7qhc24Cq53DiaHUzmcaYMUKq8kidaVW8ZAvKrTtADozG",
185 "name": "name",
186 "description": "description",
187 });
188 assert_eq!(expected_json, json);
189 Ok(())
190 }
191}