iota_types/
display.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use move_core_types::{ident_str, identifier::IdentStr, language_storage::StructTag};
6use serde::Deserialize;
7
8use crate::{
9    IOTA_FRAMEWORK_ADDRESS,
10    collection_types::VecMap,
11    event::Event,
12    id::{ID, UID},
13};
14
15pub const DISPLAY_MODULE_NAME: &IdentStr = ident_str!("display");
16pub const DISPLAY_CREATED_EVENT_NAME: &IdentStr = ident_str!("DisplayCreated");
17pub const DISPLAY_VERSION_UPDATED_EVENT_NAME: &IdentStr = ident_str!("VersionUpdated");
18
19// TODO: add tests to keep in sync
20/// Rust version of the Move iota::display::Display type
21#[derive(Debug, Deserialize, Clone, Eq, PartialEq)]
22pub struct DisplayObject {
23    pub id: UID,
24    pub fields: VecMap<String, String>,
25    pub version: u16,
26}
27
28#[derive(Deserialize, Debug)]
29/// The event that is emitted when a `Display` version is "released".
30/// Serves for Display versioning.
31pub struct DisplayVersionUpdatedEvent {
32    pub id: ID,
33    pub version: u16,
34    pub fields: VecMap<String, String>,
35}
36
37impl DisplayVersionUpdatedEvent {
38    pub fn type_(inner: &StructTag) -> StructTag {
39        StructTag {
40            address: IOTA_FRAMEWORK_ADDRESS,
41            name: DISPLAY_VERSION_UPDATED_EVENT_NAME.to_owned(),
42            module: DISPLAY_MODULE_NAME.to_owned(),
43            type_params: vec![inner.clone().into()],
44        }
45    }
46
47    // Checks if the provided `StructTag` is a DisplayVersionUpdatedEvent<T>
48    pub fn is_display_updated_event(inner: &StructTag) -> bool {
49        inner.address == IOTA_FRAMEWORK_ADDRESS
50            && inner.module.as_ident_str() == DISPLAY_MODULE_NAME
51            && inner.name.as_ident_str() == DISPLAY_VERSION_UPDATED_EVENT_NAME
52    }
53
54    // Checks if the provided `StructTag` is a DisplayVersionUpdatedEvent<T> and
55    // returns a reference to the inner type T if so.
56    pub fn inner_type(inner: &StructTag) -> Option<&StructTag> {
57        use move_core_types::language_storage::TypeTag;
58
59        if !Self::is_display_updated_event(inner) {
60            return None;
61        }
62
63        match &inner.type_params[..] {
64            [TypeTag::Struct(struct_type)] => Some(struct_type),
65            _ => None,
66        }
67    }
68
69    pub fn try_from_event(event: &Event) -> Option<(&StructTag, Self)> {
70        let inner_type = Self::inner_type(&event.type_)?;
71
72        bcs::from_bytes(&event.contents)
73            .ok()
74            .map(|event| (inner_type, event))
75    }
76}
77
78#[derive(Deserialize, Debug)]
79pub struct DisplayCreatedEvent {
80    // The Object ID of Display Object
81    pub id: ID,
82}
83
84impl DisplayCreatedEvent {
85    pub fn type_(inner: &StructTag) -> StructTag {
86        StructTag {
87            address: IOTA_FRAMEWORK_ADDRESS,
88            name: DISPLAY_CREATED_EVENT_NAME.to_owned(),
89            module: DISPLAY_MODULE_NAME.to_owned(),
90            type_params: vec![inner.clone().into()],
91        }
92    }
93}