gpl_compression/instructions/
tree_config.rs

1use crate::state::TreeConfig;
2use anchor_lang::prelude::*;
3use spl_account_compression::program::SplAccountCompression;
4use spl_account_compression::Noop;
5
6//Initialize TreeConfig
7#[derive(Accounts)]
8#[instruction(max_depth: u32, max_buffer_size: u32)]
9pub struct InitializeTreeConfig<'info> {
10    #[account(
11        init,
12        seeds = [merkle_tree.to_account_info().key.as_ref()],
13        bump,
14        payer = authority,
15        space = TreeConfig::LEN
16    )]
17    pub tree_config: Account<'info, TreeConfig>,
18
19    #[account(zero)]
20    /// CHECK: This account must be all zeros
21    pub merkle_tree: UncheckedAccount<'info>,
22
23    #[account(mut)]
24    pub authority: Signer<'info>,
25
26    pub log_wrapper: Program<'info, Noop>,
27    pub compression_program: Program<'info, SplAccountCompression>,
28    pub system_program: Program<'info, System>,
29}
30
31// Handler for InitializeTreeConfig
32pub fn initialize_tree_handler(
33    ctx: Context<InitializeTreeConfig>,
34    max_depth: u32,
35    max_buffer_size: u32,
36) -> Result<()> {
37    let merkle_tree = ctx.accounts.merkle_tree.to_account_info();
38    let seeds = &[merkle_tree.key.as_ref(), &[ctx.bumps["tree_config"]]];
39    let tree_config = &mut ctx.accounts.tree_config;
40    tree_config.set_inner(TreeConfig {
41        authority: ctx.accounts.authority.key(),
42        merkle_tree: merkle_tree.key(),
43    });
44    let authority_pda_signer = &[&seeds[..]];
45    let cpi_ctx = CpiContext::new_with_signer(
46        ctx.accounts.compression_program.to_account_info(),
47        spl_account_compression::cpi::accounts::Initialize {
48            authority: ctx.accounts.authority.to_account_info(),
49            merkle_tree,
50            noop: ctx.accounts.log_wrapper.to_account_info(),
51        },
52        authority_pda_signer,
53    );
54    spl_account_compression::cpi::init_empty_merkle_tree(cpi_ctx, max_depth, max_buffer_size)
55}