Skip to content

Commit 9aecaff

Browse files
authored
Merge pull request #159 from rmrk-team/bug/132-convert-properties-to-bounded-vec
convert Theme properties to BoundedVec
2 parents 8be39bb + d458e07 commit 9aecaff

File tree

5 files changed

+26
-18
lines changed

5 files changed

+26
-18
lines changed

pallets/rmrk-equip/src/functions.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ impl<T: Config>
4545
T::PartsLimit,
4646
>,
4747
BoundedVec<CollectionId, T::MaxCollectionsEquippablePerPart>,
48+
BoundedVec<ThemeProperty<BoundedVec<u8, T::StringLimit>>, T::MaxPropertiesPerTheme>
4849
> for Pallet<T>
4950
where
5051
T: pallet_uniques::Config<CollectionId = CollectionId, ItemId = NftId>,
@@ -365,7 +366,7 @@ where
365366
fn add_theme(
366367
issuer: T::AccountId,
367368
base_id: BaseId,
368-
theme: Theme<BoundedVec<u8, T::StringLimit>>,
369+
theme: Theme<BoundedVec<u8, T::StringLimit>, BoundedVec<ThemeProperty<BoundedVec<u8, T::StringLimit>>, T::MaxPropertiesPerTheme>>
369370
) -> Result<(), DispatchError> {
370371
// Base must exist
371372
ensure!(Bases::<T>::get(base_id).is_some(), Error::<T>::BaseDoesntExist);

pallets/rmrk-equip/src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub use pallet::*;
1717
use rmrk_traits::{
1818
primitives::*, AccountIdOrCollectionNftTuple, Base, BaseInfo, BasicResource,
1919
ComposableResource, EquippableList, PartType, ResourceTypes, SlotResource, Theme,
20+
ThemeProperty,
2021
};
2122

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

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

41+
pub type BoundedThemeOf<T> = Theme<
42+
BoundedVec<u8, <T as pallet_uniques::Config>::StringLimit>,
43+
BoundedVec<
44+
ThemeProperty<BoundedVec<u8, <T as pallet_uniques::Config>::StringLimit>>,
45+
<T as Config>::MaxPropertiesPerTheme>
46+
>;
47+
4048
#[frame_support::pallet]
4149
pub mod pallet {
4250
use super::*;
@@ -337,7 +345,7 @@ pub mod pallet {
337345
pub fn theme_add(
338346
origin: OriginFor<T>,
339347
base_id: BaseId,
340-
theme: Theme<BoundedVec<u8, T::StringLimit>>,
348+
theme: BoundedThemeOf<T>,
341349
) -> DispatchResult {
342350
let sender = ensure_signed(origin)?;
343351

pallets/rmrk-equip/src/tests.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ fn theme_add_works() {
552552
// Define a non-default theme
553553
let non_default_theme = Theme {
554554
name: stb("doglover"),
555-
properties: vec![
555+
properties: bvec![
556556
ThemeProperty { key: stb("sound"), value: stb("woof") },
557557
ThemeProperty { key: stb("secondary_color"), value: stb("blue") },
558558
],
@@ -590,7 +590,7 @@ fn theme_add_works() {
590590
// Define a default theme
591591
let default_theme = Theme {
592592
name: stb("default"),
593-
properties: vec![
593+
properties: bvec![
594594
ThemeProperty { key: stb("primary_color"), value: stb("red") },
595595
ThemeProperty { key: stb("secondary_color"), value: stb("blue") },
596596
],
@@ -644,6 +644,7 @@ fn theme_add_works() {
644644
}
645645

646646
/// Theme add fails when too many properties
647+
#[should_panic]
647648
#[test]
648649
fn theme_add_too_many_properties_fails() {
649650
ExtBuilder::default().build().execute_with(|| {
@@ -656,9 +657,10 @@ fn theme_add_too_many_properties_fails() {
656657
));
657658

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

676-
// Add default theme to base should fail (too many properties)
677-
assert_noop!(
678-
RmrkEquip::theme_add(
679-
Origin::signed(ALICE),
680-
0, // BaseID
681-
default_theme
682-
),
683-
Error::<Test>::TooManyProperties
678+
// We only run this to avoid having to define default_theme's type above
679+
// Otherwise it will fail to compile
680+
RmrkEquip::theme_add(
681+
Origin::signed(ALICE),
682+
0, // BaseID
683+
default_theme,
684684
);
685685
});
686686
}

traits/src/base.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub struct BaseInfo<AccountId, BoundedString, BoundedParts> {
2727
}
2828

2929
// Abstraction over a Base system.
30-
pub trait Base<AccountId, CollectionId, NftId, BoundedString, BoundedParts, BoundedCollectionList> {
30+
pub trait Base<AccountId, CollectionId, NftId, BoundedString, BoundedParts, BoundedCollectionList, BoundedThemeProperties> {
3131
fn base_create(
3232
issuer: AccountId,
3333
base_type: BoundedString,
@@ -55,6 +55,6 @@ pub trait Base<AccountId, CollectionId, NftId, BoundedString, BoundedParts, Boun
5555
fn add_theme(
5656
issuer: AccountId,
5757
base_id: BaseId,
58-
theme: Theme<BoundedString>,
58+
theme: Theme<BoundedString, BoundedThemeProperties>,
5959
) -> Result<(), DispatchError>;
6060
}

traits/src/theme.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
use codec::{Decode, Encode};
66
use scale_info::TypeInfo;
77
use sp_runtime::RuntimeDebug;
8-
use sp_std::vec::Vec;
98

109
#[cfg_attr(feature = "std", derive(Eq))]
1110
#[derive(Encode, Decode, RuntimeDebug, TypeInfo, Clone, PartialEq)]
12-
pub struct Theme<BoundedString> {
11+
pub struct Theme<BoundedString, BoundedThemeProperties> {
1312
/// Name of the theme
1413
pub name: BoundedString,
1514
/// Theme properties
16-
pub properties: Vec<ThemeProperty<BoundedString>>,
15+
pub properties: BoundedThemeProperties,
1716
/// Inheritability
1817
pub inherit: bool,
1918
}

0 commit comments

Comments
 (0)