From eae99acf757e0cca0103370c245443cd73b081ae Mon Sep 17 00:00:00 2001 From: blockiosaurus Date: Sun, 18 Feb 2024 11:15:43 -0700 Subject: [PATCH] Splitting trait. --- clients/js/src/generated/types/plugin.ts | 22 +------- clients/rust/src/generated/types/plugin.rs | 3 - idls/mpl_asset_program.json | 9 --- programs/mpl-asset/src/plugins/delegate.rs | 8 ++- programs/mpl-asset/src/plugins/mod.rs | 56 ++----------------- programs/mpl-asset/src/plugins/utils.rs | 9 +-- programs/mpl-asset/src/processor/transfer.rs | 4 +- programs/mpl-asset/src/state/asset.rs | 7 +-- programs/mpl-asset/src/state/hashed_asset.rs | 4 +- programs/mpl-asset/src/state/mod.rs | 3 + programs/mpl-asset/src/state/plugin_header.rs | 4 +- programs/mpl-asset/src/state/traits.rs | 44 +++++++++++++++ programs/mpl-asset/src/utils.rs | 34 +---------- 13 files changed, 75 insertions(+), 132 deletions(-) create mode 100644 programs/mpl-asset/src/state/traits.rs diff --git a/clients/js/src/generated/types/plugin.ts b/clients/js/src/generated/types/plugin.ts index bc7fa052..889abca0 100644 --- a/clients/js/src/generated/types/plugin.ts +++ b/clients/js/src/generated/types/plugin.ts @@ -20,33 +20,24 @@ import { Delegate, DelegateArgs, getDelegateSerializer } from '.'; export type Plugin = | { __kind: 'Reserved' } | { __kind: 'Royalties' } - | { __kind: 'MasterEdition' } - | { __kind: 'PrintEdition' } - | { __kind: 'Delegate'; fields: [Delegate] } - | { __kind: 'Inscription' }; + | { __kind: 'Delegate'; fields: [Delegate] }; export type PluginArgs = | { __kind: 'Reserved' } | { __kind: 'Royalties' } - | { __kind: 'MasterEdition' } - | { __kind: 'PrintEdition' } - | { __kind: 'Delegate'; fields: [DelegateArgs] } - | { __kind: 'Inscription' }; + | { __kind: 'Delegate'; fields: [DelegateArgs] }; export function getPluginSerializer(): Serializer { return dataEnum( [ ['Reserved', unit()], ['Royalties', unit()], - ['MasterEdition', unit()], - ['PrintEdition', unit()], [ 'Delegate', struct>([ ['fields', tuple([getDelegateSerializer()])], ]), ], - ['Inscription', unit()], ], { description: 'Plugin' } ) as Serializer; @@ -59,19 +50,10 @@ export function plugin( export function plugin( kind: 'Royalties' ): GetDataEnumKind; -export function plugin( - kind: 'MasterEdition' -): GetDataEnumKind; -export function plugin( - kind: 'PrintEdition' -): GetDataEnumKind; export function plugin( kind: 'Delegate', data: GetDataEnumKindContent['fields'] ): GetDataEnumKind; -export function plugin( - kind: 'Inscription' -): GetDataEnumKind; export function plugin( kind: K, data?: any diff --git a/clients/rust/src/generated/types/plugin.rs b/clients/rust/src/generated/types/plugin.rs index 3f54bda4..c2241d62 100644 --- a/clients/rust/src/generated/types/plugin.rs +++ b/clients/rust/src/generated/types/plugin.rs @@ -14,8 +14,5 @@ use borsh::BorshSerialize; pub enum Plugin { Reserved, Royalties, - MasterEdition, - PrintEdition, Delegate(Delegate), - Inscription, } diff --git a/idls/mpl_asset_program.json b/idls/mpl_asset_program.json index c5f55b0d..ac3b05ce 100644 --- a/idls/mpl_asset_program.json +++ b/idls/mpl_asset_program.json @@ -1091,12 +1091,6 @@ { "name": "Royalties" }, - { - "name": "MasterEdition" - }, - { - "name": "PrintEdition" - }, { "name": "Delegate", "fields": [ @@ -1104,9 +1098,6 @@ "defined": "Delegate" } ] - }, - { - "name": "Inscription" } ] } diff --git a/programs/mpl-asset/src/plugins/delegate.rs b/programs/mpl-asset/src/plugins/delegate.rs index e8b18d43..0dc05bb2 100644 --- a/programs/mpl-asset/src/plugins/delegate.rs +++ b/programs/mpl-asset/src/plugins/delegate.rs @@ -1,7 +1,9 @@ use borsh::{BorshDeserialize, BorshSerialize}; -use shank::ShankAccount; -use crate::{state::Key, utils::DataBlob}; +use crate::{ + state::Key, + state::{DataBlob, SolanaAccount}, +}; #[repr(C)] #[derive(Clone, BorshSerialize, BorshDeserialize, Debug, PartialEq, Eq)] @@ -33,7 +35,9 @@ impl DataBlob for Delegate { fn get_size(&self) -> usize { 2 } +} +impl SolanaAccount for Delegate { fn key() -> crate::state::Key { Key::Delegate } diff --git a/programs/mpl-asset/src/plugins/mod.rs b/programs/mpl-asset/src/plugins/mod.rs index 27235a3b..2f476a07 100644 --- a/programs/mpl-asset/src/plugins/mod.rs +++ b/programs/mpl-asset/src/plugins/mod.rs @@ -12,35 +12,16 @@ pub use utils::*; use borsh::{BorshDeserialize, BorshSerialize}; -use crate::{ - state::{Authority, Key}, - utils::DataBlob, -}; - -// macro_rules! plugin_instruction { -// ($a:expr, $b:expr) => { -// (($a as u32) << 16u32) | ($b as u32) -// }; -// } +use crate::state::{Authority, DataBlob, Key, SolanaAccount}; #[repr(u16)] #[derive(Clone, Debug, BorshSerialize, BorshDeserialize, Eq, PartialEq)] pub enum Plugin { Reserved, Royalties, - MasterEdition, - PrintEdition, Delegate(Delegate), - Inscription, } -// #[repr(u32)] -// pub enum NonFungibleInstructions { -// Create = plugin_instruction!(Plugin::Metadata, 0), -// Transfer = plugin_instruction!(Plugin::Metadata, 1), -// Burn = plugin_instruction!(Plugin::Metadata, 2), -//} - #[repr(C)] #[derive(Clone, BorshSerialize, BorshDeserialize, Debug)] pub struct RegistryData { @@ -70,28 +51,7 @@ pub struct PluginRegistry { pub external_plugins: Vec, // 4 } -// impl PluginRegistry { -// pub fn load(account: &AccountInfo, offset: usize) -> Result { -// let mut bytes: &[u8] = &(*account.data).borrow()[offset..]; -// PluginRegistry::deserialize(&mut bytes).map_err(|error| { -// msg!("Error: {}", error); -// MplAssetError::DeserializationError.into() -// }) -// } - -// pub fn save(&self, account: &AccountInfo, offset: usize) -> ProgramResult { -// borsh::to_writer(&mut account.data.borrow_mut()[offset..], self).map_err(|error| { -// msg!("Error: {}", error); -// MplAssetError::SerializationError.into() -// }) -// } -// } - impl DataBlob for PluginRegistry { - fn key() -> Key { - Key::PluginRegistry - } - fn get_initial_size() -> usize { 9 } @@ -101,12 +61,8 @@ impl DataBlob for PluginRegistry { } } -// pub trait PluginTrait -// where -// Self: BorshSerialize + BorshDeserialize + Clone + std::fmt::Debug + Sized, -// { -// fn get_plugin() -> Plugin; -// fn get_authority(&self) -> Option { -// None -// } -// } +impl SolanaAccount for PluginRegistry { + fn key() -> Key { + Key::PluginRegistry + } +} diff --git a/programs/mpl-asset/src/plugins/utils.rs b/programs/mpl-asset/src/plugins/utils.rs index fd059b6b..9d49031a 100644 --- a/programs/mpl-asset/src/plugins/utils.rs +++ b/programs/mpl-asset/src/plugins/utils.rs @@ -6,8 +6,7 @@ use solana_program::{ use crate::{ error::MplAssetError, - state::{Asset, Authority, Key, PluginHeader}, - utils::DataBlob, + state::{Asset, Authority, DataBlob, Key, PluginHeader, SolanaAccount}, }; use super::{Plugin, PluginRegistry, RegistryData, RegistryRecord}; @@ -127,10 +126,7 @@ pub fn add_plugin<'a>( let (plugin_size, key) = match plugin { Plugin::Reserved => todo!(), Plugin::Royalties => todo!(), - Plugin::MasterEdition => todo!(), - Plugin::PrintEdition => todo!(), Plugin::Delegate(delegate) => (delegate.get_size(), Key::Delegate), - Plugin::Inscription => todo!(), }; let authority_bytes = authority.try_to_vec()?; @@ -195,10 +191,7 @@ pub fn add_plugin<'a>( match plugin { Plugin::Reserved => todo!(), Plugin::Royalties => todo!(), - Plugin::MasterEdition => todo!(), - Plugin::PrintEdition => todo!(), Plugin::Delegate(delegate) => delegate.save(account, old_registry_offset)?, - Plugin::Inscription => todo!(), }; solana_program::msg!("Saving plugin registry"); diff --git a/programs/mpl-asset/src/processor/transfer.rs b/programs/mpl-asset/src/processor/transfer.rs index 6a8cf5e5..34bdd859 100644 --- a/programs/mpl-asset/src/processor/transfer.rs +++ b/programs/mpl-asset/src/processor/transfer.rs @@ -5,8 +5,8 @@ use solana_program::{account_info::AccountInfo, entrypoint::ProgramResult, progr use crate::{ error::MplAssetError, instruction::accounts::TransferAccounts, - state::{Asset, Compressible, CompressionProof, HashedAsset, Key}, - utils::{load_key, DataBlob}, + state::{Asset, Compressible, CompressionProof, HashedAsset, Key, SolanaAccount}, + utils::load_key, }; #[repr(C)] diff --git a/programs/mpl-asset/src/state/asset.rs b/programs/mpl-asset/src/state/asset.rs index c67fc0aa..8ca6e494 100644 --- a/programs/mpl-asset/src/state/asset.rs +++ b/programs/mpl-asset/src/state/asset.rs @@ -2,10 +2,7 @@ use borsh::{BorshDeserialize, BorshSerialize}; use shank::ShankAccount; use solana_program::{keccak, program_error::ProgramError, pubkey::Pubkey}; -use crate::{ - state::{CompressionProof, Key}, - utils::DataBlob, -}; +use crate::state::{CompressionProof, DataBlob, Key, SolanaAccount}; use super::Compressible; @@ -38,7 +35,9 @@ impl DataBlob for Asset { fn get_size(&self) -> usize { Asset::BASE_LENGTH + self.name.len() + self.uri.len() } +} +impl SolanaAccount for Asset { fn key() -> Key { Key::Asset } diff --git a/programs/mpl-asset/src/state/hashed_asset.rs b/programs/mpl-asset/src/state/hashed_asset.rs index 0c3931b8..d63a3cf7 100644 --- a/programs/mpl-asset/src/state/hashed_asset.rs +++ b/programs/mpl-asset/src/state/hashed_asset.rs @@ -1,7 +1,7 @@ use borsh::{BorshDeserialize, BorshSerialize}; use shank::ShankAccount; -use crate::{state::Key, utils::DataBlob}; +use crate::state::{DataBlob, Key, SolanaAccount}; #[derive(Clone, BorshSerialize, BorshDeserialize, Debug, ShankAccount, PartialEq, Eq)] pub struct HashedAsset { @@ -28,7 +28,9 @@ impl DataBlob for HashedAsset { fn get_size(&self) -> usize { HashedAsset::LENGTH } +} +impl SolanaAccount for HashedAsset { fn key() -> Key { Key::HashedAsset } diff --git a/programs/mpl-asset/src/state/mod.rs b/programs/mpl-asset/src/state/mod.rs index 9faea0f4..2d1e6655 100644 --- a/programs/mpl-asset/src/state/mod.rs +++ b/programs/mpl-asset/src/state/mod.rs @@ -8,6 +8,9 @@ mod plugin_header; use num_derive::FromPrimitive; pub use plugin_header::*; +mod traits; +pub use traits::*; + use borsh::{BorshDeserialize, BorshSerialize}; use solana_program::program_error::ProgramError; use solana_program::pubkey::Pubkey; diff --git a/programs/mpl-asset/src/state/plugin_header.rs b/programs/mpl-asset/src/state/plugin_header.rs index 1caf91b8..2fa98b66 100644 --- a/programs/mpl-asset/src/state/plugin_header.rs +++ b/programs/mpl-asset/src/state/plugin_header.rs @@ -1,4 +1,4 @@ -use crate::{state::Key, utils::DataBlob}; +use crate::state::{DataBlob, Key, SolanaAccount}; use borsh::{BorshDeserialize, BorshSerialize}; use shank::ShankAccount; @@ -17,7 +17,9 @@ impl DataBlob for PluginHeader { fn get_size(&self) -> usize { 1 + 8 } +} +impl SolanaAccount for PluginHeader { fn key() -> Key { Key::PluginHeader } diff --git a/programs/mpl-asset/src/state/traits.rs b/programs/mpl-asset/src/state/traits.rs new file mode 100644 index 00000000..85832acc --- /dev/null +++ b/programs/mpl-asset/src/state/traits.rs @@ -0,0 +1,44 @@ +use crate::{error::MplAssetError, state::Key}; +use borsh::{BorshDeserialize, BorshSerialize}; +use num_traits::FromPrimitive; +use solana_program::account_info::AccountInfo; +use solana_program::entrypoint::ProgramResult; +use solana_program::msg; +use solana_program::program_error::ProgramError; + +pub trait DataBlob: BorshSerialize + BorshDeserialize { + fn get_initial_size() -> usize; + fn get_size(&self) -> usize; +} + +pub trait SolanaAccount: BorshSerialize + BorshDeserialize { + fn key() -> Key; + + fn load(account: &AccountInfo, offset: usize) -> Result { + let key = load_key(account, offset)?; + + if key != Self::key() { + return Err(MplAssetError::DeserializationError.into()); + } + + let mut bytes: &[u8] = &(*account.data).borrow()[offset..]; + Self::deserialize(&mut bytes).map_err(|error| { + msg!("Error: {}", error); + MplAssetError::DeserializationError.into() + }) + } + + fn save(&self, account: &AccountInfo, offset: usize) -> ProgramResult { + borsh::to_writer(&mut account.data.borrow_mut()[offset..], self).map_err(|error| { + msg!("Error: {}", error); + MplAssetError::SerializationError.into() + }) + } +} + +pub fn load_key(account: &AccountInfo, offset: usize) -> Result { + let key = Key::from_u8((*account.data).borrow()[offset]) + .ok_or(MplAssetError::DeserializationError)?; + + Ok(key) +} diff --git a/programs/mpl-asset/src/utils.rs b/programs/mpl-asset/src/utils.rs index 7df899c1..0e8f8412 100644 --- a/programs/mpl-asset/src/utils.rs +++ b/programs/mpl-asset/src/utils.rs @@ -1,37 +1,7 @@ -use crate::{error::MplAssetError, state::Key}; -use borsh::{BorshDeserialize, BorshSerialize}; use num_traits::FromPrimitive; -use solana_program::account_info::AccountInfo; -use solana_program::entrypoint::ProgramResult; -use solana_program::msg; -use solana_program::program_error::ProgramError; - -pub trait DataBlob: BorshSerialize + BorshDeserialize { - fn get_initial_size() -> usize; - fn get_size(&self) -> usize; - fn key() -> Key; - - fn load(account: &AccountInfo, offset: usize) -> Result { - let key = load_key(account, offset)?; - - if key != Self::key() { - return Err(MplAssetError::DeserializationError.into()); - } +use solana_program::{account_info::AccountInfo, program_error::ProgramError}; - let mut bytes: &[u8] = &(*account.data).borrow()[offset..]; - Self::deserialize(&mut bytes).map_err(|error| { - msg!("Error: {}", error); - MplAssetError::DeserializationError.into() - }) - } - - fn save(&self, account: &AccountInfo, offset: usize) -> ProgramResult { - borsh::to_writer(&mut account.data.borrow_mut()[offset..], self).map_err(|error| { - msg!("Error: {}", error); - MplAssetError::SerializationError.into() - }) - } -} +use crate::{error::MplAssetError, state::Key}; pub fn load_key(account: &AccountInfo, offset: usize) -> Result { let key = Key::from_u8((*account.data).borrow()[offset])