Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert Theme properties to BoundedVec #159

Merged
merged 4 commits into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pallets/rmrk-equip/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ impl<T: Config>
T::PartsLimit,
>,
BoundedVec<CollectionId, T::MaxCollectionsEquippablePerPart>,
BoundedVec<ThemeProperty<BoundedVec<u8, T::StringLimit>>, T::MaxPropertiesPerTheme>
> for Pallet<T>
where
T: pallet_uniques::Config<ClassId = CollectionId, InstanceId = NftId>,
Expand Down Expand Up @@ -365,7 +366,7 @@ where
fn add_theme(
issuer: T::AccountId,
base_id: BaseId,
theme: Theme<BoundedVec<u8, T::StringLimit>>,
theme: Theme<BoundedVec<u8, T::StringLimit>, BoundedVec<ThemeProperty<BoundedVec<u8, T::StringLimit>>, T::MaxPropertiesPerTheme>>
) -> Result<(), DispatchError> {
// Base must exist
ensure!(Bases::<T>::get(base_id).is_some(), Error::<T>::BaseDoesntExist);
Expand Down
10 changes: 9 additions & 1 deletion pallets/rmrk-equip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub use pallet::*;
use rmrk_traits::{
primitives::*, AccountIdOrCollectionNftTuple, Base, BaseInfo, BasicResource,
ComposableResource, EquippableList, PartType, ResourceTypes, SlotResource, Theme,
ThemeProperty,
};

mod functions;
Expand All @@ -37,6 +38,13 @@ pub type StringLimitOf<T> = BoundedVec<u8, <T as pallet_uniques::Config>::String

pub type BoundedResource<T> = BoundedVec<u8, <T as pallet_rmrk_core::Config>::ResourceSymbolLimit>;

pub type BoundedThemeOf<T> = Theme<
BoundedVec<u8, <T as pallet_uniques::Config>::StringLimit>,
BoundedVec<
ThemeProperty<BoundedVec<u8, <T as pallet_uniques::Config>::StringLimit>>,
<T as Config>::MaxPropertiesPerTheme>
>;

#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand Down Expand Up @@ -337,7 +345,7 @@ pub mod pallet {
pub fn theme_add(
origin: OriginFor<T>,
base_id: BaseId,
theme: Theme<BoundedVec<u8, T::StringLimit>>,
theme: BoundedThemeOf<T>,
) -> DispatchResult {
let sender = ensure_signed(origin)?;

Expand Down
22 changes: 11 additions & 11 deletions pallets/rmrk-equip/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ fn theme_add_works() {
// Define a non-default theme
let non_default_theme = Theme {
name: stb("doglover"),
properties: vec![
properties: bvec![
ThemeProperty { key: stb("sound"), value: stb("woof") },
ThemeProperty { key: stb("secondary_color"), value: stb("blue") },
],
Expand Down Expand Up @@ -590,7 +590,7 @@ fn theme_add_works() {
// Define a default theme
let default_theme = Theme {
name: stb("default"),
properties: vec![
properties: bvec![
ThemeProperty { key: stb("primary_color"), value: stb("red") },
ThemeProperty { key: stb("secondary_color"), value: stb("blue") },
],
Expand Down Expand Up @@ -644,6 +644,7 @@ fn theme_add_works() {
}

/// Theme add fails when too many properties
#[should_panic]
#[test]
fn theme_add_too_many_properties_fails() {
ExtBuilder::default().build().execute_with(|| {
Expand All @@ -656,9 +657,10 @@ fn theme_add_too_many_properties_fails() {
));

// Define a default theme with too many properties (10)
// Should panic as properties exceeds mock's max (5)
let default_theme = Theme {
name: stb("default"),
properties: vec![
properties: bvec![
ThemeProperty { key: stb("1"), value: stb("red") },
ThemeProperty { key: stb("2"), value: stb("blue") },
ThemeProperty { key: stb("3"), value: stb("red") },
Expand All @@ -673,14 +675,12 @@ fn theme_add_too_many_properties_fails() {
inherit: false,
};

// Add default theme to base should fail (too many properties)
assert_noop!(
RmrkEquip::theme_add(
Origin::signed(ALICE),
0, // BaseID
default_theme
),
Error::<Test>::TooManyProperties
// We only run this to avoid having to define default_theme's type above
// Otherwise it will fail to compile
RmrkEquip::theme_add(
Origin::signed(ALICE),
0, // BaseID
default_theme,
);
});
}
4 changes: 2 additions & 2 deletions traits/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct BaseInfo<AccountId, BoundedString, BoundedParts> {
}

// Abstraction over a Base system.
pub trait Base<AccountId, CollectionId, NftId, BoundedString, BoundedParts, BoundedCollectionList> {
pub trait Base<AccountId, CollectionId, NftId, BoundedString, BoundedParts, BoundedCollectionList, BoundedThemeProperties> {
fn base_create(
issuer: AccountId,
base_type: BoundedString,
Expand Down Expand Up @@ -55,6 +55,6 @@ pub trait Base<AccountId, CollectionId, NftId, BoundedString, BoundedParts, Boun
fn add_theme(
issuer: AccountId,
base_id: BaseId,
theme: Theme<BoundedString>,
theme: Theme<BoundedString, BoundedThemeProperties>,
) -> Result<(), DispatchError>;
}
5 changes: 2 additions & 3 deletions traits/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
use codec::{Decode, Encode};
use scale_info::TypeInfo;
use sp_runtime::RuntimeDebug;
use sp_std::vec::Vec;

#[cfg_attr(feature = "std", derive(Eq))]
#[derive(Encode, Decode, RuntimeDebug, TypeInfo, Clone, PartialEq)]
pub struct Theme<BoundedString> {
pub struct Theme<BoundedString, BoundedThemeProperties> {
/// Name of the theme
pub name: BoundedString,
/// Theme properties
pub properties: Vec<ThemeProperty<BoundedString>>,
pub properties: BoundedThemeProperties,
/// Inheritability
pub inherit: bool,
}
Expand Down