Skip to content

Commit e6a42c9

Browse files
authored
Merge pull request #162 from rmrk-team/bug/146-add-resources-on-mint
allow adding resources on mint
2 parents 9aecaff + 242e882 commit e6a42c9

File tree

8 files changed

+115
-2
lines changed

8 files changed

+115
-2
lines changed

pallets/rmrk-core/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ pub type KeyLimitOf<T> = BoundedVec<u8, <T as pallet_uniques::Config>::KeyLimit>
4646

4747
pub type ValueLimitOf<T> = BoundedVec<u8, <T as pallet_uniques::Config>::ValueLimit>;
4848

49+
pub type BoundedResourceTypeOf<T> = BoundedVec<
50+
ResourceTypes<
51+
BoundedVec<u8, <T as pallet_uniques::Config>::StringLimit>,
52+
BoundedVec<PartId, <T as Config>::PartsLimit>>,
53+
<T as Config>::MaxResourcesOnMint
54+
>;
55+
4956
pub mod types;
5057

5158
// Re-export pallet items so that they can be accessed from the crate namespace.
@@ -78,6 +85,8 @@ pub mod pallet {
7885
type MaxPriorities: Get<u32>;
7986

8087
type CollectionSymbolLimit: Get<u32>;
88+
89+
type MaxResourcesOnMint: Get<u32>;
8190
}
8291

8392
#[pallet::storage]
@@ -343,6 +352,7 @@ pub mod pallet {
343352
royalty: Option<Permill>,
344353
metadata: BoundedVec<u8, T::StringLimit>,
345354
transferable: bool,
355+
resources: Option<BoundedResourceTypeOf<T>>
346356
) -> DispatchResult {
347357
let sender = ensure_signed(origin.clone())?;
348358
if let Some(collection_issuer) = pallet_uniques::Pallet::<T>::collection_owner(collection_id)
@@ -369,6 +379,12 @@ pub mod pallet {
369379
|_details| Ok(()),
370380
)?;
371381

382+
if let Some(resources) = resources {
383+
for res in resources {
384+
Self::resource_add(owner.clone(), collection_id, nft_id, res)?;
385+
}
386+
}
387+
372388
Self::deposit_event(Event::NftMinted { owner, collection_id, nft_id });
373389

374390
Ok(())

pallets/rmrk-core/src/mock.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ parameter_types! {
5050
pub const PartsLimit: u32 = 50;
5151
pub const MaxPriorities: u32 = 3;
5252
pub const CollectionSymbolLimit: u32 = 100;
53+
pub const MaxResourcesOnMint: u32 = 3;
5354
}
5455

5556
impl pallet_rmrk_core::Config for Test {
@@ -61,6 +62,7 @@ impl pallet_rmrk_core::Config for Test {
6162
type PartsLimit = PartsLimit;
6263
type MaxPriorities = MaxPriorities;
6364
type CollectionSymbolLimit = CollectionSymbolLimit;
65+
type MaxResourcesOnMint = MaxResourcesOnMint;
6466
}
6567

6668
parameter_types! {

pallets/rmrk-core/src/tests.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ fn basic_mint() -> DispatchResult {
6060
Some(Permill::from_float(1.525)),
6161
bvec![0u8; 20],
6262
true,
63+
None,
6364
)
6465
}
6566

@@ -240,6 +241,7 @@ fn mint_nft_works() {
240241
Some(Permill::from_float(20.525)),
241242
bvec![0u8; 20],
242243
true,
244+
None,
243245
));
244246
// BOB shouldn't be able to mint in ALICE's collection
245247
assert_noop!(
@@ -251,6 +253,7 @@ fn mint_nft_works() {
251253
Some(Permill::from_float(20.525)),
252254
bvec![0u8; 20],
253255
true,
256+
None,
254257
),
255258
Error::<Test>::NoPermission
256259
);
@@ -264,6 +267,7 @@ fn mint_nft_works() {
264267
Some(Permill::from_float(20.525)),
265268
bvec![0u8; 20],
266269
true,
270+
None,
267271
),
268272
Error::<Test>::CollectionUnknown
269273
);
@@ -304,6 +308,7 @@ fn royalty_recipient_default_works() {
304308
Some(Permill::from_float(20.525)),
305309
bvec![0u8; 20],
306310
true,
311+
None,
307312
));
308313
// Royalty recipient should default to issuer (ALICE)
309314
assert_eq!(RmrkCore::nfts(0, 0).unwrap().royalty.unwrap().recipient, ALICE);
@@ -316,6 +321,7 @@ fn royalty_recipient_default_works() {
316321
Some(Permill::from_float(20.525)),
317322
bvec![0u8; 20],
318323
true,
324+
None,
319325
));
320326
// Royalty recipient should be BOB
321327
assert_eq!(RmrkCore::nfts(0, 1).unwrap().royalty.unwrap().recipient, BOB);
@@ -328,6 +334,7 @@ fn royalty_recipient_default_works() {
328334
None, // No royalty amount
329335
bvec![0u8; 20],
330336
true,
337+
None,
331338
));
332339
// Royalty should not exist
333340
assert!(RmrkCore::nfts(0, 2).unwrap().royalty.is_none());
@@ -340,6 +347,7 @@ fn royalty_recipient_default_works() {
340347
None, // No royalty amount
341348
bvec![0u8; 20],
342349
true,
350+
None,
343351
));
344352
// Royalty should not exist
345353
assert!(RmrkCore::nfts(0, 3).unwrap().royalty.is_none());
@@ -511,6 +519,7 @@ fn send_non_transferable_fail() {
511519
Some(Permill::from_float(1.525)),
512520
bvec![0u8; 20],
513521
false, // non-transferable
522+
None,
514523
));
515524
assert_noop!(
516525
RMRKCore::send(
@@ -581,6 +590,7 @@ fn reject_nft_removes_self_from_parents_children() {
581590
Some(Permill::from_float(1.525)),
582591
bvec![0u8; 20],
583592
true,
593+
None
584594
));
585595
// BOB sends NFT (0, 1) to ALICE's NFT (0, 0)
586596
assert_ok!(RMRKCore::send(
@@ -986,6 +996,78 @@ fn create_resource_works() {
986996
});
987997
}
988998

999+
/// Minting with resources works
1000+
#[test]
1001+
fn add_resource_on_mint_works() {
1002+
ExtBuilder::default().build().execute_with(|| {
1003+
let basic_resource: BasicResource<BoundedVec<u8, UniquesStringLimit>> =
1004+
BasicResource { src: None, metadata: None, license: None, thumb: None };
1005+
1006+
// Create a basic collection
1007+
assert_ok!(basic_collection());
1008+
1009+
let basic_resource =
1010+
BasicResource { src: None, metadata: None, license: None, thumb: None };
1011+
1012+
// Resources to add
1013+
let resources_to_add = bvec![
1014+
ResourceTypes::Basic(basic_resource.clone()),
1015+
ResourceTypes::Basic(basic_resource),
1016+
];
1017+
1018+
// Mint NFT
1019+
assert_ok!(RMRKCore::mint_nft(
1020+
Origin::signed(ALICE),
1021+
ALICE,
1022+
COLLECTION_ID_0,
1023+
Some(ALICE),
1024+
Some(Permill::from_float(1.525)),
1025+
bvec![0u8; 20],
1026+
true,
1027+
Some(resources_to_add),
1028+
));
1029+
1030+
assert_eq!(RMRKCore::resources((0, 0, 0)).is_some(), true);
1031+
assert_eq!(RMRKCore::resources((0, 0, 1)).is_some(), true);
1032+
});
1033+
}
1034+
1035+
/// Minting with more than max resources (set to 3 in mock) should panic
1036+
#[should_panic]
1037+
#[test]
1038+
fn add_resource_on_mint_beyond_max_fails() {
1039+
ExtBuilder::default().build().execute_with(|| {
1040+
let basic_resource: BasicResource<BoundedVec<u8, UniquesStringLimit>> =
1041+
BasicResource { src: None, metadata: None, license: None, thumb: None };
1042+
1043+
// Create a basic collection
1044+
assert_ok!(basic_collection());
1045+
1046+
let basic_resource =
1047+
BasicResource { src: None, metadata: None, license: None, thumb: None };
1048+
1049+
// Resources to add
1050+
let resources_to_add = bvec![
1051+
ResourceTypes::Basic(basic_resource.clone()),
1052+
ResourceTypes::Basic(basic_resource.clone()),
1053+
ResourceTypes::Basic(basic_resource.clone()),
1054+
ResourceTypes::Basic(basic_resource),
1055+
];
1056+
1057+
// Mint NFT
1058+
RMRKCore::mint_nft(
1059+
Origin::signed(ALICE),
1060+
ALICE,
1061+
COLLECTION_ID_0,
1062+
Some(ALICE),
1063+
Some(Permill::from_float(1.525)),
1064+
bvec![0u8; 20],
1065+
true,
1066+
Some(resources_to_add),
1067+
)
1068+
});
1069+
}
1070+
9891071
/// Resource: Resource addition with pending and accept (RMRK2.0 spec: ACCEPT)
9901072
#[test]
9911073
fn add_resource_pending_works() {
@@ -1001,6 +1083,7 @@ fn add_resource_pending_works() {
10011083
Some(Permill::from_float(1.525)),
10021084
bvec![0u8; 20],
10031085
true,
1086+
None
10041087
));
10051088

10061089
let basic_resource = BasicResource {
@@ -1123,6 +1206,7 @@ fn resource_removal_pending_works() {
11231206
Some(Permill::from_float(1.525)),
11241207
bvec![0u8; 20],
11251208
true,
1209+
None
11261210
));
11271211

11281212
let basic_resource =

pallets/rmrk-equip/src/mock.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (C) 2021-2022 RMRK
22
// This file is part of rmrk-equip.
33
// License: Apache 2.0 modified by RMRK, see LICENSE.md
4-
5-
use super::*;
64
use crate as pallet_rmrk_equip;
75

86
use frame_support::{
@@ -62,6 +60,8 @@ parameter_types! {
6260
pub const PartsLimit: u32 = 10;
6361
pub const MaxPriorities: u32 = 3;
6462
pub const CollectionSymbolLimit: u32 = 100;
63+
pub const MaxResourcesOnMint: u32 = 3;
64+
6565
}
6666

6767
impl pallet_rmrk_core::Config for Test {
@@ -73,6 +73,7 @@ impl pallet_rmrk_core::Config for Test {
7373
type PartsLimit = PartsLimit;
7474
type MaxPriorities = MaxPriorities;
7575
type CollectionSymbolLimit = CollectionSymbolLimit;
76+
type MaxResourcesOnMint = MaxResourcesOnMint;
7677
}
7778

7879
parameter_types! {

pallets/rmrk-equip/src/tests.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ fn equip_works() {
180180
Some(Permill::from_float(1.525)), // royalties
181181
stb("ipfs://character-0-metadata"), // metadata
182182
true,
183+
None,
183184
));
184185

185186
// Mint NFT 1 from collection 0 (character-1)
@@ -191,6 +192,7 @@ fn equip_works() {
191192
Some(Permill::from_float(1.525)), // royalties
192193
stb("ipfs://character-1-metadata"), // metadata
193194
true,
195+
None,
194196
));
195197

196198
// Mint NFT 0 from collection 1 (sword)
@@ -202,6 +204,7 @@ fn equip_works() {
202204
Some(Permill::from_float(1.525)), // royalties
203205
stb("ipfs://sword-metadata"), // metadata
204206
true,
207+
None,
205208
));
206209

207210
// Mint NFT 1 from collection 1 (flashlight)
@@ -213,6 +216,7 @@ fn equip_works() {
213216
Some(Permill::from_float(1.525)), // royalties
214217
stb("ipfs://flashlight-metadata"), // metadata
215218
true,
219+
None,
216220
));
217221

218222
// Attempt to equip sword should fail as character-0 doesn't own sword

pallets/rmrk-market/src/mock.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ parameter_types! {
101101
pub const PartsLimit: u32 = 10;
102102
pub const MaxPriorities: u32 = 3;
103103
pub const CollectionSymbolLimit: u32 = 100;
104+
pub const MaxResourcesOnMint: u32 = 3;
104105
}
105106

106107
impl pallet_rmrk_core::Config for Test {
@@ -112,6 +113,7 @@ impl pallet_rmrk_core::Config for Test {
112113
type PartsLimit = PartsLimit;
113114
type MaxPriorities = MaxPriorities;
114115
type CollectionSymbolLimit = CollectionSymbolLimit;
116+
type MaxResourcesOnMint = MaxResourcesOnMint;
115117
}
116118

117119
parameter_types! {

pallets/rmrk-market/src/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ fn basic_mint() -> DispatchResult {
4646
Some(Permill::from_float(1.525)),
4747
bvec![0u8; 20],
4848
true,
49+
None,
4950
)
5051
}
5152

@@ -133,6 +134,7 @@ fn list_non_transferable_fail() {
133134
Some(Permill::from_float(1.525)),
134135
bvec![0u8; 20],
135136
false, // non-transferable
137+
None,
136138
));
137139
assert_noop!(
138140
RmrkMarket::list(Origin::signed(ALICE), COLLECTION_ID_0, 0, 10u128, None,),

runtime/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ parameter_types! {
338338
pub const PartsLimit: u32 = 25;
339339
pub const MaxPriorities: u32 = 25;
340340
pub const CollectionSymbolLimit: u32 = 100;
341+
pub const MaxResourcesOnMint: u32 = 100;
341342
}
342343

343344
impl pallet_rmrk_core::Config for Runtime {
@@ -348,6 +349,7 @@ impl pallet_rmrk_core::Config for Runtime {
348349
type PartsLimit = PartsLimit;
349350
type MaxPriorities = MaxPriorities;
350351
type CollectionSymbolLimit = CollectionSymbolLimit;
352+
type MaxResourcesOnMint = MaxResourcesOnMint;
351353
}
352354

353355
parameter_types! {

0 commit comments

Comments
 (0)