diff --git a/Cargo.lock b/Cargo.lock index d541b1d1..cfa203ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5069,6 +5069,7 @@ version = "0.0.0" dependencies = [ "anyhow", "bs58 0.4.0", + "chrono", "flow-lib", "rhai", "rhai-rand", diff --git a/crates/cmds-pdg/src/gen_metaplex_attrs.rs b/crates/cmds-pdg/src/gen_metaplex_attrs.rs index 08f66d34..ce9f9e49 100644 --- a/crates/cmds-pdg/src/gen_metaplex_attrs.rs +++ b/crates/cmds-pdg/src/gen_metaplex_attrs.rs @@ -6,6 +6,7 @@ use flow_lib::{ Context, }; use pdg_common::nft_metadata::{ + generate::{Effect, EffectsList}, metaplex::{MetaplexAttribute, NftTraits}, RenderParams, }; @@ -25,6 +26,7 @@ flow_lib::submit!(CommandDescription::new(NAME, |_| build())); #[derive(Deserialize, Debug)] struct Input { attributes: RenderParams, + effects: Vec, } #[derive(Serialize, Debug)] @@ -33,7 +35,8 @@ struct Output { } async fn run(_: Context, input: Input) -> Result { + let traits = NftTraits::new(&input.attributes, &EffectsList::from(input.effects)); Ok(Output { - attributes: NftTraits::new(&input.attributes).gen_metaplex_attrs()?, + attributes: traits.gen_metaplex_attrs()?, }) } diff --git a/crates/pdg-common/src/nft_metadata/metaplex.rs b/crates/pdg-common/src/nft_metadata/metaplex.rs index 1bdb8502..60b1321a 100644 --- a/crates/pdg-common/src/nft_metadata/metaplex.rs +++ b/crates/pdg-common/src/nft_metadata/metaplex.rs @@ -1,4 +1,4 @@ -use super::{EnumExt, PropertyNotFound, RenderParams}; +use super::{generate::EffectsList, EnumExt, PropertyNotFound, RenderParams}; use serde::{Deserialize, Serialize}; use thiserror::Error as ThisError; @@ -18,12 +18,14 @@ pub fn hue_to_color_name(mut hue: f64) -> String { } /// Traits that will be included when uploading to Metaplex +#[derive(Debug, Clone, PartialEq, Eq)] pub struct NftTraits { pub body: super::BodyType, pub helmet: super::HelmetType, pub helmet_light: super::HelmetLight, pub dress_color: String, pub pose: super::Pose, + pub effects_count: usize, pub composition: super::Fx0, pub transformation: super::Fx1, pub season: super::Fx2, @@ -73,13 +75,14 @@ pub enum ParseMetaflexError { } impl NftTraits { - pub fn new(r: &RenderParams) -> Self { + pub fn new(r: &RenderParams, effects: &EffectsList) -> Self { Self { body: r.body_type, helmet: r.helmet_type, helmet_light: r.helmet_light, dress_color: hue_to_color_name(r.dress_color_hue), pose: r.pose, + effects_count: effects.effects.len(), composition: r.fx0, transformation: r.fx1, season: r.fx2, @@ -105,7 +108,7 @@ impl NftTraits { /// Read from an `attributes` array /// /// https://docs.metaplex.com/programs/token-metadata/token-standard#the-programmable-non-fungible-standard - pub fn parse_metaflex_attrs(v: &[MetaplexAttribute]) -> Result { + pub fn parse_metaplex_attrs(v: &[MetaplexAttribute]) -> Result { fn find_str<'a>( v: &'a [MetaplexAttribute], trait_type: &str, @@ -163,6 +166,7 @@ impl NftTraits { helmet_light: find_enum(v, "Helmet Light")?, dress_color: find_from_str(v, "Dress Color Hue")?, pose: find_enum(v, "Pose")?, + effects_count: find_from_str(v, "Gained Effects")?, composition: find_enum(v, "Composition")?, transformation: find_enum(v, "Transformation")?, season: find_enum(v, "Season")?, @@ -184,6 +188,7 @@ impl NftTraits { helmet_light, dress_color, pose, + effects_count, composition, transformation, season, @@ -209,6 +214,7 @@ impl NftTraits { push(&mut v, "Helmet Light", helmet_light.metaplex_name()?); push(&mut v, "Dress Color Hue", dress_color.clone()); push(&mut v, "Pose", pose.metaplex_name()?); + push(&mut v, "Gained Effects", effects_count.to_string()); push(&mut v, "Composition", composition.metaplex_name()?); push(&mut v, "Transformation", transformation.metaplex_name()?); push(&mut v, "Season", season.metaplex_name()?); @@ -256,13 +262,19 @@ mod tests { } #[test] - fn test_gen_metaflex_attrs() { + fn test_gen_metaplex_attrs() { let mut json = serde_json::from_str::(include_str!("tests/123.json")).unwrap(); let params = RenderParams::from_pdg_metadata(&mut json, true).unwrap(); - let meta = NftTraits::new(¶ms); + let effects = EffectsList::from(params.clone()); + let meta = NftTraits::new(¶ms, &effects); let attrs = meta.gen_metaplex_attrs().unwrap(); let json = serde_json::to_string_pretty(&attrs).unwrap(); println!("{}", json); + let meta1 = NftTraits::parse_metaplex_attrs( + &serde_json::from_str::>(&json).unwrap(), + ) + .unwrap(); + assert_eq!(meta, meta1); } } diff --git a/crates/pdg-common/src/nft_metadata/mod.rs b/crates/pdg-common/src/nft_metadata/mod.rs index a604abb5..d91aa207 100644 --- a/crates/pdg-common/src/nft_metadata/mod.rs +++ b/crates/pdg-common/src/nft_metadata/mod.rs @@ -116,7 +116,7 @@ const DEFAULT_WEDGEATTRIBS: Attr<&[&str]> = Attr { */ /// Condensed metadata -#[derive(Serialize, Deserialize, Debug, PartialEq)] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] pub struct RenderParams { pub body_type: BodyType, pub pose: Pose, diff --git a/crates/rhai-script/src/lib.rs b/crates/rhai-script/src/lib.rs index e5a67db4..f36c767d 100644 --- a/crates/rhai-script/src/lib.rs +++ b/crates/rhai-script/src/lib.rs @@ -1,4 +1,5 @@ use anyhow::anyhow; +use chrono::Utc; use convert::{dynamic_to_value, value_to_dynamic}; use flow_lib::command::prelude::*; use rhai::{ @@ -6,7 +7,6 @@ use rhai::{ Dynamic, }; use rhai_rand::RandomPackage; -use chrono::Utc; pub mod convert;