|
| 1 | +use { |
| 2 | + super::validate_owner, |
| 3 | + crate::processor::{check_account_owner, unpack_amount}, |
| 4 | + pinocchio::{ |
| 5 | + account_info::AccountInfo, hint::likely, program_error::ProgramError, ProgramResult, |
| 6 | + }, |
| 7 | + pinocchio_token_interface::{ |
| 8 | + error::TokenError, |
| 9 | + state::{account::Account, load_mut}, |
| 10 | + }, |
| 11 | +}; |
| 12 | + |
| 13 | +#[allow(clippy::arithmetic_side_effects)] |
| 14 | +pub fn process_unwrap_lamports(accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResult { |
| 15 | + // instruction data: expected u8 (1) + optional u64 (8) |
| 16 | + let [has_amount, maybe_amount @ ..] = instruction_data else { |
| 17 | + return Err(TokenError::InvalidInstruction.into()); |
| 18 | + }; |
| 19 | + |
| 20 | + let maybe_amount = if likely(*has_amount == 0) { |
| 21 | + None |
| 22 | + } else if *has_amount == 1 { |
| 23 | + Some(unpack_amount(maybe_amount)?) |
| 24 | + } else { |
| 25 | + return Err(TokenError::InvalidInstruction.into()); |
| 26 | + }; |
| 27 | + |
| 28 | + let [source_account_info, destination_account_info, authority_info, remaining @ ..] = accounts |
| 29 | + else { |
| 30 | + return Err(ProgramError::NotEnoughAccountKeys); |
| 31 | + }; |
| 32 | + |
| 33 | + // SAFETY: single immutable borrow to `source_account_info` account data |
| 34 | + let source_account = |
| 35 | + unsafe { load_mut::<Account>(source_account_info.borrow_mut_data_unchecked())? }; |
| 36 | + |
| 37 | + if !source_account.is_native() { |
| 38 | + return Err(TokenError::NonNativeNotSupported.into()); |
| 39 | + } |
| 40 | + |
| 41 | + // SAFETY: `authority_info` is not currently borrowed; in the case |
| 42 | + // `authority_info` is the same as `source_account_info`, then it cannot be |
| 43 | + // a multisig. |
| 44 | + unsafe { validate_owner(&source_account.owner, authority_info, remaining)? }; |
| 45 | + |
| 46 | + // If we have an amount, we need to validate whether there are enough lamports |
| 47 | + // to unwrap or not; otherwise we just use the full amount. |
| 48 | + let (amount, remaining_amount) = if let Some(amount) = maybe_amount { |
| 49 | + ( |
| 50 | + amount, |
| 51 | + source_account |
| 52 | + .amount() |
| 53 | + .checked_sub(amount) |
| 54 | + .ok_or(TokenError::InsufficientFunds)?, |
| 55 | + ) |
| 56 | + } else { |
| 57 | + (source_account.amount(), 0) |
| 58 | + }; |
| 59 | + |
| 60 | + // Comparing whether the AccountInfo's "point" to the same account or |
| 61 | + // not - this is a faster comparison since it just checks the internal |
| 62 | + // raw pointer. |
| 63 | + let self_transfer = source_account_info == destination_account_info; |
| 64 | + |
| 65 | + if self_transfer || amount == 0 { |
| 66 | + // Validates the token account owner since we are not writing |
| 67 | + // to the account. |
| 68 | + check_account_owner(source_account_info) |
| 69 | + } else { |
| 70 | + source_account.set_amount(remaining_amount); |
| 71 | + |
| 72 | + // SAFETY: single mutable borrow to `source_account_info` lamports. |
| 73 | + let source_lamports = unsafe { source_account_info.borrow_mut_lamports_unchecked() }; |
| 74 | + // Note: The amount of a source token account is already validated and the |
| 75 | + // `lamports` on the account is always greater than `amount`. |
| 76 | + *source_lamports -= amount; |
| 77 | + |
| 78 | + // SAFETY: single mutable borrow to `destination_account_info` lamports; the |
| 79 | + // account is already validated to be different from `source_account_info`. |
| 80 | + let destination_lamports = |
| 81 | + unsafe { destination_account_info.borrow_mut_lamports_unchecked() }; |
| 82 | + // Note: The total lamports supply is bound to `u64::MAX`. |
| 83 | + *destination_lamports += amount; |
| 84 | + |
| 85 | + Ok(()) |
| 86 | + } |
| 87 | +} |
0 commit comments