Skip to main content

sui_types/
id.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use 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/// Rust version of the Move sui::object::Info type
25#[derive(Debug, Serialize, Deserialize, JsonSchema, Clone, Eq, PartialEq)]
26pub struct UID {
27    pub id: ID,
28}
29
30/// Rust version of the Move sui::object::ID type
31#[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 is_uid(s: &StructTag) -> bool {
54        s.address == SUI_FRAMEWORK_ADDRESS
55            && s.module.as_ident_str() == OBJECT_MODULE_NAME
56            && s.name.as_ident_str() == UID_STRUCT_NAME
57            && s.type_params.is_empty()
58    }
59
60    pub fn object_id(&self) -> &ObjectID {
61        &self.id.bytes
62    }
63
64    pub fn to_bcs_bytes(&self) -> Vec<u8> {
65        bcs::to_bytes(&self).unwrap()
66    }
67
68    pub fn layout() -> MoveStructLayout {
69        MoveStructLayout {
70            type_: Self::type_(),
71            fields: vec![MoveFieldLayout::new(
72                ident_str!("id").to_owned(),
73                MoveTypeLayout::Struct(Box::new(ID::layout())),
74            )],
75        }
76    }
77}
78
79impl ID {
80    pub fn new(object_id: ObjectID) -> Self {
81        Self { bytes: object_id }
82    }
83
84    pub fn type_() -> StructTag {
85        StructTag {
86            address: SUI_FRAMEWORK_ADDRESS,
87            module: OBJECT_MODULE_NAME.to_owned(),
88            name: ID_STRUCT_NAME.to_owned(),
89            type_params: Vec::new(),
90        }
91    }
92
93    pub fn layout() -> MoveStructLayout {
94        MoveStructLayout {
95            type_: Self::type_(),
96            fields: vec![MoveFieldLayout::new(
97                ident_str!("bytes").to_owned(),
98                MoveTypeLayout::Address,
99            )],
100        }
101    }
102}
103
104impl MoveTypeTagTrait for ID {
105    fn get_type_tag() -> TypeTag {
106        TypeTag::Struct(Box::new(Self::type_()))
107    }
108}