sol_cerberus/instructions/
initialize_app.rs

1use crate::utils::utc_now;
2use crate::{state::app::*, utils::validate_string_len};
3use anchor_lang::prelude::*;
4
5// SPACE SIZE:
6// + 8 discriminator
7// + 32 id (Pubkey)
8// + 32 authority (Pubkey)
9// + 1 + 32 Option<backup> (Pubkey)
10// + 4 + 16 name (string)
11// + 8 roles_updated_at
12// + 8 rules_updated_at
13// + 1 cached
14// + 1 + 8 Option<u64> fee
15// + 1 account_type
16// + 1 + 8 Option<i64> expires_at
17// + 1 bump
18// total = 8 + 32  + 32 + 1 + 32 + 4 + 16 + 8 + 8 + 1 + 1 + 8 + 1 + 1 + 8 + 1 = 162
19#[derive(Accounts)]
20#[instruction(app_data: AppData)]
21pub struct InitializeApp<'info> {
22    #[account(mut)]
23    pub authority: Signer<'info>,
24    #[account(
25        init,
26        payer = authority,
27        space = 162,
28        seeds = [b"app".as_ref(), app_data.id.key().as_ref()], 
29        bump
30    )]
31    pub app: Box<Account<'info, App>>,
32    pub system_program: Program<'info, System>,
33}
34
35pub fn initialize_app(ctx: Context<InitializeApp>, app_data: AppData) -> Result<()> {
36    let app = &mut ctx.accounts.app;
37    app.id = app_data.id;
38    app.account_type = AccountTypes::Basic as u8;
39    app.authority = ctx.accounts.authority.key();
40    app.recovery = app_data.recovery;
41    app.name = validate_string_len(&app_data.name, 0, 16)?;
42    app.fee = None;
43    app.cached = app_data.cached;
44    app.rules_updated_at = utc_now();
45    app.roles_updated_at = app.rules_updated_at;
46    app.expires_at = None;
47    app.bump = ctx.bumps.app;
48    emit!(AppChanged {
49        time: app.rules_updated_at,
50        app_id: ctx.accounts.app.id,
51        authority: ctx.accounts.app.authority,
52    });
53    Ok(())
54}