difftreelog
feat implement theme RMRK RPC
in: master
7 files changed
pallets/proxy-rmrk-core/src/lib.rsdiffbeforeafterboth--- a/pallets/proxy-rmrk-core/src/lib.rs
+++ b/pallets/proxy-rmrk-core/src/lib.rs
@@ -443,7 +443,7 @@
<TokenData<T>>::get((collection_id, token_id))
.unwrap()
.rmrk_nft_type()
- .ok_or(<Error<T>>::NoAvailableNftId.into())
+ .ok_or_else(|| <Error<T>>::NoAvailableNftId.into())
}
pub fn ensure_nft_type(collection_id: CollectionId, token_id: TokenId, nft_type: NftType) -> DispatchResult {
@@ -453,6 +453,65 @@
Ok(())
}
+ pub fn filter_theme_properties(
+ collection_id: CollectionId,
+ token_id: TokenId,
+ filter_keys: Option<Vec<RmrkPropertyKey>>
+ ) -> Result<Vec<RmrkThemeProperty>, DispatchError> {
+ filter_keys.map(|keys| {
+ let properties = keys.into_iter()
+ .filter_map(|key| {
+ let key: RmrkString = key.try_into().ok()?;
+
+ let value = Self::get_nft_property(
+ collection_id,
+ token_id,
+ RmrkProperty::ThemeProperty(&key)
+ ).ok()?.decode_or_default();
+
+ let property = RmrkThemeProperty {
+ key,
+ value
+ };
+
+ Some(property)
+ })
+ .collect();
+
+ Ok(properties)
+ }).unwrap_or_else(|| {
+ let properties = Self::iterate_theme_properties(collection_id, token_id)?
+ .collect();
+
+ Ok(properties)
+ })
+ }
+
+ pub fn iterate_theme_properties(
+ collection_id: CollectionId,
+ token_id: TokenId
+ ) -> Result<impl Iterator<Item=RmrkThemeProperty>, DispatchError> {
+ let key_prefix = rmrk_property!(Config=T, key: ThemeProperty(&RmrkString::default()))?;
+
+ let properties = <PalletNft<T>>::token_properties((collection_id, token_id))
+ .into_iter()
+ .filter_map(move |(key, value)| {
+ let key = key.as_slice().strip_prefix(key_prefix.as_slice())?;
+
+ let key: RmrkString = key.to_vec().try_into().ok()?;
+ let value: RmrkString = value.decode_or_default();
+
+ let property = RmrkThemeProperty {
+ key,
+ value
+ };
+
+ Some(property)
+ });
+
+ Ok(properties)
+ }
+
pub fn get_typed_nft_collection(
collection_id: CollectionId,
collection_type: CollectionType
pallets/proxy-rmrk-core/src/property.rsdiffbeforeafterboth--- a/pallets/proxy-rmrk-core/src/property.rs
+++ b/pallets/proxy-rmrk-core/src/property.rs
@@ -1,7 +1,7 @@
use super::*;
use core::convert::AsRef;
-pub enum RmrkProperty {
+pub enum RmrkProperty<'r> {
Metadata,
CollectionType,
RoyaltyInfo,
@@ -23,11 +23,11 @@
EquippableList,
ZIndex,
ThemeName,
- ThemeProperty(RmrkString),
+ ThemeProperty(&'r RmrkString),
ThemeInherit,
}
-impl RmrkProperty {
+impl<'r> RmrkProperty<'r> {
pub fn to_key<T: Config>(self) -> Result<PropertyKey, Error<T>> {
fn get_bytes<T: AsRef<[u8]>>(container: &T) -> &[u8] {
container.as_ref()
primitives/data-structs/src/lib.rsdiffbeforeafterboth--- a/primitives/data-structs/src/lib.rs
+++ b/primitives/data-structs/src/lib.rs
@@ -775,6 +775,10 @@
self.0.iter()
}
+ pub fn into_iter(self) -> impl Iterator<Item = (PropertyKey, Value)> {
+ self.0.into_iter()
+ }
+
fn check_property_key(key: &PropertyKey) -> Result<(), PropertiesError> {
if key.is_empty() {
return Err(PropertiesError::EmptyPropertyKey);
@@ -848,6 +852,10 @@
pub fn iter(&self) -> impl Iterator<Item = (&PropertyKey, &PropertyValue)> {
self.map.iter()
}
+
+ pub fn into_iter(self) -> impl Iterator<Item = (PropertyKey, PropertyValue)> {
+ self.map.into_iter()
+ }
}
impl TrySetProperty for Properties {
@@ -936,7 +944,8 @@
pub type RmrkBaseInfo<AccountId> = BaseInfo<AccountId, RmrkString>;
pub type RmrkPartType =
PartType<RmrkString, BoundedVec<RmrkCollectionId, RmrkMaxCollectionsEquippablePerPart>>;
-pub type RmrkTheme = Theme<RmrkString, Vec<ThemeProperty<RmrkString>>>;
+pub type RmrkThemeProperty = ThemeProperty<RmrkString>;
+pub type RmrkTheme = Theme<RmrkString, Vec<RmrkThemeProperty>>;
pub type RmrkRpcString = Vec<u8>;
pub type RmrkThemeName = RmrkRpcString;
runtime/common/src/runtime_apis.rsdiffbeforeafterboth379379380 fn theme_names(base_id: RmrkBaseId) -> Result<Vec<RmrkThemeName>, DispatchError> {380 fn theme_names(base_id: RmrkBaseId) -> Result<Vec<RmrkThemeName>, DispatchError> {381 use frame_support::BoundedVec;381 use frame_support::BoundedVec;382 use pallet_proxy_rmrk_core::{RmrkProperty, misc::{RmrkNft, RmrkDecode}};382 use pallet_proxy_rmrk_core::{RmrkProperty, misc::{CollectionType, RmrkNft, RmrkDecode}};383383384 let collection_id = CollectionId(base_id);384 let collection_id = CollectionId(base_id);385 // todo make sure this is theme385 if RmrkCore::ensure_collection_type(collection_id, CollectionType::Base).is_err() {386 return Ok(Vec::new());387 }386388387 let theme_names = (dispatch_unique_runtime!(collection_id.collection_tokens()) as Result<Vec<TokenId>, DispatchError>)?389 let theme_names = (dispatch_unique_runtime!(collection_id.collection_tokens()))?388 .iter()390 .iter()389 .filter_map(|token_id| {391 .filter_map(|token_id| {390 let nft_type = RmrkCore::get_nft_type(collection_id, *token_id).unwrap();392 let nft_type = RmrkCore::get_nft_type(collection_id, *token_id).unwrap();396 _ => None398 _ => None397 }399 }398 })400 })399 .collect::<Vec<RmrkThemeName>>();401 .collect();400402401 Ok(theme_names)403 Ok(theme_names)402 }404 }403405404 fn theme(base_id: RmrkBaseId, theme_name: RmrkThemeName, filter_keys: Option<Vec<RmrkPropertyKey>>) -> Result<Option<RmrkTheme>, DispatchError> {406 fn theme(base_id: RmrkBaseId, theme_name: RmrkThemeName, filter_keys: Option<Vec<RmrkPropertyKey>>) -> Result<Option<RmrkTheme>, DispatchError> {405 use frame_support::BoundedVec;407 use frame_support::BoundedVec;408 use pallet_proxy_rmrk_core::{409 RmrkProperty,410 misc::{CollectionType, NftType, RmrkNft, RmrkDecode}411 };406412407 let collection_id = CollectionId(base_id);413 let collection_id = CollectionId(base_id);408414 if RmrkCore::ensure_collection_type(collection_id, CollectionType::Base).is_err() {409 // todo one theme. filter collection tokens according to theme name, should result in one415 return Ok(None);410 // (is it possible to search with iter_prefix for part of a struct that satisfies?..)416 }411 // filter properties according to filter_keys and load them into resulting theme.properties417412 let themes = (dispatch_unique_runtime!(collection_id.collection_tokens()) as Result<Vec<TokenId>, DispatchError>)?418 let theme_info = (dispatch_unique_runtime!(collection_id.collection_tokens()))?413 .iter()419 .into_iter()414 .filter_map(|token_id| {420 .find_map(|token_id| {415 let properties = Nonfungible::token_properties((collection_id, token_id));421 RmrkCore::ensure_nft_type(collection_id, token_id, NftType::Theme).ok()?;416422417 // todo ping properties for "rmrk:nft-type"418 // if none, skip, None419 // ugh gonna go through ALL properties, searching for matches for "rmrk:theme-property-<key>"420 let nft_type = "theme";423 let name: RmrkString = RmrkCore::get_nft_property(421 match nft_type {422 "theme" => Some(RmrkTheme {423 name: BoundedVec::try_from(424 <pallet_nonfungible::TokenData<Runtime>>::get((collection_id, token_id))424 collection_id, token_id, RmrkProperty::ThemeName425 .map(|t| t.const_data)425 ).ok()?.decode_or_default();426 .unwrap_or_default()426427 .into_inner()427 if name == theme_name {428 ).unwrap(),429 // todo? (dispatch_unique_runtime!(collection_id.const_metadata(token_id)) as Result<Vec<u8>, DispatchError>)?,430 properties: Vec::new(), // pain in the ass428 Some((name, token_id))431 inherit: false, // "rmrk:theme-inherit"429 } else {432 }),433 _ => None430 None434 }431 }435 })432 });433434 let (name, theme_id) = match theme_info {435 Some((name, theme_id)) => (name, theme_id),436 None => return Ok(None)437 };438436 .collect::<Vec<_>>();439 let properties = RmrkCore::filter_theme_properties(collection_id, theme_id, filter_keys)?;437440438 // todo441 let inherit = RmrkCore::get_nft_property(442 collection_id,443 theme_id,444 RmrkProperty::ThemeInherit445 )?.decode_or_default();446447 let theme = RmrkTheme {448 name,449 properties,450 inherit,451 };452439 Ok(Some(themes[0].clone()))453 Ok(Some(theme))440 }454 }441 }455 }442456runtime/opal/src/lib.rsdiffbeforeafterboth--- a/runtime/opal/src/lib.rs
+++ b/runtime/opal/src/lib.rs
@@ -1149,7 +1149,7 @@
let collection = <Runtime as pallet_common::Config>::CollectionDispatch::dispatch(<pallet_common::CollectionHandle<Runtime>>::try_get($collection)?);
let dispatch = collection.as_dyn();
- Ok(dispatch.$method($($name),*))
+ Ok::<_, DispatchError>(dispatch.$method($($name),*))
}};
}
runtime/quartz/src/lib.rsdiffbeforeafterboth--- a/runtime/quartz/src/lib.rs
+++ b/runtime/quartz/src/lib.rs
@@ -1122,7 +1122,7 @@
let collection = <Runtime as pallet_common::Config>::CollectionDispatch::dispatch(<pallet_common::CollectionHandle<Runtime>>::try_get($collection)?);
let dispatch = collection.as_dyn();
- Ok(dispatch.$method($($name),*))
+ Ok::<_, DispatchError>(dispatch.$method($($name),*))
}};
}
runtime/unique/src/lib.rsdiffbeforeafterboth--- a/runtime/unique/src/lib.rs
+++ b/runtime/unique/src/lib.rs
@@ -1127,7 +1127,7 @@
let collection = <Runtime as pallet_common::Config>::CollectionDispatch::dispatch(<pallet_common::CollectionHandle<Runtime>>::try_get($collection)?);
let dispatch = collection.as_dyn();
- Ok(dispatch.$method($($name),*))
+ Ok::<_, DispatchError>(dispatch.$method($($name),*))
}};
}