cronos_network/instructions/
snapshot_start.rs

1use {
2    crate::state::*,
3    anchor_lang::prelude::*,
4    cronos_scheduler::{responses::ExecResponse, state::Manager},
5    std::mem::size_of,
6};
7
8#[derive(Accounts)]
9pub struct SnapshotStart<'info> {
10    #[account(seeds = [SEED_AUTHORITY], bump, has_one = manager)]
11    pub authority: Box<Account<'info, Authority>>,
12
13    #[account(seeds = [SEED_CONFIG], bump)]
14    pub config: Box<Account<'info, Config>>,
15
16    #[account(signer, constraint = manager.authority == authority.key())]
17    pub manager: Box<Account<'info, Manager>>,
18
19    #[account(mut)]
20    pub payer: Signer<'info>,
21
22    #[account(mut, seeds = [SEED_REGISTRY], bump)]
23    pub registry: Account<'info, Registry>,
24
25    #[account(
26        init,
27        seeds = [
28            SEED_SNAPSHOT,
29            registry.snapshot_count.to_be_bytes().as_ref()
30        ],
31        bump,
32        space = 8 + size_of::<Snapshot>(),
33        payer = payer
34    )]
35    pub snapshot: Account<'info, Snapshot>,
36
37    #[account()]
38    pub system_program: Program<'info, System>,
39}
40
41pub fn handler(ctx: Context<SnapshotStart>) -> Result<ExecResponse> {
42    // Get accounts
43    let registry = &mut ctx.accounts.registry;
44    let snapshot = &mut ctx.accounts.snapshot;
45
46    // Start a new snapshot
47    registry.new_snapshot(snapshot)?;
48
49    // Use dynamic accounts to run the next invocation with the new current snapshot
50    let snapshot_pubkey = snapshot.key();
51    let next_snapshot_pubkey = Snapshot::pda(snapshot.id.checked_add(1).unwrap()).0;
52    Ok(ExecResponse {
53        dynamic_accounts: Some(
54            ctx.accounts
55                .to_account_metas(None)
56                .iter()
57                .map(|acc| match acc.pubkey {
58                    _ if acc.pubkey == snapshot_pubkey => next_snapshot_pubkey,
59                    _ => acc.pubkey,
60                })
61                .collect(),
62        ),
63    })
64}