In order to access the data of an account, it has to be expected as an account input to the instruction and provided as AccountInfo
.
Once you have the token account info, it can be deserialized into its appropriate data structure with:
let mut data: &[u8] = &info.try_borrow_data()?;
let acc = TokenAccount::try_deserialize(&mut data)?;
If you are using anchor, this can all be done for you if you include the account in the instruction's account context struct:
#[derive(Accounts)]
pub struct MyInstruction<'info> {
// ...
pub token_acc: Account<'info, TokenAccount>,
// ...
}
then accessing the token account data within the instruction simply becomes:
let amount = ctx.accounts.token_acc.amount;
(or whatever data you need to access)