ephemeral_rollups_sdk/
ephem.rs

1use solana_program::account_info::AccountInfo;
2use solana_program::entrypoint::ProgramResult;
3use solana_program::instruction::{AccountMeta, Instruction};
4use solana_program::program::invoke;
5
6/// CPI to trigger a commit for one or more accounts in the ER
7#[inline(always)]
8pub fn commit_accounts<'a, 'info>(
9    payer: &'a AccountInfo<'info>,
10    account_infos: Vec<&'a AccountInfo<'info>>,
11    magic_context: &'a AccountInfo<'info>,
12    magic_program: &'a AccountInfo<'info>,
13) -> ProgramResult {
14    let ix = create_schedule_commit_ix(payer, &account_infos, magic_context, magic_program, false);
15    let mut all_accounts = vec![payer.clone(), magic_context.clone()];
16    all_accounts.extend(account_infos.into_iter().cloned());
17    invoke(&ix, &all_accounts)
18}
19
20/// CPI to trigger a commit and undelegate one or more accounts in the ER
21#[inline(always)]
22pub fn commit_and_undelegate_accounts<'a, 'info>(
23    payer: &'a AccountInfo<'info>,
24    account_infos: Vec<&'a AccountInfo<'info>>,
25    magic_context: &'a AccountInfo<'info>,
26    magic_program: &'a AccountInfo<'info>,
27) -> ProgramResult {
28    let ix = create_schedule_commit_ix(payer, &account_infos, magic_context, magic_program, true);
29    let mut all_accounts = vec![payer.clone(), magic_context.clone()];
30    all_accounts.extend(account_infos.into_iter().cloned());
31    invoke(&ix, &all_accounts)
32}
33
34pub fn create_schedule_commit_ix<'a, 'info>(
35    payer: &'a AccountInfo<'info>,
36    account_infos: &[&'a AccountInfo<'info>],
37    magic_context: &'a AccountInfo<'info>,
38    magic_program: &'a AccountInfo<'info>,
39    allow_undelegation: bool,
40) -> Instruction {
41    let instruction_data = if allow_undelegation {
42        vec![2, 0, 0, 0]
43    } else {
44        vec![1, 0, 0, 0]
45    };
46    let mut account_metas = vec![
47        AccountMeta {
48            pubkey: *payer.key,
49            is_signer: true,
50            is_writable: true,
51        },
52        AccountMeta {
53            pubkey: *magic_context.key,
54            is_signer: false,
55            is_writable: true,
56        },
57    ];
58    account_metas.extend(account_infos.iter().map(|x| AccountMeta {
59        pubkey: *x.key,
60        is_signer: x.is_signer,
61        is_writable: x.is_writable,
62    }));
63    Instruction::new_with_bytes(*magic_program.key, &instruction_data, account_metas)
64}