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