|
| 1 | +//! Initialize instruction for creating a new spending config |
| 2 | +
|
| 3 | +use anchor_lang::prelude::*; |
| 4 | + |
| 5 | +use crate::error::SpendingLimitError; |
| 6 | +use crate::state::*; |
| 7 | + |
| 8 | +/// Accounts for the initialize instruction |
| 9 | +#[derive(Accounts)] |
| 10 | +pub struct Initialize<'info> { |
| 11 | + /// The authority that will control this spending config |
| 12 | + #[account(mut)] |
| 13 | + pub authority: Signer<'info>, |
| 14 | + |
| 15 | + /// The spending config PDA to initialize |
| 16 | + #[account( |
| 17 | + init, |
| 18 | + payer = authority, |
| 19 | + space = SpendingConfig::SIZE, |
| 20 | + seeds = [SPENDING_CONFIG_SEED, authority.key().as_ref()], |
| 21 | + bump |
| 22 | + )] |
| 23 | + pub spending_config: Account<'info, SpendingConfig>, |
| 24 | + |
| 25 | + /// System program for account creation |
| 26 | + pub system_program: Program<'info, System>, |
| 27 | +} |
| 28 | + |
| 29 | +/// Initialize a new spending configuration |
| 30 | +/// |
| 31 | +/// Creates a PDA that stores spending limits for an MPC wallet. |
| 32 | +/// The authority (usually the MPC wallet address) controls all config updates. |
| 33 | +/// |
| 34 | +/// # Arguments |
| 35 | +/// * `ctx` - The instruction context |
| 36 | +/// * `config_input` - Initial configuration parameters |
| 37 | +/// |
| 38 | +/// # Errors |
| 39 | +/// * `InvalidLimitConfig` - If the limit configuration is invalid |
| 40 | +pub fn handler(ctx: Context<Initialize>, config_input: SpendingConfigInput) -> Result<()> { |
| 41 | + // Validate input |
| 42 | + require!(config_input.validate(), SpendingLimitError::InvalidLimitConfig); |
| 43 | + |
| 44 | + let config_key = ctx.accounts.spending_config.key(); |
| 45 | + let spending_config = &mut ctx.accounts.spending_config; |
| 46 | + let clock = Clock::get()?; |
| 47 | + let current_slot = clock.slot; |
| 48 | + |
| 49 | + // Initialize the spending config |
| 50 | + spending_config.authority = ctx.accounts.authority.key(); |
| 51 | + spending_config.guardian = None; |
| 52 | + spending_config.per_tx_limit = config_input.per_tx_limit; |
| 53 | + spending_config.daily_limit = config_input.daily_limit; |
| 54 | + spending_config.weekly_limit = config_input.weekly_limit; |
| 55 | + spending_config.monthly_limit = config_input.monthly_limit; |
| 56 | + spending_config.daily_spent = 0; |
| 57 | + spending_config.weekly_spent = 0; |
| 58 | + spending_config.monthly_spent = 0; |
| 59 | + spending_config.daily_reset_slot = current_slot + SLOTS_PER_DAY; |
| 60 | + spending_config.weekly_reset_slot = current_slot + SLOTS_PER_WEEK; |
| 61 | + spending_config.monthly_reset_slot = current_slot + SLOTS_PER_MONTH; |
| 62 | + spending_config.whitelist_only = config_input.whitelist_only; |
| 63 | + spending_config.is_paused = false; |
| 64 | + spending_config.whitelist_count = 0; |
| 65 | + spending_config.update_cooldown_slots = config_input.update_cooldown_slots; |
| 66 | + spending_config.pending_update_slot = 0; |
| 67 | + spending_config.bump = ctx.bumps.spending_config; |
| 68 | + spending_config._reserved = [0u8; 64]; |
| 69 | + |
| 70 | + // Emit event |
| 71 | + emit!(ConfigInitialized { |
| 72 | + config: config_key, |
| 73 | + authority: spending_config.authority, |
| 74 | + per_tx_limit: spending_config.per_tx_limit, |
| 75 | + daily_limit: spending_config.daily_limit, |
| 76 | + weekly_limit: spending_config.weekly_limit, |
| 77 | + monthly_limit: spending_config.monthly_limit, |
| 78 | + }); |
| 79 | + |
| 80 | + msg!( |
| 81 | + "Initialized spending config for authority {} with limits: per_tx={}, daily={}, weekly={}, monthly={}", |
| 82 | + spending_config.authority, |
| 83 | + spending_config.per_tx_limit, |
| 84 | + spending_config.daily_limit, |
| 85 | + spending_config.weekly_limit, |
| 86 | + spending_config.monthly_limit |
| 87 | + ); |
| 88 | + |
| 89 | + Ok(()) |
| 90 | +} |
| 91 | + |
| 92 | +/// Accounts for initializing with a custom authority |
| 93 | +#[derive(Accounts)] |
| 94 | +#[instruction(authority: Pubkey)] |
| 95 | +pub struct InitializeFor<'info> { |
| 96 | + /// The payer for account creation |
| 97 | + #[account(mut)] |
| 98 | + pub payer: Signer<'info>, |
| 99 | + |
| 100 | + /// The spending config PDA to initialize |
| 101 | + #[account( |
| 102 | + init, |
| 103 | + payer = payer, |
| 104 | + space = SpendingConfig::SIZE, |
| 105 | + seeds = [SPENDING_CONFIG_SEED, authority.as_ref()], |
| 106 | + bump |
| 107 | + )] |
| 108 | + pub spending_config: Account<'info, SpendingConfig>, |
| 109 | + |
| 110 | + /// System program for account creation |
| 111 | + pub system_program: Program<'info, System>, |
| 112 | +} |
| 113 | + |
| 114 | +/// Initialize a spending config for a specific authority |
| 115 | +/// |
| 116 | +/// Allows creating a config for an authority that may not be present as a signer. |
| 117 | +/// This is useful when the MPC wallet address is known but not yet operational. |
| 118 | +/// |
| 119 | +/// # Arguments |
| 120 | +/// * `ctx` - The instruction context |
| 121 | +/// * `authority` - The authority pubkey to set |
| 122 | +/// * `config_input` - Initial configuration parameters |
| 123 | +pub fn handler_for( |
| 124 | + ctx: Context<InitializeFor>, |
| 125 | + authority: Pubkey, |
| 126 | + config_input: SpendingConfigInput, |
| 127 | +) -> Result<()> { |
| 128 | + require!(config_input.validate(), SpendingLimitError::InvalidLimitConfig); |
| 129 | + |
| 130 | + let config_key = ctx.accounts.spending_config.key(); |
| 131 | + let spending_config = &mut ctx.accounts.spending_config; |
| 132 | + let clock = Clock::get()?; |
| 133 | + let current_slot = clock.slot; |
| 134 | + |
| 135 | + spending_config.authority = authority; |
| 136 | + spending_config.guardian = None; |
| 137 | + spending_config.per_tx_limit = config_input.per_tx_limit; |
| 138 | + spending_config.daily_limit = config_input.daily_limit; |
| 139 | + spending_config.weekly_limit = config_input.weekly_limit; |
| 140 | + spending_config.monthly_limit = config_input.monthly_limit; |
| 141 | + spending_config.daily_spent = 0; |
| 142 | + spending_config.weekly_spent = 0; |
| 143 | + spending_config.monthly_spent = 0; |
| 144 | + spending_config.daily_reset_slot = current_slot + SLOTS_PER_DAY; |
| 145 | + spending_config.weekly_reset_slot = current_slot + SLOTS_PER_WEEK; |
| 146 | + spending_config.monthly_reset_slot = current_slot + SLOTS_PER_MONTH; |
| 147 | + spending_config.whitelist_only = config_input.whitelist_only; |
| 148 | + spending_config.is_paused = false; |
| 149 | + spending_config.whitelist_count = 0; |
| 150 | + spending_config.update_cooldown_slots = config_input.update_cooldown_slots; |
| 151 | + spending_config.pending_update_slot = 0; |
| 152 | + spending_config.bump = ctx.bumps.spending_config; |
| 153 | + spending_config._reserved = [0u8; 64]; |
| 154 | + |
| 155 | + emit!(ConfigInitialized { |
| 156 | + config: config_key, |
| 157 | + authority: spending_config.authority, |
| 158 | + per_tx_limit: spending_config.per_tx_limit, |
| 159 | + daily_limit: spending_config.daily_limit, |
| 160 | + weekly_limit: spending_config.weekly_limit, |
| 161 | + monthly_limit: spending_config.monthly_limit, |
| 162 | + }); |
| 163 | + |
| 164 | + msg!( |
| 165 | + "Initialized spending config for authority {} (paid by {})", |
| 166 | + authority, |
| 167 | + ctx.accounts.payer.key() |
| 168 | + ); |
| 169 | + |
| 170 | + Ok(()) |
| 171 | +} |
0 commit comments