Skip to content

Commit

Permalink
Making fetch_plugin generic for program.
Browse files Browse the repository at this point in the history
  • Loading branch information
blockiosaurus committed Mar 1, 2024
1 parent 60c94ed commit bbf72e6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
31 changes: 24 additions & 7 deletions programs/mpl-core/src/plugins/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ pub fn assert_plugins_initialized(account: &AccountInfo) -> ProgramResult {
}

/// Fetch the plugin from the registry.
pub fn fetch_plugin(
pub fn fetch_plugin<T: DataBlob + SolanaAccount, U: BorshDeserialize>(
account: &AccountInfo,
plugin_type: PluginType,
) -> Result<(Vec<Authority>, Plugin, usize), ProgramError> {
let asset = Asset::load(account, 0)?;
) -> Result<(Vec<Authority>, U, usize), ProgramError> {
let asset = T::load(account, 0)?;

let header = PluginHeader::load(account, asset.get_size())?;
let PluginRegistry { registry, .. } =
Expand All @@ -98,19 +98,30 @@ pub fn fetch_plugin(
// Deserialize the plugin.
let plugin = Plugin::deserialize(&mut &(*account.data).borrow()[registry_record.offset..])?;

if PluginType::from(&plugin) != plugin_type {
return Err(MplCoreError::PluginNotFound.into());
}

let inner = U::deserialize(
&mut &(*account.data).borrow()[registry_record
.offset
.checked_add(1)
.ok_or(MplCoreError::NumericalOverflow)?..],
)?;

// Return the plugin and its authorities.
Ok((
registry_record.authorities.clone(),
plugin,
inner,
registry_record.offset,
))
}

/// Fetch the collection plugin from the registry.
pub fn fetch_collection_plugin(
pub fn fetch_collection_plugin<T: BorshDeserialize>(
account: &AccountInfo,
plugin_type: PluginType,
) -> Result<(Vec<Authority>, Plugin, usize), ProgramError> {
) -> Result<(Vec<Authority>, T, usize), ProgramError> {
let collection = CollectionData::load(account, 0)?;

let header = PluginHeader::load(account, collection.get_size())?;
Expand All @@ -126,10 +137,16 @@ pub fn fetch_collection_plugin(
// Deserialize the plugin.
let plugin = Plugin::deserialize(&mut &(*account.data).borrow()[registry_record.offset..])?;

if PluginType::from(&plugin) != plugin_type {
return Err(MplCoreError::PluginNotFound.into());
}

let inner = T::deserialize(&mut &(*account.data).borrow()[registry_record.offset..])?;

// Return the plugin and its authorities.
Ok((
registry_record.authorities.clone(),
plugin,
inner,
registry_record.offset,
))
}
Expand Down
8 changes: 5 additions & 3 deletions programs/mpl-core/src/state/update_authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
BurnAccounts, CompressAccounts, CreateAccounts, DecompressAccounts, TransferAccounts,
UpdateAccounts,
},
plugins::{fetch_collection_plugin, CheckResult, PluginType, ValidationResult},
plugins::{fetch_plugin, CheckResult, PluginType, UpdateDelegate, ValidationResult},
processor::CreateArgs,
state::{Authority, CollectionData, SolanaAccount},
utils::assert_collection_authority,
Expand Down Expand Up @@ -68,8 +68,10 @@ impl UpdateAuthority {
None => ctx.payer,
};

let maybe_update_delegate =
fetch_collection_plugin(collection_info, PluginType::UpdateDelegate);
let maybe_update_delegate = fetch_plugin::<CollectionData, UpdateDelegate>(
collection_info,
PluginType::UpdateDelegate,
);

if let Ok((mut authorities, _, _)) = maybe_update_delegate {
authorities.push(Authority::UpdateAuthority);
Expand Down

0 comments on commit bbf72e6

Please sign in to comment.