generated from metaplex-foundation/solana-project-template
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b1a12a
commit 2598ac4
Showing
3 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
pub mod plugins; | ||
|
||
pub use plugins::*; | ||
|
||
use crate::accounts::{Asset, CollectionData}; | ||
|
||
impl Asset { | ||
/// The base length of the asset account with an empty name and uri. | ||
pub const BASE_LENGTH: usize = 1 + 32 + 33 + 4 + 4; | ||
|
||
pub fn get_size(&self) -> usize { | ||
Asset::BASE_LENGTH + self.name.len() + self.uri.len() | ||
} | ||
} | ||
|
||
impl CollectionData { | ||
/// The base length of the collection account with an empty name and uri. | ||
pub const BASE_LENGTH: usize = 1 + 32 + 4 + 4 + 4 + 4; | ||
|
||
fn get_size(&self) -> usize { | ||
Self::BASE_LENGTH + self.name.len() + self.uri.len() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use std::{io::Error, io::ErrorKind}; | ||
|
||
use borsh::BorshDeserialize; | ||
|
||
use crate::{ | ||
accounts::{Asset, CollectionData, PluginHeader, PluginRegistry}, | ||
errors::MplCoreError, | ||
types::{Authority, Plugin, PluginType, RegistryRecord}, | ||
}; | ||
|
||
/// Fetch the plugin from the registry. | ||
pub fn fetch_plugin( | ||
account: &[u8], | ||
plugin_type: PluginType, | ||
) -> Result<(Vec<Authority>, Plugin, u64), std::io::Error> { | ||
let asset = Asset::from_bytes(account)?; | ||
|
||
let header = PluginHeader::from_bytes(&account[asset.get_size()..])?; | ||
let PluginRegistry { registry, .. } = | ||
PluginRegistry::from_bytes(&account[(header.plugin_registry_offset as usize)..])?; | ||
|
||
// Find the plugin in the registry. | ||
let registry_record = registry | ||
.iter() | ||
.find(|record| record.plugin_type == plugin_type) | ||
.ok_or(Error::new( | ||
ErrorKind::Other, | ||
MplCoreError::PluginNotFound.to_string(), | ||
))?; | ||
|
||
// Deserialize the plugin. | ||
let plugin = Plugin::deserialize(&mut &account[(registry_record.offset as usize)..])?; | ||
|
||
// Return the plugin and its authorities. | ||
Ok(( | ||
registry_record.authorities.clone(), | ||
plugin, | ||
registry_record.offset, | ||
)) | ||
} | ||
|
||
/// Fetch the collection plugin from the registry. | ||
pub fn fetch_collection_plugin( | ||
account: &[u8], | ||
plugin_type: PluginType, | ||
) -> Result<(Vec<Authority>, Plugin, u64), std::io::Error> { | ||
let collection = CollectionData::from_bytes(account)?; | ||
|
||
let header = PluginHeader::from_bytes(&account[collection.get_size()..])?; | ||
let PluginRegistry { registry, .. } = | ||
PluginRegistry::from_bytes(&account[(header.plugin_registry_offset as usize)..])?; | ||
|
||
// Find the plugin in the registry. | ||
let registry_record = registry | ||
.iter() | ||
.find(|record| record.plugin_type == plugin_type) | ||
.ok_or(Error::new( | ||
ErrorKind::Other, | ||
MplCoreError::PluginNotFound.to_string(), | ||
))?; | ||
|
||
// Deserialize the plugin. | ||
let plugin = Plugin::deserialize(&mut &account[(registry_record.offset as usize)..])?; | ||
|
||
// Return the plugin and its authorities. | ||
Ok(( | ||
registry_record.authorities.clone(), | ||
plugin, | ||
registry_record.offset, | ||
)) | ||
} | ||
|
||
/// Fetch the plugin registry. | ||
pub fn fetch_plugins(account: &[u8]) -> Result<Vec<RegistryRecord>, std::io::Error> { | ||
let asset = Asset::from_bytes(account)?; | ||
|
||
let header = PluginHeader::from_bytes(&account[asset.get_size()..])?; | ||
let PluginRegistry { registry, .. } = | ||
PluginRegistry::from_bytes(&account[(header.plugin_registry_offset as usize)..])?; | ||
|
||
Ok(registry) | ||
} | ||
|
||
/// Create plugin header and registry if it doesn't exist | ||
pub fn list_plugins(account: &[u8]) -> Result<Vec<PluginType>, std::io::Error> { | ||
let asset = Asset::from_bytes(account)?; | ||
|
||
let header = PluginHeader::from_bytes(&account[asset.get_size()..])?; | ||
let PluginRegistry { registry, .. } = | ||
PluginRegistry::from_bytes(&account[(header.plugin_registry_offset as usize)..])?; | ||
|
||
Ok(registry | ||
.iter() | ||
.map(|registry_record| registry_record.plugin_type.clone()) | ||
.collect()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
mod generated; | ||
mod hooked; | ||
|
||
pub use generated::programs::MPL_CORE_ID as ID; | ||
pub use generated::*; | ||
pub use hooked::*; |