use borsh::BorshDeserialize;
use mpl_bubblegum::types::{BubblegumEventType, LeafSchema};
use mpl_bubblegum::LeafSchemaEvent;
pub const SPL_NOOP_PROGRAM_ID: &str = "noopb9bkMVfRPU8AsbpTUg8AQkHtKwMYZiFUjNRtMmV";
pub const MPL_NOOP_PROGRAM_ID: &str = "mnoopTCrg4p8ry25e4bcWA9XZjbNjMTfgYVGGEdRsf3";
#[must_use]
pub fn is_noop_program(program_id: &str) -> bool {
program_id == SPL_NOOP_PROGRAM_ID || program_id == MPL_NOOP_PROGRAM_ID
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LeafSchemaEventDecoded {
pub schema: DecodedLeafSchema,
pub leaf_hash: [u8; 32],
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DecodedLeafSchema {
V1 {
id: [u8; 32],
owner: [u8; 32],
delegate: [u8; 32],
nonce: u64,
data_hash: [u8; 32],
creator_hash: [u8; 32],
},
V2 {
id: [u8; 32],
owner: [u8; 32],
delegate: [u8; 32],
nonce: u64,
data_hash: [u8; 32],
creator_hash: [u8; 32],
},
}
#[must_use]
pub fn decode_leaf_schema_event(data: &[u8]) -> Option<LeafSchemaEventDecoded> {
if data.is_empty() || data[0] != BubblegumEventType::LeafSchemaEvent as u8 {
return None;
}
let event = LeafSchemaEvent::try_from_slice(data).ok()?;
let schema = match event.schema {
LeafSchema::V1 {
id,
owner,
delegate,
nonce,
data_hash,
creator_hash,
} => DecodedLeafSchema::V1 {
id: id.to_bytes(),
owner: owner.to_bytes(),
delegate: delegate.to_bytes(),
nonce,
data_hash,
creator_hash,
},
LeafSchema::V2 {
id,
owner,
delegate,
nonce,
data_hash,
creator_hash,
..
} => DecodedLeafSchema::V2 {
id: id.to_bytes(),
owner: owner.to_bytes(),
delegate: delegate.to_bytes(),
nonce,
data_hash,
creator_hash,
},
};
Some(LeafSchemaEventDecoded {
schema,
leaf_hash: event.leaf_hash,
})
}
impl LeafSchemaEventDecoded {
#[must_use]
pub fn as_override(&self) -> crate::cnft::types::NoopOverride {
let (id, owner, delegate, nonce, data_hash, creator_hash) = match &self.schema {
DecodedLeafSchema::V1 {
id,
owner,
delegate,
nonce,
data_hash,
creator_hash,
}
| DecodedLeafSchema::V2 {
id,
owner,
delegate,
nonce,
data_hash,
creator_hash,
} => (*id, *owner, *delegate, *nonce, *data_hash, *creator_hash),
};
crate::cnft::types::NoopOverride {
leaf_index: nonce,
nonce,
id,
owner,
delegate,
data_hash,
creator_hash,
leaf_hash: self.leaf_hash,
}
}
#[must_use]
pub fn is_v2(&self) -> bool {
matches!(&self.schema, DecodedLeafSchema::V2 { .. })
}
}