|
| 1 | +use solana_program::{ |
| 2 | + account_info::{next_account_info, AccountInfo}, |
| 3 | + entrypoint, |
| 4 | + entrypoint::ProgramResult, |
| 5 | + incinerator, msg, |
| 6 | + program_error::ProgramError, |
| 7 | + pubkey::Pubkey, |
| 8 | +}; |
| 9 | + |
| 10 | +entrypoint!(process_instruction); |
| 11 | + |
| 12 | +fn process_instruction( |
| 13 | + _program_id: &Pubkey, |
| 14 | + accounts: &[AccountInfo], |
| 15 | + data: &[u8], |
| 16 | +) -> ProgramResult { |
| 17 | + let accounts_iter = &mut accounts.iter(); |
| 18 | + let target_account_info = next_account_info(accounts_iter)?; |
| 19 | + match data[0] { |
| 20 | + // print account size |
| 21 | + 0 => { |
| 22 | + msg!( |
| 23 | + "account size {}", |
| 24 | + target_account_info.try_borrow_data()?.len() |
| 25 | + ); |
| 26 | + } |
| 27 | + // set account data |
| 28 | + 1 => { |
| 29 | + let mut account_data = target_account_info.try_borrow_mut_data()?; |
| 30 | + account_data[0] = 100; |
| 31 | + } |
| 32 | + // deallocate account |
| 33 | + 2 => { |
| 34 | + let incinerator_info = next_account_info(accounts_iter)?; |
| 35 | + if !incinerator::check_id(incinerator_info.key) { |
| 36 | + return Err(ProgramError::InvalidAccountData); |
| 37 | + } |
| 38 | + |
| 39 | + let mut target_lamports = target_account_info.try_borrow_mut_lamports()?; |
| 40 | + let mut incinerator_lamports = incinerator_info.try_borrow_mut_lamports()?; |
| 41 | + |
| 42 | + **incinerator_lamports = incinerator_lamports |
| 43 | + .checked_add(**target_lamports) |
| 44 | + .ok_or(ProgramError::ArithmeticOverflow)?; |
| 45 | + |
| 46 | + **target_lamports = target_lamports |
| 47 | + .checked_sub(**target_lamports) |
| 48 | + .ok_or(ProgramError::InsufficientFunds)?; |
| 49 | + } |
| 50 | + // reallocate account |
| 51 | + 3 => { |
| 52 | + let new_size = usize::from_le_bytes(data[1..9].try_into().unwrap()); |
| 53 | + target_account_info.realloc(new_size, false)?; |
| 54 | + } |
| 55 | + // bad ixn |
| 56 | + _ => { |
| 57 | + return Err(ProgramError::InvalidArgument); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + Ok(()) |
| 62 | +} |
0 commit comments