Browse Source

spl: Add `TokenRecordAccount` for pNFTs (#2597)

Jimii 2 years ago
parent
commit
b5cf67f13b
1 changed files with 36 additions and 0 deletions
  1. 36 0
      spl/src/metadata.rs

+ 36 - 0
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<Self> {
+        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<Self> {
+        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;