name_tokenizer/state/
nft_record.rs

1use bonfida_utils::BorshSize;
2use borsh::{BorshDeserialize, BorshSerialize};
3use solana_program::{account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey};
4
5use crate::error::OfferError;
6
7use super::Tag;
8
9#[derive(BorshSerialize, BorshDeserialize, BorshSize)]
10#[allow(missing_docs)]
11pub struct NftRecord {
12    /// Tag
13    pub tag: Tag,
14
15    /// Nonce
16    pub nonce: u8,
17
18    /// Name account of the record
19    pub name_account: Pubkey,
20
21    /// Record owner
22    pub owner: Pubkey,
23
24    /// NFT mint
25    pub nft_mint: Pubkey,
26}
27
28#[allow(missing_docs)]
29impl NftRecord {
30    pub const SEED: &'static [u8; 10] = b"nft_record";
31
32    pub fn new(nonce: u8, owner: Pubkey, name_account: Pubkey, nft_mint: Pubkey) -> Self {
33        Self {
34            tag: Tag::ActiveRecord,
35            nonce,
36            owner,
37            name_account,
38            nft_mint,
39        }
40    }
41
42    pub fn find_key(name_account: &Pubkey, program_id: &Pubkey) -> (Pubkey, u8) {
43        let seeds: &[&[u8]] = &[NftRecord::SEED, &name_account.to_bytes()];
44        Pubkey::find_program_address(seeds, program_id)
45    }
46
47    pub fn save(&self, mut dst: &mut [u8]) {
48        self.serialize(&mut dst).unwrap()
49    }
50
51    pub fn from_account_info(a: &AccountInfo, tag: Tag) -> Result<NftRecord, ProgramError> {
52        let mut data = &a.data.borrow() as &[u8];
53        if data[0] != tag as u8 {
54            return Err(OfferError::DataTypeMismatch.into());
55        }
56        let result = NftRecord::deserialize(&mut data)?;
57        Ok(result)
58    }
59
60    pub fn is_active(&self) -> bool {
61        self.tag == Tag::ActiveRecord
62    }
63}