Skip to content

Commit 20235f9

Browse files
committed
add storage for Composable and SlotResource
this prevents need for some iteration
1 parent 91701c8 commit 20235f9

File tree

6 files changed

+77
-46
lines changed

6 files changed

+77
-46
lines changed

pallets/rmrk-core/src/functions.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,21 @@ where
8888
// Check NFT lock status
8989
ensure!(!Pallet::<T>::is_locked(collection_id, nft_id), pallet_uniques::Error::<T>::Locked);
9090

91-
let res = ResourceInfo::<
92-
BoundedVec<u8, T::StringLimit>,
93-
BoundedVec<PartId, T::PartsLimit>,
94-
> {
91+
match resource.clone() {
92+
ResourceTypes::Basic(_r) => (),
93+
ResourceTypes::Composable(r) => {
94+
ComposableResources::<T>::insert((collection_id, nft_id, r.base), ());
95+
},
96+
ResourceTypes::Slot(r) => {
97+
SlotResources::<T>::insert(
98+
(collection_id, nft_id, resource_id, r.base, r.slot),
99+
(),
100+
);
101+
},
102+
}
103+
104+
let res: ResourceInfo<BoundedVec<u8, T::StringLimit>, BoundedVec<PartId, T::PartsLimit>> =
105+
ResourceInfo::<BoundedVec<u8, T::StringLimit>, BoundedVec<PartId, T::PartsLimit>> {
95106
id: resource_id,
96107
pending: root_owner != sender,
97108
pending_removal: false,

pallets/rmrk-core/src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,34 @@ pub mod pallet {
155155
OptionQuery,
156156
>;
157157

158+
#[pallet::storage]
159+
#[pallet::getter(fn composable_resources)]
160+
/// Stores resource info
161+
pub type ComposableResources<T: Config> = StorageNMap<
162+
_,
163+
(
164+
NMapKey<Blake2_128Concat, CollectionId>,
165+
NMapKey<Blake2_128Concat, NftId>,
166+
NMapKey<Blake2_128Concat, BaseId>,
167+
),
168+
(),
169+
>;
170+
171+
#[pallet::storage]
172+
#[pallet::getter(fn slot_resources)]
173+
/// Stores resource info
174+
pub type SlotResources<T: Config> = StorageNMap<
175+
_,
176+
(
177+
NMapKey<Blake2_128Concat, CollectionId>,
178+
NMapKey<Blake2_128Concat, NftId>,
179+
NMapKey<Blake2_128Concat, ResourceId>,
180+
NMapKey<Blake2_128Concat, BaseId>,
181+
NMapKey<Blake2_128Concat, SlotId>,
182+
),
183+
(),
184+
>;
185+
158186
#[pallet::storage]
159187
#[pallet::getter(fn properties)]
160188
/// Metadata of an asset class.

pallets/rmrk-equip/src/functions.rs

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ where
111111
issuer: T::AccountId,
112112
item: (CollectionId, NftId),
113113
equipper: (CollectionId, NftId),
114+
resource_id: ResourceId,
114115
base_id: BaseId,
115116
slot_id: SlotId,
116117
) -> Result<(CollectionId, NftId, BaseId, SlotId, bool), DispatchError> {
@@ -223,49 +224,29 @@ where
223224

224225
// Equipper must have a resource that is associated with the provided base ID
225226
// First we iterate through the resources added to this NFT in search of the base ID
226-
let mut found_base_resource_on_nft = false;
227-
let resources_matching_base_iter = pallet_rmrk_core::Resources::<T>::iter_prefix_values((
227+
ensure!(
228+
pallet_rmrk_core::Pallet::<T>::composable_resources((
228229
equipper_collection_id,
229230
equipper_nft_id,
230-
));
231-
232-
for resource in resources_matching_base_iter {
233-
match resource.resource {
234-
ResourceTypes::Composable(res) => {
235-
if res.base == base_id {
236-
found_base_resource_on_nft = true;
237-
}
238-
},
239-
_ => (),
240-
}
241-
242-
}
243-
244-
// If we don't find a matching base resource, we raise a NoResourceForThisBaseFoundOnNft
245-
// error
246-
ensure!(found_base_resource_on_nft, Error::<T>::NoResourceForThisBaseFoundOnNft);
231+
// resource_id,
232+
base_id
233+
))
234+
.is_some(),
235+
Error::<T>::NoResourceForThisBaseFoundOnNft
236+
);
247237

248238
// The item being equipped must be have a resource that is equippable into that base.slot
249-
let mut found_base_slot_resource_on_nft = false;
250-
251-
// initialized so the compiler doesn't complain, though it will be overwritten if it
252-
// resource exists
253-
let mut to_equip_resource_id: ResourceId = 0_u32.into();
254-
255-
let resources_matching_base_iter =
256-
pallet_rmrk_core::Resources::<T>::iter_prefix_values((item_collection_id, item_nft_id));
257-
258-
for resource in resources_matching_base_iter {
259-
match resource.resource {
260-
ResourceTypes::Slot(res) =>
261-
if res.slot == slot_id && res.base == base_id {
262-
found_base_slot_resource_on_nft = true;
263-
to_equip_resource_id = resource.id;
264-
},
265-
_ => (),
266-
}
267-
}
268-
ensure!(found_base_slot_resource_on_nft, Error::<T>::ItemHasNoResourceToEquipThere);
239+
ensure!(
240+
pallet_rmrk_core::Pallet::<T>::slot_resources((
241+
item_collection_id,
242+
item_nft_id,
243+
resource_id,
244+
base_id,
245+
slot_id
246+
))
247+
.is_some(),
248+
Error::<T>::ItemHasNoResourceToEquipThere
249+
);
269250

270251
// Part must exist
271252
ensure!(Self::parts(base_id, slot_id).is_some(), Error::<T>::PartDoesntExist);
@@ -290,7 +271,7 @@ where
290271
// Equip item (add to Equippings)
291272
Equippings::<T>::insert(
292273
((equipper_collection_id, equipper_nft_id), base_id, slot_id),
293-
to_equip_resource_id,
274+
resource_id,
294275
);
295276

296277
// Update item's equipped property

pallets/rmrk-equip/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,14 @@ pub mod pallet {
253253
origin: OriginFor<T>,
254254
item: (CollectionId, NftId),
255255
equipper: (CollectionId, NftId),
256+
resource_id: ResourceId,
256257
base: BaseId,
257258
slot: SlotId,
258259
) -> DispatchResult {
259260
let sender = ensure_signed(origin)?;
260261

261262
let (collection_id, nft_id, base_id, slot_id, equipped) =
262-
Self::do_equip(sender, item, equipper, base, slot)?;
263+
Self::do_equip(sender, item, equipper, resource_id, base, slot)?;
263264

264265
if equipped {
265266
// Send Equip event

pallets/rmrk-equip/src/tests.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ fn equip_works() {
213213
Origin::signed(ALICE), // Signer
214214
(1, 0), // item
215215
(0, 0), // equipper
216+
0, // ResourceId (doesn't exist)
216217
0, // BaseId
217218
201, // SlotId
218219
),
@@ -234,6 +235,7 @@ fn equip_works() {
234235
Origin::signed(ALICE), // Signer
235236
(1, 0), // item
236237
(0, 0), // equipper
238+
0, // ResourceId
237239
0, // BaseId
238240
201, // SlotId
239241
),
@@ -277,6 +279,7 @@ fn equip_works() {
277279
Origin::signed(ALICE), // Signer
278280
(1, 0), // item
279281
(0, 0), // equipper
282+
0, // ResourceId
280283
0, // BaseId
281284
201, // SlotId
282285
),
@@ -305,6 +308,7 @@ fn equip_works() {
305308
Origin::signed(ALICE), // Signer
306309
(1, 0), // item
307310
(0, 0), // equipper
311+
0, // ResourceId,
308312
0, // BaseId
309313
201, // SlotId
310314
));
@@ -347,6 +351,7 @@ fn equip_works() {
347351
Origin::signed(ALICE), // Signer
348352
(1, 0), // item
349353
(0, 0), // equipper
354+
0, // ResourceId
350355
0, // BaseId
351356
202, // SlotId
352357
),
@@ -358,6 +363,7 @@ fn equip_works() {
358363
Origin::signed(ALICE), // Signer
359364
(1, 0), // item
360365
(0, 0), // equipper
366+
0, // ResourceId
361367
0, // BaseId
362368
201, // SlotId
363369
));
@@ -374,6 +380,7 @@ fn equip_works() {
374380
Origin::signed(ALICE), // Signer
375381
(1, 0), // item
376382
(0, 0), // equipper
383+
0, // ResourceId
377384
0, // BaseId
378385
201, // SlotId
379386
));
@@ -383,6 +390,7 @@ fn equip_works() {
383390
Origin::signed(ALICE), // Signer
384391
(1, 0), // item
385392
(0, 0), // equipper
393+
0, // ResourceId
386394
0, // BaseId
387395
201, // SlotId
388396
));
@@ -392,6 +400,7 @@ fn equip_works() {
392400
Origin::signed(ALICE), // Signer
393401
(1, 0), // item
394402
(0, 0), // equipper
403+
1, // ResourceId
395404
0, // BaseId
396405
202, // SlotId
397406
));

traits/src/base.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::{
22
part::{EquippableList, PartType},
33
theme::Theme,
44
};
5-
use crate::primitives::{BaseId, SlotId};
5+
use crate::primitives::{BaseId, SlotId, ResourceId};
66
use codec::{Decode, Encode};
77
use scale_info::TypeInfo;
88
use sp_runtime::{DispatchError, RuntimeDebug};
@@ -39,6 +39,7 @@ pub trait Base<AccountId, CollectionId, NftId, BoundedString, BoundedParts, Boun
3939
issuer: AccountId, // Maybe don't need?
4040
item: (CollectionId, NftId),
4141
equipper: (CollectionId, NftId),
42+
resource_id: ResourceId,
4243
base_id: BaseId, // Maybe BaseId ?
4344
slot: SlotId, // Maybe SlotId ?
4445
) -> Result<(CollectionId, NftId, BaseId, SlotId, bool), DispatchError>;

0 commit comments

Comments
 (0)