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
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,
        }
    }
}