Skip to content

Commit

Permalink
make plugins optional for create, fix rust client
Browse files Browse the repository at this point in the history
  • Loading branch information
nhanphan committed Mar 12, 2024
1 parent aca5f3c commit 7106b11
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 40 deletions.
9 changes: 6 additions & 3 deletions clients/js/src/generated/instructions/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import {
Context,
Option,
OptionOrNullable,
Pda,
PublicKey,
Signer,
Expand All @@ -18,6 +20,7 @@ import {
Serializer,
array,
mapSerializer,
option,
string,
struct,
u8,
Expand Down Expand Up @@ -62,14 +65,14 @@ export type CreateInstructionData = {
dataState: DataState;
name: string;
uri: string;
plugins: Array<PluginAuthorityPair>;
plugins: Option<Array<PluginAuthorityPair>>;
};

export type CreateInstructionDataArgs = {
dataState: DataStateArgs;
name: string;
uri: string;
plugins?: Array<PluginAuthorityPairArgs>;
plugins?: OptionOrNullable<Array<PluginAuthorityPairArgs>>;
};

export function getCreateInstructionDataSerializer(): Serializer<
Expand All @@ -83,7 +86,7 @@ export function getCreateInstructionDataSerializer(): Serializer<
['dataState', getDataStateSerializer()],
['name', string()],
['uri', string()],
['plugins', array(getPluginAuthorityPairSerializer())],
['plugins', option(array(getPluginAuthorityPairSerializer()))],
],
{ description: 'CreateInstructionData' }
),
Expand Down
16 changes: 12 additions & 4 deletions clients/js/src/generated/instructions/createCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@

import {
Context,
Option,
OptionOrNullable,
Pda,
PublicKey,
Signer,
TransactionBuilder,
none,
transactionBuilder,
} from '@metaplex-foundation/umi';
import {
Serializer,
array,
mapSerializer,
option,
string,
struct,
u8,
Expand Down Expand Up @@ -50,13 +54,13 @@ export type CreateCollectionInstructionData = {
discriminator: number;
name: string;
uri: string;
plugins: Array<PluginAuthorityPair>;
plugins: Option<Array<PluginAuthorityPair>>;
};

export type CreateCollectionInstructionDataArgs = {
name: string;
uri: string;
plugins?: Array<PluginAuthorityPairArgs>;
plugins?: OptionOrNullable<Array<PluginAuthorityPairArgs>>;
};

export function getCreateCollectionInstructionDataSerializer(): Serializer<
Expand All @@ -73,11 +77,15 @@ export function getCreateCollectionInstructionDataSerializer(): Serializer<
['discriminator', u8()],
['name', string()],
['uri', string()],
['plugins', array(getPluginAuthorityPairSerializer())],
['plugins', option(array(getPluginAuthorityPairSerializer()))],
],
{ description: 'CreateCollectionInstructionData' }
),
(value) => ({ ...value, discriminator: 1, plugins: value.plugins ?? [] })
(value) => ({
...value,
discriminator: 1,
plugins: value.plugins ?? none(),
})
) as Serializer<
CreateCollectionInstructionDataArgs,
CreateCollectionInstructionData
Expand Down
4 changes: 2 additions & 2 deletions clients/js/test/_setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const createAsset = async (umi: Umi, input: CreateAssetHelperArgs = {}) =
updateAuthority,
name: input.name || DEFAULT_ASSET.name,
uri: input.uri || DEFAULT_ASSET.uri,
plugins: input.plugins || [],
plugins: input.plugins,
collection: input.collection,
authority: input.authority,
}).sendAndConfirm(umi);
Expand Down Expand Up @@ -92,7 +92,7 @@ export const createCollection = async (
collection,
payer,
updateAuthority,
plugins: input.plugins || [],
plugins: input.plugins,
}).sendAndConfirm(umi);

return fetchCollection(umi, publicKey(collection));
Expand Down
10 changes: 5 additions & 5 deletions clients/rust/src/generated/instructions/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub struct CreateInstructionArgs {
pub data_state: DataState,
pub name: String,
pub uri: String,
pub plugins: Vec<PluginAuthorityPair>,
pub plugins: Option<Vec<PluginAuthorityPair>>,
}

/// Instruction builder for `Create`.
Expand Down Expand Up @@ -247,7 +247,7 @@ impl CreateBuilder {
self.uri = Some(uri);
self
}
/// `[optional argument, defaults to '[]']`
/// `[optional argument]`
#[inline(always)]
pub fn plugins(&mut self, plugins: Vec<PluginAuthorityPair>) -> &mut Self {
self.plugins = Some(plugins);
Expand Down Expand Up @@ -289,7 +289,7 @@ impl CreateBuilder {
data_state: self.data_state.clone().expect("data_state is not set"),
name: self.name.clone().expect("name is not set"),
uri: self.uri.clone().expect("uri is not set"),
plugins: self.plugins.clone().unwrap_or([]),
plugins: self.plugins.clone(),
};

accounts.instruction_with_remaining_accounts(args, &self.__remaining_accounts)
Expand Down Expand Up @@ -629,7 +629,7 @@ impl<'a, 'b> CreateCpiBuilder<'a, 'b> {
self.instruction.uri = Some(uri);
self
}
/// `[optional argument, defaults to '[]']`
/// `[optional argument]`
#[inline(always)]
pub fn plugins(&mut self, plugins: Vec<PluginAuthorityPair>) -> &mut Self {
self.instruction.plugins = Some(plugins);
Expand Down Expand Up @@ -684,7 +684,7 @@ impl<'a, 'b> CreateCpiBuilder<'a, 'b> {
.expect("data_state is not set"),
name: self.instruction.name.clone().expect("name is not set"),
uri: self.instruction.uri.clone().expect("uri is not set"),
plugins: self.instruction.plugins.clone().unwrap_or([]),
plugins: self.instruction.plugins.clone(),
};
let instruction = CreateCpi {
__program: self.instruction.__program,
Expand Down
10 changes: 5 additions & 5 deletions clients/rust/src/generated/instructions/create_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl CreateCollectionInstructionData {
pub struct CreateCollectionInstructionArgs {
pub name: String,
pub uri: String,
pub plugins: Vec<PluginAuthorityPair>,
pub plugins: Option<Vec<PluginAuthorityPair>>,
}

/// Instruction builder for `CreateCollection`.
Expand Down Expand Up @@ -152,7 +152,7 @@ impl CreateCollectionBuilder {
self.uri = Some(uri);
self
}
/// `[optional argument, defaults to '[]']`
/// `[optional argument]`
#[inline(always)]
pub fn plugins(&mut self, plugins: Vec<PluginAuthorityPair>) -> &mut Self {
self.plugins = Some(plugins);
Expand Down Expand Up @@ -189,7 +189,7 @@ impl CreateCollectionBuilder {
let args = CreateCollectionInstructionArgs {
name: self.name.clone().expect("name is not set"),
uri: self.uri.clone().expect("uri is not set"),
plugins: self.plugins.clone().unwrap_or([]),
plugins: self.plugins.clone(),
};

accounts.instruction_with_remaining_accounts(args, &self.__remaining_accounts)
Expand Down Expand Up @@ -403,7 +403,7 @@ impl<'a, 'b> CreateCollectionCpiBuilder<'a, 'b> {
self.instruction.uri = Some(uri);
self
}
/// `[optional argument, defaults to '[]']`
/// `[optional argument]`
#[inline(always)]
pub fn plugins(&mut self, plugins: Vec<PluginAuthorityPair>) -> &mut Self {
self.instruction.plugins = Some(plugins);
Expand Down Expand Up @@ -453,7 +453,7 @@ impl<'a, 'b> CreateCollectionCpiBuilder<'a, 'b> {
let args = CreateCollectionInstructionArgs {
name: self.instruction.name.clone().expect("name is not set"),
uri: self.instruction.uri.clone().expect("uri is not set"),
plugins: self.instruction.plugins.clone().unwrap_or([]),
plugins: self.instruction.plugins.clone(),
};
let instruction = CreateCollectionCpi {
__program: self.instruction.__program,
Expand Down
19 changes: 10 additions & 9 deletions clients/rust/src/hooked/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub use plugins::*;
use std::mem::size_of;

use crate::{
accounts::{Asset, Collection, PluginHeader, PluginRegistry},
accounts::{BaseAsset, BaseCollection, PluginHeader, PluginRegistry},
errors::MplCoreError,
types::{Key, Plugin, PluginType},
};
Expand All @@ -20,41 +20,42 @@ impl From<&Plugin> for PluginType {
Plugin::Burn(_) => PluginType::Burn,
Plugin::Transfer(_) => PluginType::Transfer,
Plugin::UpdateDelegate(_) => PluginType::UpdateDelegate,
Plugin::PermanentFreeze(_) => PluginType::PermanentFreeze,
}
}
}

impl Asset {
impl BaseAsset {
/// The base length of the asset account with an empty name and uri and no seq.
pub const BASE_LENGTH: usize = 1 + 32 + 33 + 4 + 4 + 1;
}

impl Collection {
impl BaseCollection {
/// The base length of the collection account with an empty name and uri.
pub const BASE_LENGTH: usize = 1 + 32 + 4 + 4 + 4 + 4;
}

impl DataBlob for Asset {
impl DataBlob for BaseAsset {
fn get_initial_size() -> usize {
Asset::BASE_LENGTH
BaseAsset::BASE_LENGTH
}

fn get_size(&self) -> usize {
let mut size = Asset::BASE_LENGTH + self.name.len() + self.uri.len();
let mut size = BaseAsset::BASE_LENGTH + self.name.len() + self.uri.len();
if self.seq.is_some() {
size += size_of::<u64>();
}
size
}
}

impl SolanaAccount for Asset {
impl SolanaAccount for BaseAsset {
fn key() -> Key {
Key::Asset
}
}

impl DataBlob for Collection {
impl DataBlob for BaseCollection {
fn get_initial_size() -> usize {
Self::BASE_LENGTH
}
Expand All @@ -64,7 +65,7 @@ impl DataBlob for Collection {
}
}

impl SolanaAccount for Collection {
impl SolanaAccount for BaseCollection {
fn key() -> Key {
Key::Collection
}
Expand Down
6 changes: 3 additions & 3 deletions clients/rust/src/hooked/plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use borsh::BorshDeserialize;
use solana_program::account_info::AccountInfo;

use crate::{
accounts::{Asset, PluginHeader, PluginRegistry},
accounts::{BaseAsset, PluginHeader, PluginRegistry},
errors::MplCoreError,
types::{Authority, Plugin, PluginType, RegistryRecord},
DataBlob, SolanaAccount,
Expand Down Expand Up @@ -58,7 +58,7 @@ pub fn fetch_plugin<T: DataBlob + SolanaAccount, U: BorshDeserialize>(

/// Fetch the plugin registry.
pub fn fetch_plugins(account: &[u8]) -> Result<Vec<RegistryRecord>, std::io::Error> {
let asset = Asset::from_bytes(account)?;
let asset = BaseAsset::from_bytes(account)?;

let header = PluginHeader::from_bytes(&account[asset.get_size()..])?;
let PluginRegistry { registry, .. } =
Expand All @@ -69,7 +69,7 @@ pub fn fetch_plugins(account: &[u8]) -> Result<Vec<RegistryRecord>, std::io::Err

/// 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 asset = BaseAsset::from_bytes(account)?;

let header = PluginHeader::from_bytes(&account[asset.get_size()..])?;
let PluginRegistry { registry, .. } =
Expand Down
2 changes: 1 addition & 1 deletion configs/kinobi.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ kinobi.update(
createCollection: {
arguments: {
plugins: {
defaultValue: k.arrayValueNode([])
defaultValue: k.noneValueNode()

}
}
Expand Down
12 changes: 8 additions & 4 deletions idls/mpl_core.json
Original file line number Diff line number Diff line change
Expand Up @@ -1712,8 +1712,10 @@
{
"name": "plugins",
"type": {
"vec": {
"defined": "PluginAuthorityPair"
"option": {
"vec": {
"defined": "PluginAuthorityPair"
}
}
}
}
Expand All @@ -1736,8 +1738,10 @@
{
"name": "plugins",
"type": {
"vec": {
"defined": "PluginAuthorityPair"
"option": {
"vec": {
"defined": "PluginAuthorityPair"
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions programs/mpl-core/src/processor/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub(crate) struct CreateArgs {
pub(crate) data_state: DataState,
pub(crate) name: String,
pub(crate) uri: String,
pub(crate) plugins: Vec<PluginAuthorityPair>,
pub(crate) plugins: Option<Vec<PluginAuthorityPair>>,
}

pub(crate) fn create<'a>(accounts: &'a [AccountInfo<'a>], args: CreateArgs) -> ProgramResult {
Expand Down Expand Up @@ -104,7 +104,7 @@ pub(crate) fn create<'a>(accounts: &'a [AccountInfo<'a>], args: CreateArgs) -> P
ctx.accounts.system_program,
)?;

for plugin in &args.plugins {
for plugin in &args.plugins.unwrap_or(Vec::new()) {
initialize_plugin::<Asset>(
&plugin.plugin,
&plugin.authority.unwrap_or(plugin.plugin.manager()),
Expand Down
4 changes: 2 additions & 2 deletions programs/mpl-core/src/processor/create_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
pub(crate) struct CreateCollectionArgs {
pub(crate) name: String,
pub(crate) uri: String,
pub(crate) plugins: Vec<PluginAuthorityPair>,
pub(crate) plugins: Option<Vec<PluginAuthorityPair>>,
}

pub(crate) fn create_collection<'a>(
Expand Down Expand Up @@ -83,7 +83,7 @@ pub(crate) fn create_collection<'a>(
ctx.accounts.system_program,
)?;

for plugin in args.plugins {
for plugin in args.plugins.unwrap_or(Vec::new()) {
initialize_plugin::<Collection>(
&plugin.plugin,
&plugin.authority.unwrap_or(plugin.plugin.manager()),
Expand Down

0 comments on commit 7106b11

Please sign in to comment.