use crate::generated::types::Key;
use crate::generated::types::TokenDelegateRole;
use crate::generated::types::TokenState;
#[cfg(feature = "anchor")]
use anchor_lang::prelude::{AnchorDeserialize, AnchorSerialize};
#[cfg(not(feature = "anchor"))]
use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::pubkey::Pubkey;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(not(feature = "anchor"), derive(BorshSerialize, BorshDeserialize))]
#[cfg_attr(feature = "anchor", derive(AnchorSerialize, AnchorDeserialize))]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TokenRecord {
pub key: Key,
pub bump: u8,
pub state: TokenState,
pub rule_set_revision: Option<u64>,
pub delegate: Option<Pubkey>,
pub delegate_role: Option<TokenDelegateRole>,
pub locked_transfer: Option<Pubkey>,
}
impl TokenRecord {
pub const LEN: usize = 80;
pub const PREFIX: (&'static [u8], &'static [u8]) =
("metadata".as_bytes(), "token_record".as_bytes());
pub fn create_pda(
mint: Pubkey,
token: Pubkey,
bump: u8,
) -> Result<solana_program::pubkey::Pubkey, solana_program::pubkey::PubkeyError> {
solana_program::pubkey::Pubkey::create_program_address(
&[
"metadata".as_bytes(),
crate::MPL_TOKEN_METADATA_ID.as_ref(),
mint.as_ref(),
"token_record".as_bytes(),
token.as_ref(),
&[bump],
],
&crate::MPL_TOKEN_METADATA_ID,
)
}
pub fn find_pda(mint: &Pubkey, token: &Pubkey) -> (solana_program::pubkey::Pubkey, u8) {
solana_program::pubkey::Pubkey::find_program_address(
&[
"metadata".as_bytes(),
crate::MPL_TOKEN_METADATA_ID.as_ref(),
mint.as_ref(),
"token_record".as_bytes(),
token.as_ref(),
],
&crate::MPL_TOKEN_METADATA_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 TokenRecord {
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)
}
}