use crate::generated::types::Tag;
use crate::generated::types::Tree;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;
use solana_program::pubkey::Pubkey;
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TokenAccount {
pub tag: Tag,
pub empty: [u8; 3],
#[cfg_attr(
feature = "serde",
serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
)]
pub authority: Pubkey,
#[cfg_attr(
feature = "serde",
serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
)]
pub user: Pubkey,
pub tree: Tree,
}
impl TokenAccount {
pub const PREFIX: &'static [u8] = "token_account".as_bytes();
pub fn create_pda(
user: Pubkey,
authority: Pubkey,
bump: u8,
) -> Result<solana_program::pubkey::Pubkey, solana_program::pubkey::PubkeyError> {
solana_program::pubkey::Pubkey::create_program_address(
&[
"token_account".as_bytes(),
user.as_ref(),
authority.as_ref(),
&[bump],
],
&crate::SIGIL_ID,
)
}
pub fn find_pda(user: &Pubkey, authority: &Pubkey) -> (solana_program::pubkey::Pubkey, u8) {
solana_program::pubkey::Pubkey::find_program_address(
&[
"token_account".as_bytes(),
user.as_ref(),
authority.as_ref(),
],
&crate::SIGIL_ID,
)
}
#[inline(always)]
pub fn from_bytes(data: &[u8]) -> Result<Self, std::io::Error> {
let mut data = data;
Self::deserialize(&mut data)
}
}
impl<'a> TryFrom<&solana_program::account_info::AccountInfo<'a>> for TokenAccount {
type Error = std::io::Error;
fn try_from(
account_info: &solana_program::account_info::AccountInfo<'a>,
) -> Result<Self, Self::Error> {
let mut data: &[u8] = &(*account_info.data).borrow();
Self::deserialize(&mut data)
}
}