use anchor_lang::prelude::*;
use light_batched_merkle_tree::merkle_tree::{
BatchedMerkleTreeAccount, InstructionDataBatchAppendInputs,
};
use crate::{
emit_indexer_event,
utils::check_signer_is_registered_or_authority::{
check_signer_is_registered_or_authority, GroupAccounts,
},
RegisteredProgram,
};
#[derive(Accounts)]
pub struct BatchAppend<'info> {
pub authority: Signer<'info>,
pub registered_program_pda: Option<Account<'info, RegisteredProgram>>,
pub log_wrapper: UncheckedAccount<'info>,
#[account(mut)]
pub merkle_tree: AccountInfo<'info>,
#[account(mut)]
pub output_queue: AccountInfo<'info>,
}
impl<'info> GroupAccounts<'info> for BatchAppend<'info> {
fn get_authority(&self) -> &Signer<'info> {
&self.authority
}
fn get_registered_program_pda(&self) -> &Option<Account<'info, RegisteredProgram>> {
&self.registered_program_pda
}
}
pub fn process_batch_append_leaves<'a, 'b, 'c: 'info, 'info>(
ctx: &'a Context<'a, 'b, 'c, 'info, BatchAppend<'info>>,
instruction_data: InstructionDataBatchAppendInputs,
) -> Result<()> {
let merkle_tree =
&mut BatchedMerkleTreeAccount::state_from_account_info(&ctx.accounts.merkle_tree)
.map_err(ProgramError::from)?;
check_signer_is_registered_or_authority::<BatchAppend, BatchedMerkleTreeAccount>(
ctx,
merkle_tree,
)?;
let event = merkle_tree
.update_tree_from_output_queue_account_info(&ctx.accounts.output_queue, instruction_data)
.map_err(ProgramError::from)?;
emit_indexer_event(event.try_to_vec()?, &ctx.accounts.log_wrapper)
}