|
| 1 | +use anchor_lang::{prelude::*, system_program, Discriminator}; |
| 2 | + |
| 3 | +use crate::state::{Launch, OldLaunch}; |
| 4 | +use crate::ID; |
| 5 | + |
| 6 | +#[derive(Accounts)] |
| 7 | +pub struct ResizeLaunch<'info> { |
| 8 | + /// CHECK: we check the discriminator, owner, and size |
| 9 | + #[account(mut)] |
| 10 | + pub launch: UncheckedAccount<'info>, |
| 11 | + #[account(mut)] |
| 12 | + pub payer: Signer<'info>, |
| 13 | + pub system_program: Program<'info, System>, |
| 14 | +} |
| 15 | + |
| 16 | +impl ResizeLaunch<'_> { |
| 17 | + pub fn handle(ctx: Context<Self>) -> Result<()> { |
| 18 | + let launch = &mut ctx.accounts.launch; |
| 19 | + |
| 20 | + // Owner check |
| 21 | + require_eq!(launch.owner, &ID); |
| 22 | + |
| 23 | + // Discriminator check |
| 24 | + let is_discriminator_correct = |
| 25 | + launch.data.try_borrow_mut().unwrap()[..8] == Launch::discriminator(); |
| 26 | + require_eq!(is_discriminator_correct, true); |
| 27 | + |
| 28 | + const AFTER_REALLOC_SIZE: usize = 8 + Launch::INIT_SPACE; |
| 29 | + const BEFORE_REALLOC_SIZE: usize = 8 + OldLaunch::INIT_SPACE; |
| 30 | + |
| 31 | + // Size check |
| 32 | + if launch.data_len() != BEFORE_REALLOC_SIZE { |
| 33 | + require_eq!(launch.data_len(), AFTER_REALLOC_SIZE); |
| 34 | + return Ok(()); |
| 35 | + } |
| 36 | + |
| 37 | + let old_data = OldLaunch::deserialize(&mut &launch.try_borrow_data().unwrap()[8..])?; |
| 38 | + |
| 39 | + let new_data = Launch { |
| 40 | + pda_bump: old_data.pda_bump, |
| 41 | + minimum_raise_amount: old_data.minimum_raise_amount, |
| 42 | + monthly_spending_limit_amount: old_data.monthly_spending_limit_amount, |
| 43 | + monthly_spending_limit_members: old_data.monthly_spending_limit_members, |
| 44 | + launch_authority: old_data.launch_authority, |
| 45 | + launch_signer: old_data.launch_signer, |
| 46 | + launch_signer_pda_bump: old_data.launch_signer_pda_bump, |
| 47 | + launch_quote_vault: old_data.launch_quote_vault, |
| 48 | + launch_base_vault: old_data.launch_base_vault, |
| 49 | + base_mint: old_data.base_mint, |
| 50 | + quote_mint: old_data.quote_mint, |
| 51 | + unix_timestamp_started: old_data.unix_timestamp_started, |
| 52 | + unix_timestamp_closed: old_data.unix_timestamp_closed, |
| 53 | + total_committed_amount: old_data.total_committed_amount, |
| 54 | + state: old_data.state, |
| 55 | + seq_num: old_data.seq_num, |
| 56 | + seconds_for_launch: old_data.seconds_for_launch, |
| 57 | + dao: old_data.dao, |
| 58 | + dao_vault: old_data.dao_vault, |
| 59 | + performance_package_grantee: old_data.performance_package_grantee, |
| 60 | + performance_package_token_amount: old_data.performance_package_token_amount, |
| 61 | + months_until_insiders_can_unlock: old_data.months_until_insiders_can_unlock, |
| 62 | + team_address: old_data.team_address, |
| 63 | + total_approved_amount: old_data.total_approved_amount, |
| 64 | + additional_tokens_amount: old_data.additional_tokens_amount, |
| 65 | + additional_tokens_recipient: old_data.additional_tokens_recipient, |
| 66 | + additional_tokens_claimed: old_data.additional_tokens_claimed, |
| 67 | + unix_timestamp_completed: old_data.unix_timestamp_completed, |
| 68 | + is_performance_package_initialized: old_data.is_performance_package_initialized, |
| 69 | + accumulator_activation_delay_seconds: 0, |
| 70 | + }; |
| 71 | + |
| 72 | + launch.realloc(AFTER_REALLOC_SIZE, true)?; |
| 73 | + |
| 74 | + let lamports_needed = Rent::get()?.minimum_balance(AFTER_REALLOC_SIZE); |
| 75 | + |
| 76 | + if lamports_needed > launch.lamports() { |
| 77 | + system_program::transfer( |
| 78 | + CpiContext::new( |
| 79 | + ctx.accounts.system_program.to_account_info(), |
| 80 | + system_program::Transfer { |
| 81 | + from: ctx.accounts.payer.to_account_info(), |
| 82 | + to: launch.to_account_info(), |
| 83 | + }, |
| 84 | + ), |
| 85 | + lamports_needed - launch.lamports(), |
| 86 | + )?; |
| 87 | + } |
| 88 | + |
| 89 | + new_data.serialize(&mut &mut launch.try_borrow_mut_data().unwrap()[8..])?; |
| 90 | + |
| 91 | + Ok(()) |
| 92 | + } |
| 93 | +} |
0 commit comments