Skip to content

Commit 91701c8

Browse files
committed
switch to auto-increment ResourceId
1 parent 218fe30 commit 91701c8

File tree

7 files changed

+112
-163
lines changed

7 files changed

+112
-163
lines changed

pallets/rmrk-core/src/functions.rs

Lines changed: 17 additions & 15 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,42 @@ 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

9191
let res = ResourceInfo::<
92-
BoundedVec<u8, T::ResourceSymbolLimit>,
9392
BoundedVec<u8, T::StringLimit>,
9493
BoundedVec<PartId, T::PartsLimit>,
9594
> {
96-
id: resource_id.clone(),
95+
id: resource_id,
9796
pending: root_owner != sender,
9897
pending_removal: false,
99-
resource
98+
resource,
10099
};
101100
Resources::<T>::insert((collection_id, nft_id, resource_id), res);
102101

103-
Ok(())
102+
Ok(resource_id)
104103
}
105104

106105
fn accept(
107106
sender: T::AccountId,
108107
collection_id: CollectionId,
109108
nft_id: NftId,
110-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
109+
resource_id: ResourceId,
111110
) -> DispatchResult {
112111
let (root_owner, _) = Pallet::<T>::lookup_root_owner(collection_id, nft_id)?;
113112
ensure!(root_owner == sender, Error::<T>::NoPermission);
114113
// Check NFT lock status
115114
ensure!(!Pallet::<T>::is_locked(collection_id, nft_id), pallet_uniques::Error::<T>::Locked);
116115
Resources::<T>::try_mutate_exists(
117-
(collection_id, nft_id, resource_id.clone()),
116+
(collection_id, nft_id, resource_id),
118117
|resource| -> DispatchResult {
119118
if let Some(res) = resource {
120119
res.pending = false;
@@ -131,13 +130,13 @@ where
131130
sender: T::AccountId,
132131
collection_id: CollectionId,
133132
nft_id: NftId,
134-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
133+
resource_id: ResourceId,
135134
) -> DispatchResult {
136135
let (root_owner, _) = Pallet::<T>::lookup_root_owner(collection_id, nft_id)?;
137136
let collection = Self::collections(collection_id).ok_or(Error::<T>::CollectionUnknown)?;
138137
ensure!(collection.issuer == sender, Error::<T>::NoPermission);
139138
ensure!(
140-
Resources::<T>::contains_key((collection_id, nft_id, &resource_id)),
139+
Resources::<T>::contains_key((collection_id, nft_id, resource_id)),
141140
Error::<T>::ResourceDoesntExist
142141
);
143142

@@ -162,7 +161,7 @@ where
162161
sender: T::AccountId,
163162
collection_id: CollectionId,
164163
nft_id: NftId,
165-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
164+
resource_id: ResourceId,
166165
) -> DispatchResult {
167166
let (root_owner, _) = Pallet::<T>::lookup_root_owner(collection_id, nft_id)?;
168167
ensure!(root_owner == sender, Error::<T>::NoPermission);
@@ -652,10 +651,13 @@ where
652651
})
653652
}
654653

655-
pub fn get_next_resource_id() -> Result<ResourceId, Error<T>> {
656-
NextResourceId::<T>::try_mutate(|id| {
654+
pub fn get_next_resource_id(
655+
collection_id: CollectionId,
656+
nft_id: NftId,
657+
) -> Result<ResourceId, Error<T>> {
658+
NextResourceId::<T>::try_mutate(collection_id, nft_id, |id| {
657659
let current_id = *id;
658-
*id = id.checked_add(1).ok_or(Error::<T>::NoAvailableCollectionId)?;
660+
*id = id.checked_add(1).ok_or(Error::<T>::NoAvailableResourceId)?;
659661
Ok(current_id)
660662
})
661663
}

pallets/rmrk-core/src/lib.rs

Lines changed: 30 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,9 +149,9 @@ 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

@@ -229,19 +236,19 @@ pub mod pallet {
229236
},
230237
ResourceAdded {
231238
nft_id: NftId,
232-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
239+
resource_id: ResourceId,
233240
},
234241
ResourceAccepted {
235242
nft_id: NftId,
236-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
243+
resource_id: ResourceId,
237244
},
238245
ResourceRemoval {
239246
nft_id: NftId,
240-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
247+
resource_id: ResourceId,
241248
},
242249
ResourceRemovalAccepted {
243250
nft_id: NftId,
244-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
251+
resource_id: ResourceId,
245252
},
246253
PrioritySet {
247254
collection_id: CollectionId,
@@ -258,6 +265,7 @@ pub mod pallet {
258265
StorageOverflow,
259266
TooLong,
260267
NoAvailableCollectionId,
268+
NoAvailableResourceId,
261269
MetadataNotSet,
262270
RecipientNotSet,
263271
NoAvailableNftId,
@@ -582,16 +590,14 @@ pub mod pallet {
582590
origin: OriginFor<T>,
583591
collection_id: CollectionId,
584592
nft_id: NftId,
585-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
586593
resource: BasicResource<StringLimitOf<T>>,
587594
) -> DispatchResult {
588595
let sender = ensure_signed(origin.clone())?;
589596

590-
Self::resource_add(
597+
let resource_id = Self::resource_add(
591598
sender,
592599
collection_id,
593600
nft_id,
594-
resource_id.clone(),
595601
ResourceTypes::Basic(resource),
596602
)?;
597603

@@ -611,11 +617,10 @@ pub mod pallet {
611617
) -> DispatchResult {
612618
let sender = ensure_signed(origin.clone())?;
613619

614-
Self::resource_add(
620+
let resource_id = Self::resource_add(
615621
sender,
616622
collection_id,
617623
nft_id,
618-
resource_id.clone(),
619624
ResourceTypes::Composable(resource),
620625
)?;
621626

@@ -630,16 +635,14 @@ pub mod pallet {
630635
origin: OriginFor<T>,
631636
collection_id: CollectionId,
632637
nft_id: NftId,
633-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
634638
resource: SlotResource<StringLimitOf<T>>,
635639
) -> DispatchResult {
636640
let sender = ensure_signed(origin.clone())?;
637641

638-
Self::resource_add(
642+
let resource_id = Self::resource_add(
639643
sender,
640644
collection_id,
641645
nft_id,
642-
resource_id.clone(),
643646
ResourceTypes::Slot(resource),
644647
)?;
645648

@@ -654,19 +657,19 @@ pub mod pallet {
654657
origin: OriginFor<T>,
655658
collection_id: CollectionId,
656659
nft_id: NftId,
657-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
660+
resource_id: ResourceId,
658661
) -> DispatchResult {
659662
let sender = ensure_signed(origin.clone())?;
660663
ensure!(
661-
Resources::<T>::get((collection_id, nft_id, resource_id.clone())).is_some(),
664+
Resources::<T>::get((collection_id, nft_id, resource_id)).is_some(),
662665
Error::<T>::ResourceDoesntExist
663666
);
664667

665668
let (owner, _) = Pallet::<T>::lookup_root_owner(collection_id, nft_id)?;
666669
ensure!(owner == sender, Error::<T>::NoPermission);
667670

668671
Resources::<T>::try_mutate_exists(
669-
(collection_id, nft_id, resource_id.clone()),
672+
(collection_id, nft_id, resource_id),
670673
|resource| -> DispatchResult {
671674
if let Some(res) = resource.into_mut() {
672675
ensure!(res.pending, Error::<T>::ResourceNotPending);
@@ -687,11 +690,11 @@ pub mod pallet {
687690
origin: OriginFor<T>,
688691
collection_id: CollectionId,
689692
nft_id: NftId,
690-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
693+
resource_id: ResourceId,
691694
) -> DispatchResult {
692695
let sender = ensure_signed(origin.clone())?;
693696

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

696699
Self::deposit_event(Event::ResourceRemoval { nft_id, resource_id });
697700
Ok(())
@@ -704,11 +707,11 @@ pub mod pallet {
704707
origin: OriginFor<T>,
705708
collection_id: CollectionId,
706709
nft_id: NftId,
707-
resource_id: BoundedResource<T::ResourceSymbolLimit>,
710+
resource_id: ResourceId,
708711
) -> DispatchResult {
709712
let sender = ensure_signed(origin.clone())?;
710713

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

713716
Self::deposit_event(Event::ResourceRemovalAccepted { nft_id, resource_id });
714717
Ok(())

0 commit comments

Comments
 (0)