Skip to main content

shadow_drive_user_staking/instructions/
update_config.rs

1use crate::constants::admin1;
2use crate::errors::ErrorCodes;
3use crate::instructions::initialize_config::StorageConfig;
4use anchor_lang::prelude::*;
5
6/// This is the function that handles the `update_config` ix
7pub fn handler(
8    ctx: Context<UpdateConfig>,
9    new_storage_cost: Option<u64>,
10    new_storage_available: Option<u128>,
11    new_admin_2: Option<Pubkey>,
12    new_max_acct_size: Option<u64>,
13    new_min_acct_size: Option<u64>,
14    new_shades_per_gb_epoch: Option<u64>,
15) -> Result<()> {
16    msg!("Updating StorageConfig");
17    {
18        let storage_config = &mut ctx.accounts.storage_config;
19
20        // Update storage cost
21        if let Some(storage_cost) = new_storage_cost {
22            storage_config.shades_per_gib = storage_cost;
23        }
24        if let Some(shades_per_gb_epoch) = new_shades_per_gb_epoch {
25            storage_config.shades_per_gib_per_epoch = shades_per_gb_epoch;
26        }
27
28        // Update storage available
29        if let Some(storage_available) = new_storage_available {
30            storage_config.storage_available = storage_available;
31        }
32
33        // Update admins. admin_1 is a program constant.
34        if let Some(admin_2) = new_admin_2 {
35            require!(
36                ctx.accounts.admin.key() == admin1::ID,
37                ErrorCodes::OnlyAdmin1CanChangeAdmins
38            );
39            storage_config.admin_2 = admin_2;
40        }
41
42        // Update account size limits
43        if let Some(max_account_size) = new_max_acct_size {
44            storage_config.max_account_size = max_account_size;
45        }
46        if let Some(min_account_size) = new_min_acct_size {
47            storage_config.min_account_size = min_account_size;
48        }
49    }
50
51    Ok(())
52}
53
54#[derive(Accounts)]
55/// This `UpdateConfig` context is used to update the account which stores Shadow Drive
56/// configuration data including storage costs, admin pubkeys.
57pub struct UpdateConfig<'info> {
58    /// This account is a PDA that holds storage config parameters and admin pubkeys.
59    #[account(
60        mut,
61        seeds = [
62            "storage-config".as_bytes()
63        ],
64        bump
65    )]
66    pub storage_config: Box<Account<'info, StorageConfig>>,
67
68    /// This account is the SPL's staking policy admin.
69    /// Must be either freeze or mint authority
70    #[account(mut, constraint=is_admin(&admin, &storage_config))]
71    pub admin: Signer<'info>,
72}
73
74pub fn is_admin<'info>(
75    admin: &Signer<'info>,
76    storage_config: &Account<'info, StorageConfig>,
77) -> bool {
78    admin.key() == admin1::ID || admin.key() == storage_config.admin_2
79}