Skip to content

Commit

Permalink
Decode Rule Set V1 (#276)
Browse files Browse the repository at this point in the history
* decode rule set v1; update to tm 1.11

* remove panics
  • Loading branch information
samuelvanderwaal authored May 7, 2023
1 parent b392094 commit ea566e4
Show file tree
Hide file tree
Showing 19 changed files with 204 additions and 66 deletions.
14 changes: 8 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ indexmap = { version = "1.9.3", features = ["serde"] }
indicatif = { version = "0.16.2", features = ["rayon"] }
lazy_static = "1.4.0"
log = "0.4.17"
metaboss_lib = "0.10.1"
mpl-token-metadata = { version = "1.10.0", features = [ "no-entrypoint", "serde-feature"] }
metaboss_lib = "0.11"
mpl-token-metadata = { version = "1.11.0", features = [ "no-entrypoint", "serde-feature"] }
num_cpus = "1.15.0"
phf = { version = "0.10", features = ["macros"] }
ratelimit = "0.4.4"
Expand Down
18 changes: 11 additions & 7 deletions src/collections/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,20 @@ pub fn set_and_verify_nft_collection(
let mut instructions = vec![];

// Token Metadata UpdateArgs enum.
let mut update_args = UpdateArgs::default();
let mut update_args = UpdateArgs::default_v1();

// We set the collection key with update, but can only verify with Verify.
let UpdateArgs::V1 {
if let UpdateArgs::V1 {
ref mut collection, ..
} = update_args;
*collection = CollectionToggle::Set(MdCollection {
key: collection_pubkey,
verified: false,
});
} = update_args
{
*collection = CollectionToggle::Set(MdCollection {
key: collection_pubkey,
verified: false,
});
} else {
return Err(anyhow!("UpdateArgs enum is not V1!".to_string()));
}

// Metaboss UpdateAssetArgs enum.
let update_args = UpdateAssetArgs::V1 {
Expand Down
21 changes: 14 additions & 7 deletions src/collections/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,22 @@ fn set_and_verify(

// Add update instruction to set the collection.
// Token Metadata UpdateArgs enum.
let mut update_args = UpdateArgs::default();
let mut update_args = UpdateArgs::default_v1();

let UpdateArgs::V1 {
if let UpdateArgs::V1 {
ref mut collection, ..
} = update_args;
*collection = CollectionToggle::Set(Collection {
key: collection_mint_pubkey,
verified: false,
});
} = update_args
{
*collection = CollectionToggle::Set(Collection {
key: collection_mint_pubkey,
verified: false,
});
} else {
return Err(MigrateError::MigrationFailed(
nft_mint,
"UpdateArgs enum is not V1!".to_string(),
));
}

let update_ix = update_asset_ix(
&client,
Expand Down
3 changes: 3 additions & 0 deletions src/decode.rs → src/decode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ use crate::errors::*;
use crate::limiter::create_default_rate_limiter;
use crate::parse::is_only_one_option;

mod rule_set;
pub use rule_set::*;

#[derive(Debug, Serialize)]
pub struct JSONCreator {
pub address: String,
Expand Down
17 changes: 17 additions & 0 deletions src/decode/rule_set.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use metaboss_lib::decode::decode_rule_set;

use super::*;

pub fn process_decode_rule_set(
client: &RpcClient,
rule_set_pubkey: Pubkey,
revision: Option<usize>,
) -> AnyResult<()> {
let rule_set = decode_rule_set(client, &rule_set_pubkey, revision)?;

let f = File::create(format!("{rule_set_pubkey}_rule_set.json"))?;

serde_json::to_writer_pretty(f, &rule_set)?;

Ok(())
}
13 changes: 13 additions & 0 deletions src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,19 @@ pub enum DecodeSubcommands {
#[structopt(short, long)]
marker_num: Option<u64>,
},
/// Decode a token-auth-rules rule set
RuleSet {
/// Rule Set pubkey
rule_set: Pubkey,

/// Rule Set revision, defaults to latest
#[structopt(short = "R", long)]
revision: Option<usize>,
},
Pubkey {
/// Pubkey
pubkey: String,
},
}

#[derive(Debug, StructOpt)]
Expand Down
19 changes: 18 additions & 1 deletion src/process_subcommands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::data::UpdateNftData;
use crate::decode::{
decode_edition_marker, decode_master_edition, decode_metadata, decode_metadata_from_mint,
decode_mint_account, decode_print_edition, decode_token_account,
process_decode_bpf_loader_upgradable_state,
process_decode_bpf_loader_upgradable_state, process_decode_rule_set,
};
use crate::derive::{
get_cmv2_pda, get_edition_marker_pda, get_edition_pda, get_generic_pda, get_metadata_pda,
Expand Down Expand Up @@ -402,6 +402,23 @@ pub fn process_decode(client: &RpcClient, commands: DecodeSubcommands) -> Result
edition_num,
marker_num,
} => decode_edition_marker(client, &account, edition_num, marker_num)?,
DecodeSubcommands::RuleSet { rule_set, revision } => {
process_decode_rule_set(client, rule_set, revision)?
}
DecodeSubcommands::Pubkey { pubkey } => {
let key: Vec<u8> = pubkey
.trim_start_matches('[')
.trim_end_matches(']')
.split(',')
.map(|c| {
c.parse()
.unwrap_or_else(|_| panic!("failed to parse {}", c))
})
.collect();

let array: [u8; 32] = key.try_into().map_err(|_| anyhow!("Invalid pubkey"))?;
println!("{:?}", Pubkey::new_from_array(array));
}
}
Ok(())
}
Expand Down
12 changes: 9 additions & 3 deletions src/update/creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,18 @@ pub async fn update_creator(args: UpdateCreatorArgs) -> Result<Signature, Action
}

// Token Metadata UpdateArgs enum.
let mut update_args = UpdateArgs::default();
let mut update_args = UpdateArgs::default_v1();

// Update the creators on the data struct.
current_md.data.creators = Some(new_creators);
let UpdateArgs::V1 { ref mut data, .. } = update_args;
*data = Some(current_md.data);
if let UpdateArgs::V1 { ref mut data, .. } = update_args {
*data = Some(current_md.data);
} else {
return Err(ActionError::ActionFailed(
args.mint_account,
"UpdateArgs enum is not V1!".to_string(),
));
}

// Metaboss UpdateAssetArgs enum.
let update_args = UpdateAssetArgs::V1 {
Expand Down
12 changes: 9 additions & 3 deletions src/update/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ pub async fn update_data(args: UpdateDataArgs) -> Result<Signature, ActionError>
// Add metadata delegate record here later.

// Token Metadata UpdateArgs enum.
let mut update_args = UpdateArgs::default();
let mut update_args = UpdateArgs::default_v1();

let UpdateArgs::V1 { ref mut data, .. } = update_args;
*data = Some(args.new_data);
if let UpdateArgs::V1 { ref mut data, .. } = update_args {
*data = Some(args.new_data);
} else {
return Err(ActionError::ActionFailed(
args.mint_account,
"UpdateArgs enum is not V1!".to_string(),
));
}

// Metaboss UpdateAssetArgs enum.
let update_args = UpdateAssetArgs::V1 {
Expand Down
15 changes: 11 additions & 4 deletions src/update/immutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@ pub async fn set_immutable(args: SetImmutableArgs) -> Result<Signature, ActionEr
// Add metadata delegate record here later.

// Token Metadata UpdateArgs enum.
let mut update_args = UpdateArgs::default();
let mut update_args = UpdateArgs::default_v1();

let UpdateArgs::V1 {
if let UpdateArgs::V1 {
ref mut is_mutable, ..
} = update_args;
*is_mutable = Some(false);
} = update_args
{
*is_mutable = Some(false);
} else {
return Err(ActionError::ActionFailed(
args.mint_account,
"UpdateArgs enum is not V1!".to_string(),
));
}

// Metaboss UpdateAssetArgs enum.
let update_args = UpdateAssetArgs::V1 {
Expand Down
12 changes: 9 additions & 3 deletions src/update/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ pub async fn update_name(args: UpdateNameArgs) -> Result<Signature, ActionError>
.map_err(|e| ActionError::ActionFailed(args.mint_account.to_string(), e.to_string()))?;

// Token Metadata UpdateArgs enum.
let mut update_args = UpdateArgs::default();
let mut update_args = UpdateArgs::default_v1();

// Update the name on the data struct.
current_md.data.name = args.new_name.clone();
let UpdateArgs::V1 { ref mut data, .. } = update_args;
*data = Some(current_md.data);
if let UpdateArgs::V1 { ref mut data, .. } = update_args {
*data = Some(current_md.data);
} else {
return Err(ActionError::ActionFailed(
args.mint_account,
"UpdateArgs enum is not V1!".to_string(),
));
}

// Metaboss UpdateAssetArgs enum.
let update_args = UpdateAssetArgs::V1 {
Expand Down
15 changes: 11 additions & 4 deletions src/update/primary_sale_happened.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ pub async fn set_primary_sale_happened(
args: SetPrimarySaleHappenedArgs,
) -> Result<Signature, ActionError> {
// Token Metadata UpdateArgs enum.
let mut update_args = UpdateArgs::default();
let mut update_args = UpdateArgs::default_v1();

let UpdateArgs::V1 {
if let UpdateArgs::V1 {
ref mut primary_sale_happened,
..
} = update_args;
*primary_sale_happened = Some(true);
} = update_args
{
*primary_sale_happened = Some(true);
} else {
return Err(ActionError::ActionFailed(
args.mint_account,
"UpdateArgs enum is not V1!".to_string(),
));
}

// Metaboss UpdateAssetArgs enum.
let update_args = UpdateAssetArgs::V1 {
Expand Down
32 changes: 22 additions & 10 deletions src/update/rule_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,20 @@ pub async fn update_rule_set(args: UpdateRuleSetArgs) -> Result<Signature, Actio
// Add metadata delegate record here later.

// Token Metadata UpdateArgs enum.
let mut update_args = UpdateArgs::default();
let mut update_args = UpdateArgs::default_v1();

// Update the rule set.
let UpdateArgs::V1 {
if let UpdateArgs::V1 {
ref mut rule_set, ..
} = update_args;

*rule_set = RuleSetToggle::Set(new_rule_set);
} = update_args
{
*rule_set = RuleSetToggle::Set(new_rule_set);
} else {
return Err(ActionError::ActionFailed(
args.mint_account,
"UpdateArgs enum is not V1!".to_string(),
));
}

// Metaboss UpdateAssetArgs enum.
let update_args = UpdateAssetArgs::V1 {
Expand All @@ -71,14 +77,20 @@ pub async fn clear_rule_set(args: ClearRuleSetArgs) -> Result<Signature, ActionE
// Add metadata delegate record here later.

// Token Metadata UpdateArgs enum.
let mut update_args = UpdateArgs::default();
let mut update_args = UpdateArgs::default_v1();

// Update the rule set.
let UpdateArgs::V1 {
if let UpdateArgs::V1 {
ref mut rule_set, ..
} = update_args;

*rule_set = RuleSetToggle::Clear;
} = update_args
{
*rule_set = RuleSetToggle::Clear;
} else {
return Err(ActionError::ActionFailed(
args.mint_account,
"UpdateArgs enum is not V1!".to_string(),
));
}

// Metaboss UpdateAssetArgs enum.
let update_args = UpdateAssetArgs::V1 {
Expand Down
12 changes: 9 additions & 3 deletions src/update/seller_fee_basis_points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ pub async fn update_sfbp(args: UpdateSellerFeeBasisPointsArgs) -> Result<Signatu
current_md.data.seller_fee_basis_points = args.new_sfbp;

// Token Metadata UpdateArgs enum.
let mut update_args = UpdateArgs::default();
let mut update_args = UpdateArgs::default_v1();

// Update the sfbp on the data struct.
let UpdateArgs::V1 { ref mut data, .. } = update_args;
*data = Some(current_md.data);
if let UpdateArgs::V1 { ref mut data, .. } = update_args {
*data = Some(current_md.data);
} else {
return Err(ActionError::ActionFailed(
args.mint_account,
"UpdateArgs enum is not V1!".to_string(),
));
}

// Metaboss UpdateAssetArgs enum.
let update_args = UpdateAssetArgs::V1 {
Expand Down
Loading

0 comments on commit ea566e4

Please sign in to comment.