iota_types/stardust/output/
alias.rs1use iota_sdk_types::{Identifier, StructTag, TypeTag};
5use serde::{Deserialize, Serialize};
6use serde_with::serde_as;
7
8use crate::{
9 balance::Balance,
10 base_types::IotaAddress,
11 collection_types::Bag,
12 error::IotaError,
13 id::UID,
14 object::{Data, Object},
15};
16
17pub const ALIAS_MODULE_NAME: Identifier = Identifier::from_static("alias");
18pub const ALIAS_OUTPUT_MODULE_NAME: Identifier = Identifier::from_static("alias_output");
19pub const ALIAS_OUTPUT_STRUCT_NAME: Identifier = Identifier::from_static("AliasOutput");
20pub const ALIAS_STRUCT_NAME: Identifier = Identifier::from_static("Alias");
21pub const ALIAS_DYNAMIC_OBJECT_FIELD_KEY: &[u8] = b"alias";
22pub const ALIAS_DYNAMIC_OBJECT_FIELD_KEY_TYPE: &str = "vector<u8>";
23
24#[serde_as]
25#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
26pub struct Alias {
27 pub id: UID,
30
31 pub legacy_state_controller: IotaAddress,
33 pub state_index: u32,
35 pub state_metadata: Option<Vec<u8>>,
37
38 pub sender: Option<IotaAddress>,
40 pub metadata: Option<Vec<u8>>,
42
43 pub immutable_issuer: Option<IotaAddress>,
45 pub immutable_metadata: Option<Vec<u8>>,
47}
48
49impl Alias {
50 pub fn tag() -> StructTag {
53 StructTag::new(
54 IotaAddress::STARDUST,
55 ALIAS_MODULE_NAME,
56 ALIAS_STRUCT_NAME,
57 Vec::new(),
58 )
59 }
60}
61
62#[serde_as]
63#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
64pub struct AliasOutput {
65 pub id: UID,
67
68 pub balance: Balance,
70 pub native_tokens: Bag,
74}
75
76impl AliasOutput {
77 pub fn tag(type_param: TypeTag) -> StructTag {
80 StructTag::new(
81 IotaAddress::STARDUST,
82 ALIAS_OUTPUT_MODULE_NAME,
83 ALIAS_OUTPUT_STRUCT_NAME,
84 vec![type_param],
85 )
86 }
87
88 pub fn from_bcs_bytes(content: &[u8]) -> Result<Self, IotaError> {
90 bcs::from_bytes(content).map_err(|err| IotaError::ObjectDeserialization {
91 error: format!("Unable to deserialize AliasOutput object: {err:?}"),
92 })
93 }
94
95 pub fn is_alias_output(s: &StructTag) -> bool {
96 s.address() == IotaAddress::STARDUST
97 && s.module() == &ALIAS_OUTPUT_MODULE_NAME
98 && s.name() == &ALIAS_OUTPUT_STRUCT_NAME
99 }
100}
101
102impl TryFrom<&Object> for AliasOutput {
103 type Error = IotaError;
104 fn try_from(object: &Object) -> Result<Self, Self::Error> {
105 match &object.data {
106 Data::Struct(o) => {
107 if AliasOutput::is_alias_output(o.struct_tag()) {
108 return AliasOutput::from_bcs_bytes(o.contents());
109 }
110 }
111 Data::Package(_) => {}
112 }
113
114 Err(IotaError::Type {
115 error: format!("Object type is not an AliasOutput: {object:?}"),
116 })
117 }
118}