Skip to content

Commit

Permalink
Merging code.
Browse files Browse the repository at this point in the history
  • Loading branch information
blockiosaurus committed Feb 18, 2024
2 parents 433025d + b6445b9 commit 3ccc714
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 61 deletions.
6 changes: 6 additions & 0 deletions programs/mpl-asset/src/plugins/delegate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use borsh::{BorshDeserialize, BorshSerialize};

#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, PartialEq, Eq)]
pub struct Delegate {
frozen: bool,
}
41 changes: 25 additions & 16 deletions programs/mpl-asset/src/plugins/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
mod collection;
mod delegate;
mod royalties;
mod utils;

pub use collection::*;
pub use delegate::*;
pub use royalties::*;

use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, msg, program_error::ProgramError,
};
Expand All @@ -30,7 +33,7 @@ pub enum Plugin {
Royalties,
MasterEdition,
PrintEdition,
Delegate,
Delegate(Delegate),
Inscription,
}

Expand All @@ -51,27 +54,33 @@ pub struct RegistryData {
#[repr(C)]
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug)]
pub struct PluginRegistry {
pub key: Key,
pub registry: Vec<(Key, RegistryData)>, // 4
pub external_plugins: Vec<(Authority, RegistryData)>,
}

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()
})
}
// 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()
})
}
}
// 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 {
4
}
Expand Down
35 changes: 31 additions & 4 deletions programs/mpl-asset/src/plugins/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ pub fn create_idempotent<'a>(
if asset.get_size() == account.data_len() {
// They don't exist, so create them.
let header = PluginHeader {
version: 1,
key: Key::PluginHeader,
plugin_map_offset: asset.get_size() + PluginHeader::get_initial_size(),
};
let registry = PluginRegistry { registry: vec![] };
let registry = PluginRegistry {
key: Key::PluginRegistry,
registry: vec![],
external_plugins: vec![],
};

resize_or_reallocate_account_raw(
account,
Expand Down Expand Up @@ -66,7 +70,7 @@ pub fn fetch_plugin(
let asset = Asset::deserialize(&mut bytes)?;

let header = PluginHeader::load(account, asset.get_size())?;
let PluginRegistry { registry } = PluginRegistry::load(account, header.plugin_map_offset)?;
let PluginRegistry { registry, .. } = PluginRegistry::load(account, header.plugin_map_offset)?;

// Find the plugin in the registry.
let plugin_data = registry
Expand All @@ -88,7 +92,30 @@ pub fn list_plugins(account: &AccountInfo) -> Result<Vec<Key>, ProgramError> {
let asset = Asset::deserialize(&mut bytes)?;

let header = PluginHeader::load(account, asset.get_size())?;
let PluginRegistry { registry } = PluginRegistry::load(account, header.plugin_map_offset)?;
let PluginRegistry { registry, .. } = PluginRegistry::load(account, header.plugin_map_offset)?;

Ok(registry.iter().map(|(key, _)| *key).collect())
}

/// Add a plugin into the registry
pub fn add_plugin(plugin: &Plugin, authority: Authority, account: &AccountInfo) -> ProgramResult {
let mut bytes: &[u8] = &(*account.data).borrow();
let asset = Asset::deserialize(&mut bytes)?;

//TODO: Bytemuck this.
let mut header = PluginHeader::load(account, asset.get_size())?;
let mut plugin_registry = PluginRegistry::load(account, header.plugin_map_offset)?;

let plugin_type = match plugin {
Plugin::Reserved => todo!(),
Plugin::Royalties => todo!(),
Plugin::MasterEdition => todo!(),
Plugin::PrintEdition => todo!(),
Plugin::Delegate(delegate) => todo!(),
Plugin::Inscription => todo!(),
};

// plugin_registry.registry.push(());

Ok(())
}
13 changes: 2 additions & 11 deletions programs/mpl-asset/src/processor/delegate.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
use borsh::{BorshDeserialize, BorshSerialize};
use mpl_utils::assert_signer;
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, program::invoke,
program_memory::sol_memcpy, rent::Rent, system_instruction, system_program, sysvar::Sysvar,
};
use solana_program::{account_info::AccountInfo, entrypoint::ProgramResult};

use crate::{
error::MplAssetError,
instruction::accounts::{CreateAccounts, DelegateAccounts},
plugins::create_idempotent,
state::{Asset, Compressible, DataState, HashedAsset, Key},
};
use crate::{instruction::accounts::DelegateAccounts, plugins::create_idempotent};

#[repr(C)]
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Debug, Clone)]
Expand Down
88 changes: 86 additions & 2 deletions programs/mpl-asset/src/processor/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,103 @@ use borsh::{BorshDeserialize, BorshSerialize};
use mpl_utils::assert_signer;
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, program::invoke,
program_memory::sol_memcpy, rent::Rent, system_instruction, system_program, sysvar::Sysvar,
program_memory::sol_memcpy, system_instruction, system_program,
};

use crate::{
error::MplAssetError,
instruction::accounts::CreateAccounts,
instruction::accounts::TransferAccounts,
state::{Asset, Compressible, DataState, HashedAsset, Key},
utils::DataBlob,
};

#[repr(C)]
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Debug, Clone)]
pub struct TransferArgs {}

// // danenbm working on
// #[account(0, writable, name="asset_address", desc = "The address of the asset")]
// #[account(1, optional, name="collection", desc = "The collection to which the asset belongs")]
// #[account(2, signer, name="authority", desc = "The owner or delegate of the asset")]
// #[account(3, optional, writable, signer, name="payer", desc = "The account paying for the storage fees")]
// #[account(4, name="new_owner", desc = "The new owner to which to transfer the asset")]
// #[account(5, optional, name="log_wrapper", desc = "The SPL Noop Program")]
// Transfer(TransferArgs),

pub(crate) fn transfer<'a>(accounts: &'a [AccountInfo<'a>], args: TransferArgs) -> ProgramResult {
// Accounts.
let ctx = TransferAccounts::context(accounts)?;

// Guards.
assert_signer(ctx.accounts.authority)?;

if let Some(payer) = ctx.accounts.payer {
assert_signer(payer)?;
}

let asset = Asset::load(ctx.accounts.asset_address, 0)?;

asset.owner = ctx.accounts.new_owner.key();

// if *ctx.accounts.system_program.key != system_program::id() {
// return Err(MplAssetError::InvalidSystemProgram.into());
// }

// let updated_asset = Asset {
// key: Key::Asset,
// update_authority: *ctx
// .accounts
// .update_authority
// .unwrap_or(ctx.accounts.payer)
// .key,
// owner: *ctx
// .accounts
// .owner
// .unwrap_or(ctx.accounts.update_authority.unwrap_or(ctx.accounts.payer))
// .key,
// name: args.name,
// uri: args.uri,
// };

// let serialized_data = new_asset.try_to_vec()?;

// let serialized_data = match args.data_state {
// DataState::AccountState => serialized_data,
// DataState::LedgerState => {
// invoke(&spl_noop::instruction(serialized_data.clone()), &[])?;

// let hashed_asset = HashedAsset {
// key: Key::HashedAsset,
// hash: new_asset.hash()?,
// };

// hashed_asset.try_to_vec()?
// }
// };

//let lamports = rent.minimum_balance(serialized_data.len());

// CPI to the System Program.
// invoke(
// &system_instruction::create_account(
// ctx.accounts.payer.key,
// ctx.accounts.asset_address.key,
// lamports,
// serialized_data.len() as u64,
// &crate::id(),
// ),
// &[
// ctx.accounts.payer.clone(),
// ctx.accounts.asset_address.clone(),
// ctx.accounts.system_program.clone(),
// ],
// )?;

// sol_memcpy(
// &mut ctx.accounts.asset_address.try_borrow_mut_data()?,
// &serialized_data,
// serialized_data.len(),
// );

Ok(())
}
8 changes: 6 additions & 2 deletions programs/mpl-asset/src/state/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use borsh::{BorshDeserialize, BorshSerialize};
use shank::ShankAccount;
use solana_program::{keccak, program_error::ProgramError, pubkey::Pubkey};

use crate::utils::DataBlob;
use crate::{state::Key, utils::DataBlob};

use super::{Compressible, Key};
use super::Compressible;

#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, ShankAccount)]
pub struct Asset {
Expand Down Expand Up @@ -35,6 +35,10 @@ impl DataBlob for Asset {
fn get_size(&self) -> usize {
Asset::BASE_LENGTH + self.name.len() + self.uri.len()
}

fn key() -> Key {
Key::Asset
}
}

#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, ShankAccount)]
Expand Down
5 changes: 4 additions & 1 deletion programs/mpl-asset/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod asset;
pub use asset::*;

mod plugin_header;
use num_derive::FromPrimitive;
pub use plugin_header::*;

use borsh::{BorshDeserialize, BorshSerialize};
Expand Down Expand Up @@ -44,13 +45,15 @@ pub trait Compressible {
fn hash(&self) -> Result<[u8; 32], ProgramError>;
}

#[derive(Clone, Copy, BorshSerialize, BorshDeserialize, Debug, PartialEq, Eq)]
#[derive(Clone, Copy, BorshSerialize, BorshDeserialize, Debug, PartialEq, Eq, FromPrimitive)]
pub enum Key {
Uninitialized,
Asset,
HashedAsset,
Collection,
HashedCollection,
PluginHeader,
PluginRegistry,
}

#[repr(C)]
Expand Down
30 changes: 6 additions & 24 deletions programs/mpl-asset/src/state/plugin_header.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,14 @@
use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::account_info::AccountInfo;
use solana_program::entrypoint::ProgramResult;
use solana_program::msg;
use solana_program::program_error::ProgramError;

use crate::error::MplAssetError;
use crate::utils::DataBlob;
use crate::{state::Key, utils::DataBlob};

#[repr(C)]
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug)]
pub struct PluginHeader {
pub version: u8, // 1
pub key: Key,
pub plugin_map_offset: usize, // 8
}

impl PluginHeader {
pub fn load(account: &AccountInfo, offset: usize) -> Result<Self, ProgramError> {
let mut bytes: &[u8] = &(*account.data).borrow()[offset..];
PluginHeader::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 PluginHeader {
fn get_initial_size() -> usize {
1 + 8
Expand All @@ -39,4 +17,8 @@ impl DataBlob for PluginHeader {
fn get_size(&self) -> usize {
1 + 8
}

fn key() -> Key {
Key::PluginHeader
}
}
33 changes: 32 additions & 1 deletion programs/mpl-asset/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
pub trait DataBlob {
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 = Key::from_u8((*account.data).borrow()[offset])
.ok_or(MplAssetError::DeserializationError)?;

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()
})
}
}

0 comments on commit 3ccc714

Please sign in to comment.