sol_cerberus/instructions/
update_app.rs

1use crate::state::app::*;
2use crate::utils::{program_authority_field, utc_now, validate_string_len};
3use crate::Errors;
4use anchor_lang::prelude::*;
5
6#[derive(Accounts)]
7pub struct UpdateApp<'info> {
8    pub signer: Signer<'info>, // Only current Authority or Recovery key can update the Authority
9    #[account(
10        mut,
11        seeds = [b"app".as_ref(), app.id.key().as_ref()], 
12        bump = app.bump,
13        constraint = app.authority == signer.key() || (app.recovery.is_some() && app.recovery.unwrap() == signer.key())   @ Errors::UnauthorizedAuthorityUpdate,
14    )]
15    pub app: Account<'info, App>,
16    pub system_program: Program<'info, System>,
17}
18
19pub fn update_app(ctx: Context<UpdateApp>, app_data: UpdateAppData) -> Result<()> {
20    let app = &mut ctx.accounts.app;
21    app.authority = app_data.authority;
22    app.recovery = app_data.recovery;
23    app.name = validate_string_len(&app_data.name, 0, 16)?;
24    app.account_type =
25        program_authority_field(&app_data.authority, app.account_type, app_data.account_type)?;
26    app.fee = program_authority_field(&app_data.authority, app.fee, app_data.fee)?;
27    app.cached = app_data.cached;
28    app.expires_at =
29        program_authority_field(&app_data.authority, app.expires_at, app_data.expires_at)?;
30    emit!(AppChanged {
31        time: utc_now(),
32        app_id: ctx.accounts.app.id,
33        authority: ctx.accounts.app.authority,
34    });
35    Ok(())
36}