[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 <25483142+joepetrowski@users.noreply.github.com>

---------

Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com>
This commit is contained in:
Jegor Sidorenko
2023-05-29 18:59:43 +02:00
committed by GitHub
parent f602db2d96
commit 50cf22b0bb
3 changed files with 51 additions and 12 deletions
+11 -4
View File
@@ -636,6 +636,8 @@ pub mod pallet {
WrongNamespace, WrongNamespace,
/// Can't delete non-empty collections. /// Can't delete non-empty collections.
CollectionNotEmpty, CollectionNotEmpty,
/// The witness data should be provided.
WitnessRequired,
} }
#[pallet::call] #[pallet::call]
@@ -771,7 +773,8 @@ pub mod pallet {
/// - `item`: An identifier of the new item. /// - `item`: An identifier of the new item.
/// - `mint_to`: Account into which the item will be minted. /// - `mint_to`: Account into which the item will be minted.
/// - `witness_data`: When the mint type is `HolderOf(collection_id)`, then the owned /// - `witness_data`: When the mint type is `HolderOf(collection_id)`, then the owned
/// item_id from that collection needs to be provided within the witness data object. /// item_id from that collection needs to be provided within the witness data object. If
/// the mint price is set, then it should be additionally confirmed in the `witness_data`.
/// ///
/// Note: the deposit will be taken from the `origin` and not the `owner` of the `item`. /// Note: the deposit will be taken from the `origin` and not the `owner` of the `item`.
/// ///
@@ -785,7 +788,7 @@ pub mod pallet {
collection: T::CollectionId, collection: T::CollectionId,
item: T::ItemId, item: T::ItemId,
mint_to: AccountIdLookupOf<T>, mint_to: AccountIdLookupOf<T>,
witness_data: Option<MintWitness<T::ItemId>>, witness_data: Option<MintWitness<T::ItemId, DepositBalanceOf<T, I>>>,
) -> DispatchResult { ) -> DispatchResult {
let caller = ensure_signed(origin)?; let caller = ensure_signed(origin)?;
let mint_to = T::Lookup::lookup(mint_to)?; let mint_to = T::Lookup::lookup(mint_to)?;
@@ -817,8 +820,8 @@ pub mod pallet {
); );
}, },
MintType::HolderOf(collection_id) => { MintType::HolderOf(collection_id) => {
let MintWitness { owned_item } = let MintWitness { owned_item, .. } =
witness_data.ok_or(Error::<T, I>::BadWitness)?; witness_data.clone().ok_or(Error::<T, I>::WitnessRequired)?;
let owns_item = Account::<T, I>::contains_key(( let owns_item = Account::<T, I>::contains_key((
&caller, &caller,
@@ -858,6 +861,10 @@ pub mod pallet {
} }
if let Some(price) = mint_settings.price { if let Some(price) = mint_settings.price {
let MintWitness { mint_price, .. } =
witness_data.clone().ok_or(Error::<T, I>::WitnessRequired)?;
let mint_price = mint_price.ok_or(Error::<T, I>::BadWitness)?;
ensure!(mint_price >= price, Error::<T, I>::BadWitness);
T::Currency::transfer( T::Currency::transfer(
&caller, &caller,
&collection_details.owner, &collection_details.owner,
+36 -6
View File
@@ -369,7 +369,37 @@ fn mint_should_work() {
MintSettings { mint_type: MintType::Public, price: Some(1), ..Default::default() } MintSettings { mint_type: MintType::Public, price: Some(1), ..Default::default() }
)); ));
Balances::make_free_balance_be(&account(2), 100); Balances::make_free_balance_be(&account(2), 100);
assert_ok!(Nfts::mint(RuntimeOrigin::signed(account(2)), 0, 43, account(2), None)); assert_noop!(
Nfts::mint(RuntimeOrigin::signed(account(2)), 0, 43, account(2), None,),
Error::<Test>::WitnessRequired
);
assert_noop!(
Nfts::mint(
RuntimeOrigin::signed(account(2)),
0,
43,
account(2),
Some(MintWitness { ..Default::default() })
),
Error::<Test>::BadWitness
);
assert_noop!(
Nfts::mint(
RuntimeOrigin::signed(account(2)),
0,
43,
account(2),
Some(MintWitness { mint_price: Some(0), ..Default::default() })
),
Error::<Test>::BadWitness
);
assert_ok!(Nfts::mint(
RuntimeOrigin::signed(account(2)),
0,
43,
account(2),
Some(MintWitness { mint_price: Some(1), ..Default::default() })
));
assert_eq!(Balances::total_balance(&account(2)), 99); assert_eq!(Balances::total_balance(&account(2)), 99);
// validate types // validate types
@@ -385,11 +415,11 @@ fn mint_should_work() {
)); ));
assert_noop!( assert_noop!(
Nfts::mint(RuntimeOrigin::signed(account(3)), 1, 42, account(3), None), Nfts::mint(RuntimeOrigin::signed(account(3)), 1, 42, account(3), None),
Error::<Test>::BadWitness Error::<Test>::WitnessRequired
); );
assert_noop!( assert_noop!(
Nfts::mint(RuntimeOrigin::signed(account(2)), 1, 42, account(2), None), Nfts::mint(RuntimeOrigin::signed(account(2)), 1, 42, account(2), None),
Error::<Test>::BadWitness Error::<Test>::WitnessRequired
); );
assert_noop!( assert_noop!(
Nfts::mint( Nfts::mint(
@@ -397,7 +427,7 @@ fn mint_should_work() {
1, 1,
42, 42,
account(2), account(2),
Some(MintWitness { owned_item: 42 }) Some(MintWitness { owned_item: 42, ..Default::default() })
), ),
Error::<Test>::BadWitness Error::<Test>::BadWitness
); );
@@ -406,7 +436,7 @@ fn mint_should_work() {
1, 1,
42, 42,
account(2), account(2),
Some(MintWitness { owned_item: 43 }) Some(MintWitness { owned_item: 43, ..Default::default() })
)); ));
// can't mint twice // can't mint twice
@@ -416,7 +446,7 @@ fn mint_should_work() {
1, 1,
46, 46,
account(2), account(2),
Some(MintWitness { owned_item: 43 }) Some(MintWitness { owned_item: 43, ..Default::default() })
), ),
Error::<Test>::AlreadyClaimed Error::<Test>::AlreadyClaimed
); );
+4 -2
View File
@@ -124,10 +124,12 @@ impl<AccountId, DepositBalance> CollectionDetails<AccountId, DepositBalance> {
} }
/// Witness data for items mint transactions. /// Witness data for items mint transactions.
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo)] #[derive(Clone, Encode, Decode, Default, Eq, PartialEq, RuntimeDebug, TypeInfo)]
pub struct MintWitness<ItemId> { pub struct MintWitness<ItemId, Balance> {
/// Provide the id of the item in a required collection. /// Provide the id of the item in a required collection.
pub owned_item: ItemId, pub owned_item: ItemId,
/// The price specified in mint settings.
pub mint_price: Option<Balance>,
} }
/// Information concerning the ownership of a single unique item. /// Information concerning the ownership of a single unique item.