Skip to content

Commit

Permalink
Adding helpers to rust client.
Browse files Browse the repository at this point in the history
  • Loading branch information
blockiosaurus committed Feb 29, 2024
1 parent 2b1a12a commit 2598ac4
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
23 changes: 23 additions & 0 deletions clients/rust/src/hooked/mod.rs
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()
}
}
96 changes: 96 additions & 0 deletions clients/rust/src/hooked/plugins.rs
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())
}
2 changes: 2 additions & 0 deletions clients/rust/src/lib.rs
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::*;

0 comments on commit 2598ac4

Please sign in to comment.