use crate::{Schema, SchemaValue};
use anchor_lang::{prelude::*, solana_program::keccak};
use spl_account_compression::{wrap_application_data_v1, Noop};
#[derive(AnchorDeserialize, AnchorSerialize)]
pub enum CompressedDataEventStream {
Full { data: SchemaValue },
PatchChunk { key: String, data: SchemaValue },
Empty,
}
#[derive(AnchorDeserialize, AnchorSerialize)]
pub enum CompressedDataEvent {
Leaf {
slot: u64,
tree_id: [u8; 32],
leaf_idx: u32,
seq: u64,
stream_type: CompressedDataEventStream,
},
TreeSchemaValue {
discriminator: [u8; 32],
tree_id: [u8; 32],
schema: Schema,
},
}
impl CompressedDataEvent {
pub fn wrap<'info>(&self, noop: &Program<'info, Noop>) -> Result<()> {
wrap_application_data_v1(self.try_to_vec().unwrap(), noop)
}
pub fn tree(tree_id: Pubkey, schema: Schema, program_id: Pubkey, account_name: String) -> Self {
Self::TreeSchemaValue {
discriminator: keccak::hashv(&[program_id.as_ref(), account_name.as_bytes()][..])
.to_bytes(),
tree_id: tree_id.to_bytes(),
schema,
}
}
}