1use std::fmt;
6
7use move_core_types::{
8 account_address::AccountAddress,
9 annotated_value::{MoveFieldLayout, MoveStructLayout, MoveTypeLayout},
10 ident_str,
11 identifier::IdentStr,
12 language_storage::{StructTag, TypeTag},
13};
14use schemars::JsonSchema;
15use serde::{Deserialize, Serialize};
16
17use crate::{IOTA_FRAMEWORK_ADDRESS, MoveTypeTagTrait, base_types::ObjectID};
18
19pub const OBJECT_MODULE_NAME_STR: &str = "object";
20pub const OBJECT_MODULE_NAME: &IdentStr = ident_str!(OBJECT_MODULE_NAME_STR);
21pub const UID_STRUCT_NAME: &IdentStr = ident_str!("UID");
22pub const ID_STRUCT_NAME: &IdentStr = ident_str!("ID");
23pub const RESOLVED_IOTA_ID: (&AccountAddress, &IdentStr, &IdentStr) =
24 (&IOTA_FRAMEWORK_ADDRESS, OBJECT_MODULE_NAME, ID_STRUCT_NAME);
25
26#[derive(Debug, Serialize, Deserialize, JsonSchema, Clone, Eq, PartialEq)]
28pub struct UID {
29 pub id: ID,
30}
31
32#[derive(Debug, Serialize, Deserialize, JsonSchema, Clone, Eq, PartialEq)]
34#[serde(transparent)]
35pub struct ID {
36 pub bytes: ObjectID,
37}
38
39impl UID {
40 pub fn new(bytes: ObjectID) -> Self {
41 Self {
42 id: { ID::new(bytes) },
43 }
44 }
45
46 pub fn type_() -> StructTag {
47 StructTag {
48 address: IOTA_FRAMEWORK_ADDRESS,
49 module: OBJECT_MODULE_NAME.to_owned(),
50 name: UID_STRUCT_NAME.to_owned(),
51 type_params: Vec::new(),
52 }
53 }
54
55 pub fn object_id(&self) -> &ObjectID {
56 &self.id.bytes
57 }
58
59 pub fn to_bcs_bytes(&self) -> Vec<u8> {
60 bcs::to_bytes(&self).unwrap()
61 }
62
63 pub fn layout() -> MoveStructLayout {
64 MoveStructLayout {
65 type_: Self::type_(),
66 fields: vec![MoveFieldLayout::new(
67 ident_str!("id").to_owned(),
68 MoveTypeLayout::Struct(Box::new(ID::layout())),
69 )],
70 }
71 }
72}
73
74impl fmt::Display for UID {
75 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
76 self.id.fmt(f)
77 }
78}
79
80impl ID {
81 pub fn new(object_id: ObjectID) -> Self {
82 Self { bytes: object_id }
83 }
84
85 pub fn type_() -> StructTag {
86 StructTag {
87 address: IOTA_FRAMEWORK_ADDRESS,
88 module: OBJECT_MODULE_NAME.to_owned(),
89 name: ID_STRUCT_NAME.to_owned(),
90 type_params: Vec::new(),
91 }
92 }
93
94 pub fn layout() -> MoveStructLayout {
95 MoveStructLayout {
96 type_: Self::type_(),
97 fields: vec![MoveFieldLayout::new(
98 ident_str!("bytes").to_owned(),
99 MoveTypeLayout::Address,
100 )],
101 }
102 }
103}
104
105impl fmt::Display for ID {
106 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
107 self.bytes.fmt(f)
108 }
109}
110
111impl MoveTypeTagTrait for ID {
112 fn get_type_tag() -> TypeTag {
113 TypeTag::Struct(Box::new(Self::type_()))
114 }
115}