sol_cerberus/instructions/
initialize_app.rs1use crate::utils::utc_now;
2use crate::{state::app::*, utils::validate_string_len};
3use anchor_lang::prelude::*;
4
5#[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}