Skip to content

Commit

Permalink
Stubbing out lifecycle events.
Browse files Browse the repository at this point in the history
  • Loading branch information
blockiosaurus committed Feb 21, 2024
1 parent fc002b5 commit 3479261
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 124 deletions.
4 changes: 2 additions & 2 deletions clients/js/src/generated/types/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
struct,
} from '@metaplex-foundation/umi/serializers';

export type Collection = { collectionAddress: PublicKey; required: boolean };
export type Collection = { collectionAddress: PublicKey; managed: boolean };

export type CollectionArgs = Collection;

Expand All @@ -25,7 +25,7 @@ export function getCollectionSerializer(): Serializer<
return struct<Collection>(
[
['collectionAddress', publicKeySerializer()],
['required', bool()],
['managed', bool()],
],
{ description: 'Collection' }
) as Serializer<CollectionArgs, Collection>;
Expand Down
19 changes: 17 additions & 2 deletions clients/js/src/generated/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,28 @@ import {
unit,
} from '@metaplex-foundation/umi/serializers';
import {
Collection,
CollectionArgs,
Delegate,
DelegateArgs,
Royalties,
RoyaltiesArgs,
getCollectionSerializer,
getDelegateSerializer,
getRoyaltiesSerializer,
} from '.';

export type Plugin =
| { __kind: 'Reserved' }
| { __kind: 'Royalties'; fields: [Royalties] }
| { __kind: 'Delegate'; fields: [Delegate] };
| { __kind: 'Delegate'; fields: [Delegate] }
| { __kind: 'Collection'; fields: [Collection] };

export type PluginArgs =
| { __kind: 'Reserved' }
| { __kind: 'Royalties'; fields: [RoyaltiesArgs] }
| { __kind: 'Delegate'; fields: [DelegateArgs] };
| { __kind: 'Delegate'; fields: [DelegateArgs] }
| { __kind: 'Collection'; fields: [CollectionArgs] };

export function getPluginSerializer(): Serializer<PluginArgs, Plugin> {
return dataEnum<Plugin>(
Expand All @@ -50,6 +55,12 @@ export function getPluginSerializer(): Serializer<PluginArgs, Plugin> {
['fields', tuple([getDelegateSerializer()])],
]),
],
[
'Collection',
struct<GetDataEnumKindContent<Plugin, 'Collection'>>([
['fields', tuple([getCollectionSerializer()])],
]),
],
],
{ description: 'Plugin' }
) as Serializer<PluginArgs, Plugin>;
Expand All @@ -67,6 +78,10 @@ export function plugin(
kind: 'Delegate',
data: GetDataEnumKindContent<PluginArgs, 'Delegate'>['fields']
): GetDataEnumKind<PluginArgs, 'Delegate'>;
export function plugin(
kind: 'Collection',
data: GetDataEnumKindContent<PluginArgs, 'Collection'>['fields']
): GetDataEnumKind<PluginArgs, 'Collection'>;
export function plugin<K extends PluginArgs['__kind']>(
kind: K,
data?: any
Expand Down
1 change: 1 addition & 0 deletions clients/js/src/generated/types/pluginType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum PluginType {
Reserved,
Royalties,
Delegate,
Collection,
}

export type PluginTypeArgs = PluginType;
Expand Down
2 changes: 1 addition & 1 deletion clients/rust/src/generated/types/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ pub struct Collection {
serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
)]
pub collection_address: Pubkey,
pub required: bool,
pub managed: bool,
}
2 changes: 2 additions & 0 deletions clients/rust/src/generated/types/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! [https://github.com/metaplex-foundation/kinobi]
//!
use crate::generated::types::Collection;
use crate::generated::types::Delegate;
use crate::generated::types::Royalties;
use borsh::BorshDeserialize;
Expand All @@ -16,4 +17,5 @@ pub enum Plugin {
Reserved,
Royalties(Royalties),
Delegate(Delegate),
Collection(Collection),
}
1 change: 1 addition & 0 deletions clients/rust/src/generated/types/plugin_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ pub enum PluginType {
Reserved,
Royalties,
Delegate,
Collection,
}
13 changes: 12 additions & 1 deletion idls/mpl_asset_program.json
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@
"type": "publicKey"
},
{
"name": "required",
"name": "managed",
"type": "bool"
}
]
Expand Down Expand Up @@ -1105,6 +1105,14 @@
"defined": "Delegate"
}
]
},
{
"name": "Collection",
"fields": [
{
"defined": "Collection"
}
]
}
]
}
Expand All @@ -1122,6 +1130,9 @@
},
{
"name": "Delegate"
},
{
"name": "Collection"
}
]
}
Expand Down
4 changes: 2 additions & 2 deletions programs/mpl-asset/src/plugins/collection.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::pubkey::Pubkey;

#[derive(Clone, BorshSerialize, BorshDeserialize, Debug)]
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, Eq, PartialEq)]
pub struct Collection {
collection_address: Pubkey,
required: bool,
managed: bool,
}
140 changes: 57 additions & 83 deletions programs/mpl-asset/src/plugins/lifecycle.rs
Original file line number Diff line number Diff line change
@@ -1,109 +1,83 @@
use solana_program::{entrypoint::ProgramResult, program_error::ProgramError, slot_history::Check};
use solana_program::program_error::ProgramError;

use super::{Plugin, PluginType};

// Lifecycle permissions
pub enum CheckResult {
CanApprove,
CanReject,
None,
}

pub enum ValidationResult {
Approved,
Rejected,
Pass,
}

// Lifecycle validations
pub fn check_create(plugin_type: PluginType) -> Result<CheckResult, ProgramError> {
match plugin_type {
_ => Ok(CheckResult::No),
}
}

pub fn check_update(plugin_type: PluginType) -> Result<CheckResult, ProgramError> {
match plugin_type {
_ => Ok(CheckResult::No),
}
}

pub fn check_burn(plugin_type: PluginType) -> Result<CheckResult, ProgramError> {
match plugin_type {
PluginType::Delegate => {
// If the delegate plugin authority is the signer, then approve the burn.
Ok(CheckResult::Yes)
// Otherwise pass.
pub trait CheckLifecyclePermission {
fn check_create(plugin_type: PluginType) -> Result<CheckResult, ProgramError> {
#[allow(clippy::match_single_binding)]
match plugin_type {
_ => Ok(CheckResult::None),
}
_ => Ok(CheckResult::No),
}
}

pub fn check_transfer(plugin_type: PluginType) -> Result<CheckResult, ProgramError> {
match plugin_type {
PluginType::Delegate => {
// If the delegate plugin authority is the signer, then approve the burn.
Ok(CheckResult::Yes)
// Otherwise pass.
fn check_update(plugin_type: PluginType) -> Result<CheckResult, ProgramError> {
#[allow(clippy::match_single_binding)]
match plugin_type {
_ => Ok(CheckResult::None),
}
PluginType::Royalties => {
// If the auth rules passes, pass.
Ok(CheckResult::Yes)
// Otherwise fail.
}
_ => Ok(CheckResult::No),
}
}

pub fn check_compress(plugin_type: PluginType) -> Result<CheckResult, ProgramError> {
match plugin_type {
_ => Ok(CheckResult::No),
fn check_burn(plugin_type: PluginType) -> Result<CheckResult, ProgramError> {
match plugin_type {
PluginType::Delegate => {
// If the delegate plugin authority is the signer, then approve the burn.
Ok(CheckResult::CanApprove)
// Otherwise pass.
}
_ => Ok(CheckResult::None),
}
}
}

pub fn check_decompress(plugin_type: PluginType) -> Result<CheckResult, ProgramError> {
match plugin_type {
_ => Ok(CheckResult::No),
fn check_transfer(plugin_type: PluginType) -> Result<CheckResult, ProgramError> {
match plugin_type {
PluginType::Delegate => {
// If the delegate plugin authority is the signer, then approve the burn.
Ok(CheckResult::CanApprove)
// Otherwise pass.
}
PluginType::Royalties => {
// If the auth rules passes, pass.
Ok(CheckResult::CanReject)
// Otherwise fail.
}
_ => Ok(CheckResult::None),
}
}
}

// Lifecycle hooks
pub fn on_create(plugin: Plugin) -> Result<ValidationResult, ProgramError> {
match plugin {
_ => Ok(ValidationResult::Pass),
}
}
pub fn on_update(plugin: Plugin) -> Result<ValidationResult, ProgramError> {
match plugin {
_ => Ok(ValidationResult::Pass),
}
}
pub fn on_burn(plugin: Plugin) -> Result<ValidationResult, ProgramError> {
match plugin {
Plugin::Delegate(_) => {
// If the delegate plugin authority is the signer, then approve the burn.
Ok(ValidationResult::Pass)
// Otherwise fail.
fn check_compress(plugin_type: PluginType) -> Result<CheckResult, ProgramError> {
#[allow(clippy::match_single_binding)]
match plugin_type {
_ => Ok(CheckResult::None),
}
_ => Ok(ValidationResult::Pass),
}
}
pub fn on_transfer(plugin: Plugin) -> Result<ValidationResult, ProgramError> {
match plugin {
Plugin::Delegate(_) => {
// If the delegate plugin authority is the signer, then approve the Transfer.
Ok(ValidationResult::Pass)
// Otherwise fail.

fn check_decompress(plugin_type: PluginType) -> Result<CheckResult, ProgramError> {
#[allow(clippy::match_single_binding)]
match plugin_type {
_ => Ok(CheckResult::None),
}
_ => Ok(ValidationResult::Pass),
}
}
pub fn on_compress(plugin: Plugin) -> Result<ValidationResult, ProgramError> {
match plugin {
_ => Ok(ValidationResult::Pass),
}

// Lifecycle validations
pub enum ValidationResult {
Approved,
Rejected,
Pass,
}
pub fn on_decompress(plugin: Plugin) -> Result<ValidationResult, ProgramError> {
match plugin {
_ => Ok(ValidationResult::Pass),
}

pub trait ValidateLifecycle {
fn validate_create(plugin: &Plugin) -> Result<ValidationResult, ProgramError>;
fn validate_update(plugin: &Plugin) -> Result<ValidationResult, ProgramError>;
fn validate_burn(plugin: &Plugin) -> Result<ValidationResult, ProgramError>;
fn validate_transfer(plugin: &Plugin) -> Result<ValidationResult, ProgramError>;
fn validate_compress(plugin: &Plugin) -> Result<ValidationResult, ProgramError>;
fn validate_decompress(plugin: &Plugin) -> Result<ValidationResult, ProgramError>;
}
10 changes: 8 additions & 2 deletions programs/mpl-asset/src/plugins/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
mod collection;
mod delegate;
// mod lifecycle;
mod lifecycle;
mod plugin_header;
mod plugin_registry;
mod royalties;
mod utils;

pub use collection::*;
pub use delegate::*;
pub use lifecycle::*;
pub use plugin_header::*;
// pub use lifecycle::*;
pub use plugin_registry::*;
pub use royalties::*;

Expand All @@ -31,6 +31,7 @@ pub enum Plugin {
Reserved,
Royalties(Royalties),
Delegate(Delegate),
Collection(Collection),
}

impl Plugin {
Expand All @@ -39,16 +40,20 @@ impl Plugin {
Plugin::Reserved => Err(MplAssetError::InvalidPlugin.into()),
Plugin::Royalties(_) => Ok(Authority::UpdateAuthority),
Plugin::Delegate(_) => Ok(Authority::Owner),
Plugin::Collection(_) => Ok(Authority::UpdateAuthority),
}
}
}

impl CheckLifecyclePermission for Plugin {}

#[repr(u16)]
#[derive(Clone, Copy, Debug, BorshSerialize, BorshDeserialize, Eq, PartialEq)]
pub enum PluginType {
Reserved,
Royalties,
Delegate,
Collection,
}

impl DataBlob for PluginType {
Expand All @@ -67,6 +72,7 @@ impl From<&Plugin> for PluginType {
Plugin::Reserved => PluginType::Reserved,
Plugin::Royalties(_) => PluginType::Royalties,
Plugin::Delegate(_) => PluginType::Delegate,
Plugin::Collection(_) => PluginType::Collection,
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions programs/mpl-asset/src/state/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use solana_program::{

use crate::{
error::MplAssetError,
plugins::{CheckLifecyclePermission, CheckResult, PluginType},
state::{Compressible, CompressionProof, DataBlob, HashedAsset, Key, SolanaAccount},
};

Expand Down Expand Up @@ -77,3 +78,13 @@ impl From<CompressionProof> for Asset {
}
}
}

impl CheckLifecyclePermission for Asset {
fn check_transfer(_: PluginType) -> Result<CheckResult, ProgramError> {
Ok(CheckResult::CanApprove)
}

fn check_update(_: PluginType) -> Result<CheckResult, ProgramError> {
Ok(CheckResult::CanApprove)
}
}
Loading

0 comments on commit 3479261

Please sign in to comment.