Skip to content

Commit 84a73f7

Browse files
authored
Merge pull request #145 from rmrk-team/bug/138-switch-resource-id-to-auto-increment
switch to auto-increment ResourceId
2 parents 82e1bd5 + a692239 commit 84a73f7

File tree

8 files changed

+187
-207
lines changed

8 files changed

+187
-207
lines changed

pallets/rmrk-core/src/functions.rs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ impl<T: Config>
6969
Resource<
7070
BoundedVec<u8, T::StringLimit>,
7171
T::AccountId,
72-
BoundedResource<T::ResourceSymbolLimit>,
7372
BoundedVec<PartId, T::PartsLimit>,
7473
> for Pallet<T>
7574
where
@@ -79,42 +78,53 @@ where
7978
sender: T::AccountId,
8079
collection_id: CollectionId,
8180
nft_id: NftId,
82-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
8381
resource: ResourceTypes<BoundedVec<u8, T::StringLimit>, BoundedVec<PartId, T::PartsLimit>>,
84-
) -> DispatchResult {
82+
) -> Result<ResourceId, DispatchError> {
8583
let collection = Self::collections(collection_id).ok_or(Error::<T>::CollectionUnknown)?;
84+
let resource_id = Self::get_next_resource_id(collection_id, nft_id)?;
85+
8686
ensure!(collection.issuer == sender, Error::<T>::NoPermission);
8787
let (root_owner, _) = Pallet::<T>::lookup_root_owner(collection_id, nft_id)?;
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::ResourceSymbolLimit>,
93-
BoundedVec<u8, T::StringLimit>,
94-
BoundedVec<PartId, T::PartsLimit>,
95-
> {
96-
id: resource_id.clone(),
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>> {
106+
id: resource_id,
97107
pending: root_owner != sender,
98108
pending_removal: false,
99-
resource
109+
resource,
100110
};
101111
Resources::<T>::insert((collection_id, nft_id, resource_id), res);
102112

103-
Ok(())
113+
Ok(resource_id)
104114
}
105115

106116
fn accept(
107117
sender: T::AccountId,
108118
collection_id: CollectionId,
109119
nft_id: NftId,
110-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
120+
resource_id: ResourceId,
111121
) -> DispatchResult {
112122
let (root_owner, _) = Pallet::<T>::lookup_root_owner(collection_id, nft_id)?;
113123
ensure!(root_owner == sender, Error::<T>::NoPermission);
114124
// Check NFT lock status
115125
ensure!(!Pallet::<T>::is_locked(collection_id, nft_id), pallet_uniques::Error::<T>::Locked);
116126
Resources::<T>::try_mutate_exists(
117-
(collection_id, nft_id, resource_id.clone()),
127+
(collection_id, nft_id, resource_id),
118128
|resource| -> DispatchResult {
119129
if let Some(res) = resource {
120130
res.pending = false;
@@ -131,13 +141,13 @@ where
131141
sender: T::AccountId,
132142
collection_id: CollectionId,
133143
nft_id: NftId,
134-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
144+
resource_id: ResourceId,
135145
) -> DispatchResult {
136146
let (root_owner, _) = Pallet::<T>::lookup_root_owner(collection_id, nft_id)?;
137147
let collection = Self::collections(collection_id).ok_or(Error::<T>::CollectionUnknown)?;
138148
ensure!(collection.issuer == sender, Error::<T>::NoPermission);
139149
ensure!(
140-
Resources::<T>::contains_key((collection_id, nft_id, &resource_id)),
150+
Resources::<T>::contains_key((collection_id, nft_id, resource_id)),
141151
Error::<T>::ResourceDoesntExist
142152
);
143153

@@ -162,7 +172,7 @@ where
162172
sender: T::AccountId,
163173
collection_id: CollectionId,
164174
nft_id: NftId,
165-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
175+
resource_id: ResourceId,
166176
) -> DispatchResult {
167177
let (root_owner, _) = Pallet::<T>::lookup_root_owner(collection_id, nft_id)?;
168178
ensure!(root_owner == sender, Error::<T>::NoPermission);
@@ -660,10 +670,13 @@ where
660670
})
661671
}
662672

663-
pub fn get_next_resource_id() -> Result<ResourceId, Error<T>> {
664-
NextResourceId::<T>::try_mutate(|id| {
673+
pub fn get_next_resource_id(
674+
collection_id: CollectionId,
675+
nft_id: NftId,
676+
) -> Result<ResourceId, Error<T>> {
677+
NextResourceId::<T>::try_mutate(collection_id, nft_id, |id| {
665678
let current_id = *id;
666-
*id = id.checked_add(1).ok_or(Error::<T>::NoAvailableCollectionId)?;
679+
*id = id.checked_add(1).ok_or(Error::<T>::NoAvailableResourceId)?;
667680
Ok(current_id)
668681
})
669682
}

pallets/rmrk-core/src/lib.rs

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use sp_runtime::{traits::StaticLookup, DispatchError, Permill};
1111
use sp_std::convert::TryInto;
1212

1313
use rmrk_traits::{
14-
primitives::*, AccountIdOrCollectionNftTuple, Collection, CollectionInfo, Nft, NftInfo,
15-
Priority, Property, Resource, ResourceInfo, RoyaltyInfo,
16-
ResourceTypes, BasicResource, SlotResource, ComposableResource
14+
primitives::*, AccountIdOrCollectionNftTuple, BasicResource, Collection, CollectionInfo,
15+
ComposableResource, Nft, NftInfo, Priority, Property, Resource, ResourceInfo, ResourceTypes,
16+
RoyaltyInfo, SlotResource,
1717
};
1818
use sp_std::result::Result;
1919

@@ -29,8 +29,7 @@ pub type InstanceInfoOf<T> = NftInfo<
2929
<T as frame_system::Config>::AccountId,
3030
BoundedVec<u8, <T as pallet_uniques::Config>::StringLimit>,
3131
>;
32-
pub type ResourceOf<T, R, P> = ResourceInfo<
33-
BoundedVec<u8, R>,
32+
pub type ResourceOf<T, P> = ResourceInfo<
3433
BoundedVec<u8, <T as pallet_uniques::Config>::StringLimit>,
3534
BoundedVec<PartId, P>,
3635
>;
@@ -90,7 +89,15 @@ pub mod pallet {
9089
/// Next available Resource ID.
9190
#[pallet::storage]
9291
#[pallet::getter(fn next_resource_id)]
93-
pub type NextResourceId<T: Config> = StorageValue<_, ResourceId, ValueQuery>;
92+
pub type NextResourceId<T: Config> = StorageDoubleMap<
93+
_,
94+
Twox64Concat,
95+
CollectionId,
96+
Twox64Concat,
97+
NftId,
98+
ResourceId,
99+
ValueQuery,
100+
>;
94101

95102
#[pallet::storage]
96103
#[pallet::getter(fn collections)]
@@ -142,12 +149,40 @@ pub mod pallet {
142149
(
143150
NMapKey<Blake2_128Concat, CollectionId>,
144151
NMapKey<Blake2_128Concat, NftId>,
145-
NMapKey<Blake2_128Concat, BoundedResource<T::ResourceSymbolLimit>>,
152+
NMapKey<Blake2_128Concat, ResourceId>,
146153
),
147-
ResourceOf<T, T::ResourceSymbolLimit, T::PartsLimit>,
154+
ResourceOf<T, T::PartsLimit>,
148155
OptionQuery,
149156
>;
150157

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+
151186
#[pallet::storage]
152187
#[pallet::getter(fn properties)]
153188
/// Metadata of an asset class.
@@ -229,19 +264,19 @@ pub mod pallet {
229264
},
230265
ResourceAdded {
231266
nft_id: NftId,
232-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
267+
resource_id: ResourceId,
233268
},
234269
ResourceAccepted {
235270
nft_id: NftId,
236-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
271+
resource_id: ResourceId,
237272
},
238273
ResourceRemoval {
239274
nft_id: NftId,
240-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
275+
resource_id: ResourceId,
241276
},
242277
ResourceRemovalAccepted {
243278
nft_id: NftId,
244-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
279+
resource_id: ResourceId,
245280
},
246281
PrioritySet {
247282
collection_id: CollectionId,
@@ -258,6 +293,7 @@ pub mod pallet {
258293
StorageOverflow,
259294
TooLong,
260295
NoAvailableCollectionId,
296+
NoAvailableResourceId,
261297
MetadataNotSet,
262298
RecipientNotSet,
263299
NoAvailableNftId,
@@ -582,16 +618,14 @@ pub mod pallet {
582618
origin: OriginFor<T>,
583619
collection_id: CollectionId,
584620
nft_id: NftId,
585-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
586621
resource: BasicResource<StringLimitOf<T>>,
587622
) -> DispatchResult {
588623
let sender = ensure_signed(origin.clone())?;
589624

590-
Self::resource_add(
625+
let resource_id = Self::resource_add(
591626
sender,
592627
collection_id,
593628
nft_id,
594-
resource_id.clone(),
595629
ResourceTypes::Basic(resource),
596630
)?;
597631

@@ -611,11 +645,10 @@ pub mod pallet {
611645
) -> DispatchResult {
612646
let sender = ensure_signed(origin.clone())?;
613647

614-
Self::resource_add(
648+
let resource_id = Self::resource_add(
615649
sender,
616650
collection_id,
617651
nft_id,
618-
resource_id.clone(),
619652
ResourceTypes::Composable(resource),
620653
)?;
621654

@@ -630,16 +663,14 @@ pub mod pallet {
630663
origin: OriginFor<T>,
631664
collection_id: CollectionId,
632665
nft_id: NftId,
633-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
634666
resource: SlotResource<StringLimitOf<T>>,
635667
) -> DispatchResult {
636668
let sender = ensure_signed(origin.clone())?;
637669

638-
Self::resource_add(
670+
let resource_id = Self::resource_add(
639671
sender,
640672
collection_id,
641673
nft_id,
642-
resource_id.clone(),
643674
ResourceTypes::Slot(resource),
644675
)?;
645676

@@ -654,19 +685,19 @@ pub mod pallet {
654685
origin: OriginFor<T>,
655686
collection_id: CollectionId,
656687
nft_id: NftId,
657-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
688+
resource_id: ResourceId,
658689
) -> DispatchResult {
659690
let sender = ensure_signed(origin.clone())?;
660691
ensure!(
661-
Resources::<T>::get((collection_id, nft_id, resource_id.clone())).is_some(),
692+
Resources::<T>::get((collection_id, nft_id, resource_id)).is_some(),
662693
Error::<T>::ResourceDoesntExist
663694
);
664695

665696
let (owner, _) = Pallet::<T>::lookup_root_owner(collection_id, nft_id)?;
666697
ensure!(owner == sender, Error::<T>::NoPermission);
667698

668699
Resources::<T>::try_mutate_exists(
669-
(collection_id, nft_id, resource_id.clone()),
700+
(collection_id, nft_id, resource_id),
670701
|resource| -> DispatchResult {
671702
if let Some(res) = resource.into_mut() {
672703
ensure!(res.pending, Error::<T>::ResourceNotPending);
@@ -687,11 +718,11 @@ pub mod pallet {
687718
origin: OriginFor<T>,
688719
collection_id: CollectionId,
689720
nft_id: NftId,
690-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
721+
resource_id: ResourceId,
691722
) -> DispatchResult {
692723
let sender = ensure_signed(origin.clone())?;
693724

694-
Self::resource_remove(sender, collection_id, nft_id, resource_id.clone())?;
725+
Self::resource_remove(sender, collection_id, nft_id, resource_id)?;
695726

696727
Self::deposit_event(Event::ResourceRemoval { nft_id, resource_id });
697728
Ok(())
@@ -704,11 +735,11 @@ pub mod pallet {
704735
origin: OriginFor<T>,
705736
collection_id: CollectionId,
706737
nft_id: NftId,
707-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
738+
resource_id: ResourceId,
708739
) -> DispatchResult {
709740
let sender = ensure_signed(origin.clone())?;
710741

711-
Self::accept_removal(sender, collection_id, nft_id, resource_id.clone())?;
742+
Self::accept_removal(sender, collection_id, nft_id, resource_id)?;
712743

713744
Self::deposit_event(Event::ResourceRemovalAccepted { nft_id, resource_id });
714745
Ok(())

0 commit comments

Comments
 (0)