difftreelog
feat add all rmrk properties
in: master
4 files changed
pallets/rmrk-proxy/src/lib.rsdiffbeforeafterboth--- a/pallets/rmrk-proxy/src/lib.rs
+++ b/pallets/rmrk-proxy/src/lib.rs
@@ -27,8 +27,10 @@
pub use pallet::*;
pub mod misc;
+pub mod property;
use misc::*;
+pub use property::*;
#[frame_support::pallet]
pub mod pallet {
@@ -69,6 +71,7 @@
/* Unique-specific events */
CorruptedCollectionType,
NotRmrkCollection,
+ RmrkPropertyIsTooLong,
/* RMRK compatible events */
CollectionNotEmpty,
@@ -113,8 +116,8 @@
&collection,
PropertyScope::Rmrk,
[
- rmrk_property!(Metadata, metadata),
- rmrk_property!(CollectionType, CollectionType::Regular),
+ rmrk_property!(Config=T, Metadata: metadata)?,
+ rmrk_property!(Config=T, CollectionType: CollectionType::Regular)?,
].into_iter()
)?;
@@ -215,7 +218,7 @@
fn get_collection_type(collection_id: CollectionId) -> Result<CollectionType, DispatchError> {
let collection_type: CollectionType = <PalletCommon<T>>::collection_properties(collection_id)
- .get(&rmrk_property!(CollectionType))
+ .get(&rmrk_property!(Config=T, CollectionType)?)
.ok_or(<Error<T>>::NotRmrkCollection)?
.try_into()
.map_err(<Error<T>>::from)?;
pallets/rmrk-proxy/src/misc.rsdiffbeforeafterboth1use super::*;2use codec::{Encode, Decode};3use frame_support::dispatch::Vec;4use pallet_nonfungible::NonfungibleHandle;56#[macro_export]7macro_rules! rmrk_property {8 ($key:ident, $value:expr) => {9 Property {10 key: rmrk_property!(@raw $key),11 value: $value.into()12 }13 };1415 (@raw $key:ident) => {16 RmrkProperty::$key.to_key()17 };1819 ($key:ident) => {20 PropertyScope::Rmrk.apply(rmrk_property!(@raw $key)).unwrap()21 };22}2324macro_rules! impl_rmrk_value {25 ($enum_name:path, decode_error: $error:ident) => {26 impl Into<PropertyValue> for $enum_name {27 fn into(self) -> PropertyValue {28 self.encode().try_into().unwrap()29 }30 }3132 impl TryFrom<&PropertyValue> for $enum_name {33 type Error = MiscError;3435 fn try_from(value: &PropertyValue) -> Result<Self, Self::Error> {36 let mut value = value.as_slice();3738 <$enum_name>::decode(&mut value)39 .map_err(|_| MiscError::$error)40 }41 }4243 };44}4546pub enum MiscError {47 CorruptedCollectionType,48}4950impl<T: Config> From<MiscError> for Error<T> {51 fn from(error: MiscError) -> Self {52 match error {53 MiscError::CorruptedCollectionType => Self::CorruptedCollectionType,54 }55 }56}5758pub trait IntoNftCollection<T: Config> {59 fn into_nft_collection(self) -> Result<NonfungibleHandle<T>, Error<T>>;60}6162impl<T: Config> IntoNftCollection<T> for CollectionHandle<T> {63 fn into_nft_collection(self) -> Result<NonfungibleHandle<T>, Error<T>> {64 match self.mode {65 CollectionMode::NFT => Ok(NonfungibleHandle::cast(self)),66 _ => Err(<Error<T>>::NotRmrkCollection)67 }68 }69}7071pub enum RmrkProperty {72 Metadata,73 CollectionType,74}7576impl RmrkProperty {77 pub fn to_key(self) -> PropertyKey {78 let key = |str_key: &str| {79 PropertyKey::try_from(80 str_key.bytes().collect::<Vec<_>>()81 ).unwrap()82 };8384 match self {85 Self::Metadata => key("metadata"),86 Self::CollectionType => key("collection-type"),87 }88 }89}9091#[derive(Encode, Decode, PartialEq, Eq)]92pub enum CollectionType {93 Regular,94 Resource,95 Base,96}9798impl_rmrk_value!(CollectionType, decode_error: CorruptedCollectionType);1use super::*;2use codec::{Encode, Decode};3use pallet_nonfungible::NonfungibleHandle;45macro_rules! impl_rmrk_value {6 ($enum_name:path, decode_error: $error:ident) => {7 impl From<$enum_name> for PropertyValue {8 fn from(e: $enum_name) -> Self {9 e.encode().try_into().unwrap()10 }11 }1213 impl TryFrom<&PropertyValue> for $enum_name {14 type Error = MiscError;1516 fn try_from(value: &PropertyValue) -> Result<Self, Self::Error> {17 let mut value = value.as_slice();1819 <$enum_name>::decode(&mut value)20 .map_err(|_| MiscError::$error)21 }22 }2324 };25}2627pub enum MiscError {28 CorruptedCollectionType,29}3031impl<T: Config> From<MiscError> for Error<T> {32 fn from(error: MiscError) -> Self {33 match error {34 MiscError::CorruptedCollectionType => Self::CorruptedCollectionType,35 }36 }37}3839pub trait IntoNftCollection<T: Config> {40 fn into_nft_collection(self) -> Result<NonfungibleHandle<T>, Error<T>>;41}4243impl<T: Config> IntoNftCollection<T> for CollectionHandle<T> {44 fn into_nft_collection(self) -> Result<NonfungibleHandle<T>, Error<T>> {45 match self.mode {46 CollectionMode::NFT => Ok(NonfungibleHandle::cast(self)),47 _ => Err(<Error<T>>::NotRmrkCollection)48 }49 }50}5152#[derive(Encode, Decode, PartialEq, Eq)]53pub enum CollectionType {54 Regular,55 Resource,56 Base,57}5859impl_rmrk_value!(CollectionType, decode_error: CorruptedCollectionType);pallets/rmrk-proxy/src/property.rsdiffbeforeafterboth--- /dev/null
+++ b/pallets/rmrk-proxy/src/property.rs
@@ -0,0 +1,92 @@
+use super::*;
+use core::convert::AsRef;
+
+pub enum RmrkProperty {
+ Metadata,
+ CollectionType,
+ Recipient,
+ Royalty,
+ Equipped,
+ Pending,
+ ResourceCollection,
+ ResourcePriorities,
+ PendingRemoval,
+ Parts,
+ Base,
+ Src,
+ Slot,
+ License,
+ Thumb,
+ EquippedNft,
+ BaseType,
+ // // RmrkPartId(/* Id type? */)
+ EquippableList,
+ ZIndex,
+ ThemeName,
+ ThemeProperty(RmrkString),
+ ThemeInherit,
+}
+
+impl RmrkProperty {
+ pub fn to_key<T: Config>(self) -> Result<PropertyKey, Error<T>> {
+ fn get_bytes<T: AsRef<[u8]>>(container: &T) -> &[u8] {
+ container.as_ref()
+ }
+
+ macro_rules! key {
+ ($($component:expr),+) => {
+ PropertyKey::try_from([$(key!(@ &$component)),+].concat())
+ .map_err(|_| <Error<T>>::RmrkPropertyIsTooLong)
+ };
+
+ (@ $key:expr) => {
+ get_bytes($key)
+ };
+ }
+
+ match self {
+ Self::Metadata => key!("metadata"),
+ Self::CollectionType => key!("collection-type"),
+ Self::Recipient => key!("recipient"),
+ Self::Royalty => key!("royalty"),
+ Self::Equipped => key!("equipped"),
+ Self::Pending => key!("pending"),
+ Self::ResourceCollection => key!("resource-collection"),
+ Self::ResourcePriorities => key!("resource-priorities"),
+ Self::PendingRemoval => key!("pending-removal"),
+ Self::Parts => key!("parts"),
+ Self::Base => key!("base"),
+ Self::Src => key!("src"),
+ Self::Slot => key!("slot"),
+ Self::License => key!("license"),
+ Self::Thumb => key!("thumb"),
+ Self::EquippedNft => key!("equipped-nft"),
+ Self::BaseType => key!("base-type"),
+ // RmrkPartId(/* Id type? */)
+ Self::EquippableList => key!("equippable-list"),
+ Self::ZIndex => key!("z-index"),
+ Self::ThemeName => key!("theme-name"),
+ Self::ThemeProperty(name) => key!("theme-property-", name),
+ Self::ThemeInherit => key!("theme-inherit"),
+ }
+ }
+}
+
+#[macro_export]
+macro_rules! rmrk_property {
+ (Config=$cfg:ty, $key:ident: $value:expr) => {
+ rmrk_property!(@$cfg, $key).map(|key| Property {
+ key,
+ value: $value.into()
+ })
+ };
+
+ (@$cfg:ty, $key:ident) => {
+ $crate::RmrkProperty::$key.to_key::<$cfg>()
+ };
+
+ (Config=$cfg:ty, $key:ident) => {
+ PropertyScope::Rmrk.apply(rmrk_property!(@$cfg, $key)?)
+ .map_err(|_| <Error<T>>::RmrkPropertyIsTooLong)
+ };
+}
primitives/data-structs/src/lib.rsdiffbeforeafterboth--- a/primitives/data-structs/src/lib.rs
+++ b/primitives/data-structs/src/lib.rs
@@ -924,4 +924,4 @@
pub type RmrkThemeName = RmrkRpcString;
pub type RmrkPropertyKey = RmrkRpcString;
-type RmrkString = BoundedVec<u8, RmrkStringLimit>;
+pub type RmrkString = BoundedVec<u8, RmrkStringLimit>;