get_account_data_size.rs 867 B

1234567891011121314151617181920212223242526272829
  1. use pinocchio::{
  2. account_info::AccountInfo, program::set_return_data, program_error::ProgramError, ProgramResult,
  3. };
  4. use token_interface::{
  5. error::TokenError,
  6. state::{account::Account, load, mint::Mint, RawType},
  7. };
  8. use super::check_account_owner;
  9. #[inline(always)]
  10. pub fn process_get_account_data_size(accounts: &[AccountInfo]) -> ProgramResult {
  11. let [mint_info, _remaining @ ..] = accounts else {
  12. return Err(ProgramError::NotEnoughAccountKeys);
  13. };
  14. // Make sure the mint is valid.
  15. check_account_owner(mint_info)?;
  16. // SAFETY: single immutable borrow to `mint_info` account data and
  17. // `load` validates that the mint is initialized.
  18. let _ = unsafe {
  19. load::<Mint>(mint_info.borrow_data_unchecked()).map_err(|_| TokenError::InvalidMint)?
  20. };
  21. set_return_data(&Account::LEN.to_le_bytes());
  22. Ok(())
  23. }