use anchor_lang::prelude::*;
use anchor_lang::InstructionData;
use ephemeral_rollups_sdk::ephem::{MagicInstructionBuilder, MagicAction, CallHandler, CommitType};
use ephemeral_rollups_sdk::{ActionArgs, ShortAccountMeta};
#[commit]
pub fn commit_with_action(ctx: Context<CommitWithAction>) -> Result<()> {
let instruction_data = anchor_lang::InstructionData::data(&crate::instruction::YourHandler {});
let call_handler = CallHandler {
args: ActionArgs { escrow_index: 1, data: instruction_data },
compute_units: 200_000,
escrow_authority: ctx.accounts.payer.to_account_info(),
destination_program: crate::ID,
accounts: vec![
ShortAccountMeta { pubkey: ctx.accounts.target_account.key(), is_writable: true },
ShortAccountMeta { pubkey: ctx.accounts.committed_account.key(), is_writable: false },
],
};
MagicInstructionBuilder {
payer: ctx.accounts.payer.to_account_info(),
magic_context: ctx.accounts.magic_context.to_account_info(),
magic_program: ctx.accounts.magic_program.to_account_info(),
magic_action: MagicAction::Commit(CommitType::WithHandler {
commited_accounts: vec![ctx.accounts.committed_account.to_account_info()],
call_handlers: vec![call_handler],
}),
}.build_and_invoke()?;
Ok(())
}
#[derive(Accounts)]
pub struct CommitWithAction<'info> {
#[account(mut)]
pub payer: Signer<'info>,
#[account(mut)]
pub committed_account: Account<'info, YourAccountType>,
/// CHECK: handler target
pub target_account: UncheckedAccount<'info>,
}