1

I am trying to obtain the state of a token account(spl_token::state::Account::AccountState), How do I deserialize a raw Solana Account to obtain this state. Other solutions i have come across involve using AccountInfo but I want to achieve this with rust without using anchor

1 Answer 1

3

AccountInfo is not an Anchor type, but rather a solana-program type. So if you want to get the state, you can deserialize from a given AccountInfo using:

solana_program::entrypoint!(process_instruction);

pub fn process_instruction(
    program_id: &Pubkey,
    accounts: &[AccountInfo],
    _instruction_data: &[u8],
) -> ProgramResult {
    // Create an iterator to safely reference accounts in the slice
    let account_info_iter = &mut accounts.iter();
    let token_account_info = next_account_info(account_info_iter)?;
    let token_account = Account::unpack(&token_account_info.try_borrow_data()?)?;
    let state = token_account.state;
}

Adapted from https://github.com/solana-labs/solana-program-library/blob/1044fe47f7bf005b64c11a8a867b911ae13ae442/examples/rust/transfer-tokens/src/processor.rs#L44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.