From b5cf67f13ba2806f1c7168cf437d086d034c8185 Mon Sep 17 00:00:00 2001 From: Jimii <30603522+jim4067@users.noreply.github.com> Date: Sat, 12 Aug 2023 23:02:37 +0300 Subject: [PATCH] spl: Add `TokenRecordAccount` for pNFTs (#2597) --- spl/src/metadata.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/spl/src/metadata.rs b/spl/src/metadata.rs index 76f7c4ec1a..9172b86dab 100644 --- a/spl/src/metadata.rs +++ b/spl/src/metadata.rs @@ -819,6 +819,42 @@ impl anchor_lang::Owner for MasterEditionAccount { } } +#[derive(Clone, Debug, PartialEq)] +pub struct TokenRecordAccount(mpl_token_metadata::state::TokenRecord); + +impl TokenRecordAccount { + pub const LEN: usize = mpl_token_metadata::state::TOKEN_RECORD_SIZE; +} +impl anchor_lang::AccountDeserialize for TokenRecordAccount { + fn try_deserialize(buf: &mut &[u8]) -> anchor_lang::Result { + let tr = Self::try_deserialize_unchecked(buf)?; + if tr.key != mpl_token_metadata::state::TokenRecord::key() { + return Err(ErrorCode::AccountNotInitialized.into()); + } + Ok(tr) + } + + fn try_deserialize_unchecked(buf: &mut &[u8]) -> anchor_lang::Result { + let tr = mpl_token_metadata::state::TokenRecord::safe_deserialize(buf)?; + Ok(Self(tr)) + } +} + +impl anchor_lang::AccountSerialize for TokenRecordAccount {} + +impl anchor_lang::Owner for TokenRecordAccount { + fn owner() -> Pubkey { + ID + } +} + +impl Deref for TokenRecordAccount { + type Target = mpl_token_metadata::state::TokenRecord; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + #[derive(Clone)] pub struct Metadata;