Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 5b11f65

Browse files
[NFTs] Add mint price to the witness object on mint and confirm it (#14257)
* Add mint price to the witness object on mint and confirm it * Chore * Put the new error to the bottom * Update frame/nfts/src/lib.rs Co-authored-by: joe petrowski <[email protected]> --------- Co-authored-by: joe petrowski <[email protected]>
1 parent c080caf commit 5b11f65

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

frame/nfts/src/lib.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,8 @@ pub mod pallet {
636636
WrongNamespace,
637637
/// Can't delete non-empty collections.
638638
CollectionNotEmpty,
639+
/// The witness data should be provided.
640+
WitnessRequired,
639641
}
640642

641643
#[pallet::call]
@@ -771,7 +773,8 @@ pub mod pallet {
771773
/// - `item`: An identifier of the new item.
772774
/// - `mint_to`: Account into which the item will be minted.
773775
/// - `witness_data`: When the mint type is `HolderOf(collection_id)`, then the owned
774-
/// item_id from that collection needs to be provided within the witness data object.
776+
/// item_id from that collection needs to be provided within the witness data object. If
777+
/// the mint price is set, then it should be additionally confirmed in the `witness_data`.
775778
///
776779
/// Note: the deposit will be taken from the `origin` and not the `owner` of the `item`.
777780
///
@@ -785,7 +788,7 @@ pub mod pallet {
785788
collection: T::CollectionId,
786789
item: T::ItemId,
787790
mint_to: AccountIdLookupOf<T>,
788-
witness_data: Option<MintWitness<T::ItemId>>,
791+
witness_data: Option<MintWitness<T::ItemId, DepositBalanceOf<T, I>>>,
789792
) -> DispatchResult {
790793
let caller = ensure_signed(origin)?;
791794
let mint_to = T::Lookup::lookup(mint_to)?;
@@ -817,8 +820,8 @@ pub mod pallet {
817820
);
818821
},
819822
MintType::HolderOf(collection_id) => {
820-
let MintWitness { owned_item } =
821-
witness_data.ok_or(Error::<T, I>::BadWitness)?;
823+
let MintWitness { owned_item, .. } =
824+
witness_data.clone().ok_or(Error::<T, I>::WitnessRequired)?;
822825

823826
let owns_item = Account::<T, I>::contains_key((
824827
&caller,
@@ -858,6 +861,10 @@ pub mod pallet {
858861
}
859862

860863
if let Some(price) = mint_settings.price {
864+
let MintWitness { mint_price, .. } =
865+
witness_data.clone().ok_or(Error::<T, I>::WitnessRequired)?;
866+
let mint_price = mint_price.ok_or(Error::<T, I>::BadWitness)?;
867+
ensure!(mint_price >= price, Error::<T, I>::BadWitness);
861868
T::Currency::transfer(
862869
&caller,
863870
&collection_details.owner,

frame/nfts/src/tests.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,37 @@ fn mint_should_work() {
369369
MintSettings { mint_type: MintType::Public, price: Some(1), ..Default::default() }
370370
));
371371
Balances::make_free_balance_be(&account(2), 100);
372-
assert_ok!(Nfts::mint(RuntimeOrigin::signed(account(2)), 0, 43, account(2), None));
372+
assert_noop!(
373+
Nfts::mint(RuntimeOrigin::signed(account(2)), 0, 43, account(2), None,),
374+
Error::<Test>::WitnessRequired
375+
);
376+
assert_noop!(
377+
Nfts::mint(
378+
RuntimeOrigin::signed(account(2)),
379+
0,
380+
43,
381+
account(2),
382+
Some(MintWitness { ..Default::default() })
383+
),
384+
Error::<Test>::BadWitness
385+
);
386+
assert_noop!(
387+
Nfts::mint(
388+
RuntimeOrigin::signed(account(2)),
389+
0,
390+
43,
391+
account(2),
392+
Some(MintWitness { mint_price: Some(0), ..Default::default() })
393+
),
394+
Error::<Test>::BadWitness
395+
);
396+
assert_ok!(Nfts::mint(
397+
RuntimeOrigin::signed(account(2)),
398+
0,
399+
43,
400+
account(2),
401+
Some(MintWitness { mint_price: Some(1), ..Default::default() })
402+
));
373403
assert_eq!(Balances::total_balance(&account(2)), 99);
374404

375405
// validate types
@@ -385,19 +415,19 @@ fn mint_should_work() {
385415
));
386416
assert_noop!(
387417
Nfts::mint(RuntimeOrigin::signed(account(3)), 1, 42, account(3), None),
388-
Error::<Test>::BadWitness
418+
Error::<Test>::WitnessRequired
389419
);
390420
assert_noop!(
391421
Nfts::mint(RuntimeOrigin::signed(account(2)), 1, 42, account(2), None),
392-
Error::<Test>::BadWitness
422+
Error::<Test>::WitnessRequired
393423
);
394424
assert_noop!(
395425
Nfts::mint(
396426
RuntimeOrigin::signed(account(2)),
397427
1,
398428
42,
399429
account(2),
400-
Some(MintWitness { owned_item: 42 })
430+
Some(MintWitness { owned_item: 42, ..Default::default() })
401431
),
402432
Error::<Test>::BadWitness
403433
);
@@ -406,7 +436,7 @@ fn mint_should_work() {
406436
1,
407437
42,
408438
account(2),
409-
Some(MintWitness { owned_item: 43 })
439+
Some(MintWitness { owned_item: 43, ..Default::default() })
410440
));
411441

412442
// can't mint twice
@@ -416,7 +446,7 @@ fn mint_should_work() {
416446
1,
417447
46,
418448
account(2),
419-
Some(MintWitness { owned_item: 43 })
449+
Some(MintWitness { owned_item: 43, ..Default::default() })
420450
),
421451
Error::<Test>::AlreadyClaimed
422452
);

frame/nfts/src/types.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,12 @@ impl<AccountId, DepositBalance> CollectionDetails<AccountId, DepositBalance> {
124124
}
125125

126126
/// Witness data for items mint transactions.
127-
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo)]
128-
pub struct MintWitness<ItemId> {
127+
#[derive(Clone, Encode, Decode, Default, Eq, PartialEq, RuntimeDebug, TypeInfo)]
128+
pub struct MintWitness<ItemId, Balance> {
129129
/// Provide the id of the item in a required collection.
130130
pub owned_item: ItemId,
131+
/// The price specified in mint settings.
132+
pub mint_price: Option<Balance>,
131133
}
132134

133135
/// Information concerning the ownership of a single unique item.

0 commit comments

Comments
 (0)