Skip to content

Commit

Permalink
Splitting trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
blockiosaurus committed Feb 18, 2024
1 parent 885328d commit eae99ac
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 132 deletions.
22 changes: 2 additions & 20 deletions clients/js/src/generated/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PluginArgs, Plugin> {
return dataEnum<Plugin>(
[
['Reserved', unit()],
['Royalties', unit()],
['MasterEdition', unit()],
['PrintEdition', unit()],
[
'Delegate',
struct<GetDataEnumKindContent<Plugin, 'Delegate'>>([
['fields', tuple([getDelegateSerializer()])],
]),
],
['Inscription', unit()],
],
{ description: 'Plugin' }
) as Serializer<PluginArgs, Plugin>;
Expand All @@ -59,19 +50,10 @@ export function plugin(
export function plugin(
kind: 'Royalties'
): GetDataEnumKind<PluginArgs, 'Royalties'>;
export function plugin(
kind: 'MasterEdition'
): GetDataEnumKind<PluginArgs, 'MasterEdition'>;
export function plugin(
kind: 'PrintEdition'
): GetDataEnumKind<PluginArgs, 'PrintEdition'>;
export function plugin(
kind: 'Delegate',
data: GetDataEnumKindContent<PluginArgs, 'Delegate'>['fields']
): GetDataEnumKind<PluginArgs, 'Delegate'>;
export function plugin(
kind: 'Inscription'
): GetDataEnumKind<PluginArgs, 'Inscription'>;
export function plugin<K extends PluginArgs['__kind']>(
kind: K,
data?: any
Expand Down
3 changes: 0 additions & 3 deletions clients/rust/src/generated/types/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,5 @@ use borsh::BorshSerialize;
pub enum Plugin {
Reserved,
Royalties,
MasterEdition,
PrintEdition,
Delegate(Delegate),
Inscription,
}
9 changes: 0 additions & 9 deletions idls/mpl_asset_program.json
Original file line number Diff line number Diff line change
Expand Up @@ -1091,22 +1091,13 @@
{
"name": "Royalties"
},
{
"name": "MasterEdition"
},
{
"name": "PrintEdition"
},
{
"name": "Delegate",
"fields": [
{
"defined": "Delegate"
}
]
},
{
"name": "Inscription"
}
]
}
Expand Down
8 changes: 6 additions & 2 deletions programs/mpl-asset/src/plugins/delegate.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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
}
Expand Down
56 changes: 6 additions & 50 deletions programs/mpl-asset/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -70,28 +51,7 @@ pub struct PluginRegistry {
pub external_plugins: Vec<ExternalPluginRecord>, // 4
}

// impl PluginRegistry {
// pub fn load(account: &AccountInfo, offset: usize) -> Result<Self, ProgramError> {
// 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
}
Expand All @@ -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<Pubkey> {
// None
// }
// }
impl SolanaAccount for PluginRegistry {
fn key() -> Key {
Key::PluginRegistry
}
}
9 changes: 1 addition & 8 deletions programs/mpl-asset/src/plugins/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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()?;

Expand Down Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions programs/mpl-asset/src/processor/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
7 changes: 3 additions & 4 deletions programs/mpl-asset/src/state/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion programs/mpl-asset/src/state/hashed_asset.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -28,7 +28,9 @@ impl DataBlob for HashedAsset {
fn get_size(&self) -> usize {
HashedAsset::LENGTH
}
}

impl SolanaAccount for HashedAsset {
fn key() -> Key {
Key::HashedAsset
}
Expand Down
3 changes: 3 additions & 0 deletions programs/mpl-asset/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion programs/mpl-asset/src/state/plugin_header.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{state::Key, utils::DataBlob};
use crate::state::{DataBlob, Key, SolanaAccount};
use borsh::{BorshDeserialize, BorshSerialize};
use shank::ShankAccount;

Expand All @@ -17,7 +17,9 @@ impl DataBlob for PluginHeader {
fn get_size(&self) -> usize {
1 + 8
}
}

impl SolanaAccount for PluginHeader {
fn key() -> Key {
Key::PluginHeader
}
Expand Down
44 changes: 44 additions & 0 deletions programs/mpl-asset/src/state/traits.rs
Original file line number Diff line number Diff line change
@@ -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<Self, ProgramError> {
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<Key, ProgramError> {
let key = Key::from_u8((*account.data).borrow()[offset])
.ok_or(MplAssetError::DeserializationError)?;

Ok(key)
}
34 changes: 2 additions & 32 deletions programs/mpl-asset/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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<Self, ProgramError> {
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<Key, ProgramError> {
let key = Key::from_u8((*account.data).borrow()[offset])
Expand Down

0 comments on commit eae99ac

Please sign in to comment.