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");
#[derive(Debug, Deserialize, Clone, Eq, PartialEq)]
pub struct DisplayObject {
pub id: UID,
pub fields: VecMap<String, String>,
pub version: u16,
}
#[derive(Deserialize, Debug)]
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()],
}
}
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
}
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 {
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()],
}
}
}