1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) Mysten Labs, Inc.
// Modifications Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use move_core_types::{ident_str, identifier::IdentStr, language_storage::StructTag};
use serde::Deserialize;

use crate::{
    IOTA_FRAMEWORK_ADDRESS,
    collection_types::VecMap,
    event::Event,
    id::{ID, UID},
};

pub const DISPLAY_MODULE_NAME: &IdentStr = ident_str!("display");
pub const DISPLAY_CREATED_EVENT_NAME: &IdentStr = ident_str!("DisplayCreated");
pub const DISPLAY_VERSION_UPDATED_EVENT_NAME: &IdentStr = ident_str!("VersionUpdated");

// TODO: add tests to keep in sync
/// Rust version of the Move iota::display::Display type
#[derive(Debug, Deserialize, Clone, Eq, PartialEq)]
pub struct DisplayObject {
    pub id: UID,
    pub fields: VecMap<String, String>,
    pub version: u16,
}

#[derive(Deserialize, Debug)]
/// The event that is emitted when a `Display` version is "released".
/// Serves for Display versioning.
pub struct DisplayVersionUpdatedEvent {
    pub id: ID,
    pub version: u16,
    pub fields: VecMap<String, String>,
}

impl DisplayVersionUpdatedEvent {
    pub fn type_(inner: &StructTag) -> StructTag {
        StructTag {
            address: IOTA_FRAMEWORK_ADDRESS,
            name: DISPLAY_VERSION_UPDATED_EVENT_NAME.to_owned(),
            module: DISPLAY_MODULE_NAME.to_owned(),
            type_params: vec![inner.clone().into()],
        }
    }

    // Checks if the provided `StructTag` is a DisplayVersionUpdatedEvent<T>
    pub fn is_display_updated_event(inner: &StructTag) -> bool {
        inner.address == IOTA_FRAMEWORK_ADDRESS
            && inner.module.as_ident_str() == DISPLAY_MODULE_NAME
            && inner.name.as_ident_str() == DISPLAY_VERSION_UPDATED_EVENT_NAME
    }

    // Checks if the provided `StructTag` is a DisplayVersionUpdatedEvent<T> and
    // returns a reference to the inner type T if so.
    pub fn inner_type(inner: &StructTag) -> Option<&StructTag> {
        use move_core_types::language_storage::TypeTag;

        if !Self::is_display_updated_event(inner) {
            return None;
        }

        match &inner.type_params[..] {
            [TypeTag::Struct(struct_type)] => Some(struct_type),
            _ => None,
        }
    }

    pub fn try_from_event(event: &Event) -> Option<(&StructTag, Self)> {
        let inner_type = Self::inner_type(&event.type_)?;

        bcs::from_bytes(&event.contents)
            .ok()
            .map(|event| (inner_type, event))
    }
}

#[derive(Deserialize, Debug)]
pub struct DisplayCreatedEvent {
    // The Object ID of Display Object
    pub id: ID,
}

impl DisplayCreatedEvent {
    pub fn type_(inner: &StructTag) -> StructTag {
        StructTag {
            address: IOTA_FRAMEWORK_ADDRESS,
            name: DISPLAY_CREATED_EVENT_NAME.to_owned(),
            module: DISPLAY_MODULE_NAME.to_owned(),
            type_params: vec![inner.clone().into()],
        }
    }
}