From 8e4361ae2f423459ac8718082940984d4dbc1f6e Mon Sep 17 00:00:00 2001 From: chungquantin <56880684+chungquantin@users.noreply.github.com> Date: Mon, 30 Dec 2024 11:02:18 +0700 Subject: [PATCH] chore: update tests --- pallets/nfts/src/tests.rs | 567 ++++++++++++++++++++------- runtime/devnet/src/config/assets.rs | 20 +- runtime/testnet/src/config/assets.rs | 20 +- 3 files changed, 470 insertions(+), 137 deletions(-) diff --git a/pallets/nfts/src/tests.rs b/pallets/nfts/src/tests.rs index f1c36c69..61f50b83 100644 --- a/pallets/nfts/src/tests.rs +++ b/pallets/nfts/src/tests.rs @@ -21,12 +21,10 @@ use enumflags2::BitFlags; use frame_support::{ assert_noop, assert_ok, dispatch::{DispatchResultWithPostInfo, WithPostDispatchInfo}, - pallet_prelude::MaxEncodedLen, traits::{ tokens::nonfungibles_v2::{Create, Destroy, Inspect, Mutate}, Currency, Get, }, - Blake2_128Concat, StorageHasher, }; use frame_system::pallet_prelude::BlockNumberFor; use pallet_balances::Error as BalancesError; @@ -40,6 +38,7 @@ use sp_runtime::{ use crate::{mock::*, Event, SystemConfig, *}; type AccountIdOf = ::AccountId; +type AccountBalance = crate::AccountBalance; type CollectionApprovals = crate::CollectionApprovals; type CollectionApprovalDeposit = ::CollectionApprovalDeposit; type CollectionId = ::CollectionId; @@ -231,7 +230,7 @@ fn basic_minting_should_work() { )); assert_eq!(collections(), vec![(account(1), 0)]); assert_ok!(Nfts::mint(RuntimeOrigin::signed(account(1)), 0, 42, account(1), None)); - assert_eq!(AccountBalance::::get(0, &account(1)), 1); + assert_eq!(AccountBalance::get(0, &account(1)), 1); assert_eq!(items(), vec![(account(1), 0, 42)]); assert_ok!(Nfts::force_create( @@ -241,7 +240,7 @@ fn basic_minting_should_work() { )); assert_eq!(collections(), vec![(account(1), 0), (account(2), 1)]); assert_ok!(Nfts::mint(RuntimeOrigin::signed(account(2)), 1, 69, account(1), None)); - assert_eq!(AccountBalance::::get(1, &account(1)), 1); + assert_eq!(AccountBalance::get(1, &account(1)), 1); assert_eq!(items(), vec![(account(1), 0, 42), (account(1), 1, 69)]); }); } @@ -281,10 +280,10 @@ fn lifecycle_should_work() { account(20), default_item_config() )); - assert_eq!(AccountBalance::::get(0, account(20)), 1); + assert_eq!(AccountBalance::get(0, account(20)), 1); assert_eq!(Balances::reserved_balance(&account(1)), 7); assert_ok!(Nfts::mint(RuntimeOrigin::signed(account(1)), 0, 70, account(1), None)); - assert_eq!(AccountBalance::::get(0, &account(1)), 1); + assert_eq!(AccountBalance::get(0, &account(1)), 1); assert_eq!(items(), vec![(account(1), 0, 70), (account(10), 0, 42), (account(20), 0, 69)]); assert_eq!(Collection::::get(0).unwrap().items, 3); assert_eq!(Collection::::get(0).unwrap().item_metadatas, 0); @@ -292,8 +291,8 @@ fn lifecycle_should_work() { assert_eq!(Balances::reserved_balance(&account(1)), 8); assert_ok!(Nfts::transfer(RuntimeOrigin::signed(account(1)), 0, 70, account(2))); - assert_eq!(AccountBalance::::get(0, &account(1)), 0); - assert_eq!(AccountBalance::::get(0, account(2)), 1); + assert!(!AccountBalance::contains_key(0, &account(1))); + assert_eq!(AccountBalance::get(0, account(2)), 1); assert_eq!(Balances::reserved_balance(&account(1)), 8); assert_eq!(Balances::reserved_balance(&account(2)), 0); @@ -321,9 +320,9 @@ fn lifecycle_should_work() { bvec![0], )); assert_ok!(Nfts::burn(RuntimeOrigin::signed(account(10)), 0, 42)); - assert_eq!(AccountBalance::::get(0, account(10)), 0); + assert!(!AccountBalance::contains_key(0, account(10))); assert_ok!(Nfts::burn(RuntimeOrigin::signed(account(20)), 0, 69)); - assert_eq!(AccountBalance::::get(0, account(20)), 0); + assert!(!AccountBalance::contains_key(0, account(20))); assert_ok!(Nfts::burn(RuntimeOrigin::root(), 0, 70)); let w = Nfts::get_destroy_witness(&0).unwrap(); @@ -331,7 +330,7 @@ fn lifecycle_should_work() { assert_eq!(w.item_metadatas, 0); assert_eq!(w.item_configs, 0); assert_ok!(Nfts::destroy(RuntimeOrigin::signed(account(1)), 0, w)); - assert_eq!(AccountBalance::::get(0, &account(1)), 0); + assert!(!AccountBalance::contains_key(0, &account(1))); assert_eq!(Balances::reserved_balance(&account(1)), 0); assert!(!Collection::::contains_key(0)); @@ -374,7 +373,6 @@ fn destroy_with_bad_witness_should_not_work() { fn destroy_should_work() { new_test_ext().execute_with(|| { Balances::make_free_balance_be(&account(1), 100); - Balances::make_free_balance_be(&account(2), 100); assert_ok!(Nfts::create( RuntimeOrigin::signed(account(1)), account(1), @@ -382,12 +380,6 @@ fn destroy_should_work() { )); assert_ok!(Nfts::mint(RuntimeOrigin::signed(account(1)), 0, 42, account(2), None)); - assert_ok!(Nfts::approve_collection_transfer( - RuntimeOrigin::signed(account(2)), - 0, - account(3), - None - )); assert_noop!( Nfts::destroy( RuntimeOrigin::signed(account(1)), @@ -401,19 +393,6 @@ fn destroy_should_work() { assert_eq!(Collection::::get(0).unwrap().item_configs, 1); assert_eq!(ItemConfigOf::::iter_prefix(0).count() as u32, 1); assert!(ItemConfigOf::::contains_key(0, 42)); - assert_noop!( - Nfts::destroy( - RuntimeOrigin::signed(account(1)), - 0, - Nfts::get_destroy_witness(&0).unwrap() - ), - Error::::CollectionApprovalsExist - ); - assert_ok!(Nfts::cancel_collection_approval( - RuntimeOrigin::signed(account(2)), - 0, - account(3) - )); assert_ok!(Nfts::destroy( RuntimeOrigin::signed(account(1)), 0, @@ -433,7 +412,7 @@ fn mint_should_work() { default_collection_config() )); assert_ok!(Nfts::mint(RuntimeOrigin::signed(account(1)), 0, 42, account(1), None)); - assert_eq!(AccountBalance::::get(0, account(1)), 1); + assert_eq!(AccountBalance::get(0, account(1)), 1); assert_eq!(Nfts::owner(0, 42).unwrap(), account(1)); assert_eq!(collections(), vec![(account(1), 0)]); assert_eq!(items(), vec![(account(1), 0, 42)]); @@ -499,7 +478,6 @@ fn mint_should_work() { account(2), Some(MintWitness { mint_price: Some(1), ..Default::default() }) )); - assert_eq!(AccountBalance::::get(0, account(2)), 1); assert_eq!(Balances::total_balance(&account(2)), 99); // validate types @@ -538,7 +516,6 @@ fn mint_should_work() { account(2), Some(MintWitness { owned_item: Some(43), ..Default::default() }) )); - assert_eq!(AccountBalance::::get(1, account(2)), 1); assert!(events().contains(&Event::::PalletAttributeSet { collection: 0, item: Some(43), @@ -576,8 +553,6 @@ fn transfer_should_work() { default_item_config() )); assert_ok!(Nfts::transfer(RuntimeOrigin::signed(account(2)), 0, 42, account(3))); - assert_eq!(AccountBalance::::get(0, account(2)), 0); - assert_eq!(AccountBalance::::get(0, account(3)), 1); assert_eq!(items(), vec![(account(3), 0, 42)]); assert_noop!( Nfts::transfer(RuntimeOrigin::signed(account(2)), 0, 42, account(4)), @@ -592,8 +567,7 @@ fn transfer_should_work() { None )); assert_ok!(Nfts::transfer(RuntimeOrigin::signed(account(2)), 0, 42, account(4))); - assert_eq!(AccountBalance::::get(0, account(3)), 0); - assert_eq!(AccountBalance::::get(0, account(4)), 1); + // validate we can't transfer non-transferable items let collection_id = 1; assert_ok!(Nfts::force_create( @@ -1856,7 +1830,6 @@ fn burn_works() { assert_ok!(Nfts::burn(RuntimeOrigin::signed(account(5)), 0, 42)); assert_ok!(Nfts::burn(RuntimeOrigin::signed(account(5)), 0, 69)); - assert!(!AccountBalance::::contains_key(0, &account(5))); assert_eq!(Balances::reserved_balance(account(1)), 0); }); } @@ -1933,7 +1906,6 @@ fn approval_lifecycle_works() { #[test] fn cancel_approval_works() { new_test_ext().execute_with(|| { - Balances::make_free_balance_be(&account(2), 100); assert_ok!(Nfts::force_create( RuntimeOrigin::root(), account(1), @@ -1971,24 +1943,6 @@ fn cancel_approval_works() { Error::::NotDelegate ); - // Throws `DelegateApprovalConflict`` if the delegate has been granted a collection - // approval. - assert_ok!(Nfts::approve_collection_transfer( - RuntimeOrigin::signed(account(2)), - 0, - account(3), - None - )); - assert_noop!( - Nfts::cancel_approval(RuntimeOrigin::signed(account(2)), 0, 42, account(3)), - Error::::DelegateApprovalConflict - ); - assert_ok!(Nfts::cancel_collection_approval( - RuntimeOrigin::signed(account(2)), - 0, - account(3) - )); - assert_ok!(Nfts::cancel_approval(RuntimeOrigin::signed(account(2)), 0, 42, account(3))); assert!(events().contains(&Event::::ApprovalCancelled { collection: 0, @@ -2200,7 +2154,6 @@ fn approval_deadline_works() { #[test] fn cancel_approval_works_with_admin() { new_test_ext().execute_with(|| { - Balances::make_free_balance_be(&account(2), 100); assert_ok!(Nfts::force_create( RuntimeOrigin::root(), account(1), @@ -2234,24 +2187,6 @@ fn cancel_approval_works_with_admin() { Error::::NotDelegate ); - // Throws `DelegateApprovalConflict`` if the delegate has been granted a collection - // approval. - assert_ok!(Nfts::approve_collection_transfer( - RuntimeOrigin::signed(account(2)), - 0, - account(3), - None - )); - assert_noop!( - Nfts::cancel_approval(RuntimeOrigin::signed(account(2)), 0, 42, account(3)), - Error::::DelegateApprovalConflict - ); - assert_ok!(Nfts::cancel_collection_approval( - RuntimeOrigin::signed(account(2)), - 0, - account(3) - )); - assert_ok!(Nfts::cancel_approval(RuntimeOrigin::signed(account(2)), 0, 42, account(3))); assert!(events().contains(&Event::::ApprovalCancelled { collection: 0, @@ -2270,7 +2205,6 @@ fn cancel_approval_works_with_admin() { #[test] fn cancel_approval_works_with_force() { new_test_ext().execute_with(|| { - Balances::make_free_balance_be(&account(2), 100); assert_ok!(Nfts::force_create( RuntimeOrigin::root(), account(1), @@ -2304,24 +2238,6 @@ fn cancel_approval_works_with_force() { Error::::NotDelegate ); - // Throws `DelegateApprovalConflict`` if the delegate has been granted a collection - // approval. - assert_ok!(Nfts::approve_collection_transfer( - RuntimeOrigin::signed(account(2)), - 0, - account(3), - None - )); - assert_noop!( - Nfts::cancel_approval(RuntimeOrigin::signed(account(2)), 0, 42, account(3)), - Error::::DelegateApprovalConflict - ); - assert_ok!(Nfts::cancel_collection_approval( - RuntimeOrigin::signed(account(2)), - 0, - account(3) - )); - assert_ok!(Nfts::cancel_approval(RuntimeOrigin::root(), 0, 42, account(3))); assert!(events().contains(&Event::::ApprovalCancelled { collection: 0, @@ -2339,19 +2255,11 @@ fn cancel_approval_works_with_force() { #[test] fn clear_all_transfer_approvals_works() { new_test_ext().execute_with(|| { - Balances::make_free_balance_be(&account(1), 100); assert_ok!(Nfts::force_create( RuntimeOrigin::root(), account(1), default_collection_config() )); - assert_ok!(Nfts::force_mint( - RuntimeOrigin::signed(account(1)), - 0, - 43, - account(1), - default_item_config() - )); assert_ok!(Nfts::force_mint( RuntimeOrigin::signed(account(1)), 0, @@ -2382,25 +2290,6 @@ fn clear_all_transfer_approvals_works() { assert_ok!(Nfts::clear_all_transfer_approvals(RuntimeOrigin::signed(account(2)), 0, 42)); - // Throws `DelegateApprovalConflict` if there are existing collection approvals. - assert_ok!(Nfts::approve_collection_transfer( - RuntimeOrigin::signed(account(1)), - 0, - account(3), - None - )); - assert_noop!( - Nfts::clear_all_transfer_approvals(RuntimeOrigin::signed(account(1)), 0, 42), - Error::::DelegateApprovalConflict - ); - assert_ok!(Nfts::cancel_collection_approval( - RuntimeOrigin::signed(account(1)), - 0, - account(3) - )); - - assert_ok!(Nfts::clear_all_transfer_approvals(RuntimeOrigin::signed(account(2)), 0, 42)); - assert!(events().contains(&Event::::AllApprovalsCancelled { collection: 0, item: 42, @@ -2755,7 +2644,6 @@ fn buy_item_should_work() { item_1, price_1 + 1, )); - assert_eq!(AccountBalance::::get(collection_id, &user_2), 1); // validate the new owner & balances let item = Item::::get(collection_id, item_1).unwrap(); @@ -3213,8 +3101,6 @@ fn claim_swap_should_work() { item_1, Some(price_with_direction.clone()), )); - assert_eq!(AccountBalance::::get(collection_id, &user_1), 2); - assert_eq!(AccountBalance::::get(collection_id, &user_2), 3); // validate the new owner let item = Item::::get(collection_id, item_1).unwrap(); @@ -4141,6 +4027,72 @@ fn collection_item_works() { }); } +#[test] +fn clear_all_transfer_approvals_with_conflict_returns_error() { + new_test_ext().execute_with(|| { + let collection_id = 0; + let delegate = account(3); + let item_id = 42; + let owner = account(1); + + Balances::make_free_balance_be(&owner, 100); + assert_ok!(Nfts::force_create( + RuntimeOrigin::root(), + owner.clone(), + default_collection_config() + )); + assert_ok!(Nfts::force_mint( + RuntimeOrigin::signed(owner.clone()), + collection_id, + item_id, + owner.clone(), + default_item_config() + )); + + assert_ok!(Nfts::approve_transfer( + RuntimeOrigin::signed(owner.clone()), + collection_id, + item_id, + delegate.clone(), + None + )); + + // Throws `DelegateApprovalConflict` if there are existing collection approvals. + assert_ok!(Nfts::approve_collection_transfer( + RuntimeOrigin::signed(owner.clone()), + collection_id, + delegate.clone(), + None + )); + assert_noop!( + Nfts::clear_all_transfer_approvals( + RuntimeOrigin::signed(owner.clone()), + collection_id, + item_id + ), + Error::::DelegateApprovalConflict + ); + assert_ok!(Nfts::cancel_collection_approval( + RuntimeOrigin::signed(owner.clone()), + collection_id, + delegate + )); + + assert_ok!(Nfts::clear_all_transfer_approvals( + RuntimeOrigin::signed(owner.clone()), + collection_id, + item_id + )); + + assert!(events().contains(&Event::::AllApprovalsCancelled { + collection: collection_id, + item: item_id, + owner, + })); + assert_eq!(approvals(collection_id, item_id), vec![]); + }); +} + #[test] fn clear_collection_approvals_works() { new_test_ext().execute_with(|| { @@ -4427,6 +4379,78 @@ fn approve_collection_transfer_works() { }); } +#[test] +fn cancel_approval_with_conflict_returns_error() { + new_test_ext().execute_with(|| { + let collection_id = 0; + let delegate = account(3); + let item_id = 42; + let owner = account(1); + + Balances::make_free_balance_be(&owner, 100); + assert_ok!(Nfts::force_create( + RuntimeOrigin::root(), + owner.clone(), + default_collection_config() + )); + assert_ok!(Nfts::force_mint( + RuntimeOrigin::signed(account(1)), + collection_id, + item_id, + owner.clone(), + default_item_config() + )); + + assert_ok!(Nfts::approve_transfer( + RuntimeOrigin::signed(owner.clone()), + collection_id, + item_id, + delegate.clone(), + None + )); + + // Throws `DelegateApprovalConflict`` if the delegate has been granted a collection + // approval. + assert_ok!(Nfts::approve_collection_transfer( + RuntimeOrigin::signed(owner.clone()), + collection_id, + delegate.clone(), + None + )); + assert_noop!( + Nfts::cancel_approval( + RuntimeOrigin::signed(owner.clone()), + collection_id, + item_id, + delegate.clone() + ), + Error::::DelegateApprovalConflict + ); + assert_ok!(Nfts::cancel_collection_approval( + RuntimeOrigin::signed(owner.clone()), + collection_id, + delegate.clone() + )); + + assert_ok!(Nfts::cancel_approval( + RuntimeOrigin::signed(owner.clone()), + collection_id, + item_id, + delegate.clone() + )); + assert!(events().contains(&Event::::ApprovalCancelled { + collection: collection_id, + item: Some(item_id), + owner: owner.clone(), + delegate: delegate.clone() + })); + assert_noop!( + Nfts::cancel_approval(RuntimeOrigin::signed(owner), collection_id, item_id, delegate), + Error::::NotDelegate + ); + }); +} + #[test] fn cancel_collection_approval_works() { new_test_ext().execute_with(|| { @@ -4729,11 +4753,288 @@ fn check_approval_with_deadline_works() { } #[test] -fn ensure_collection_approval_bytes() { - let key = Blake2_128Concat::max_len::<::CollectionId>() + - Blake2_128Concat::max_len::() + - Blake2_128Concat::max_len::(); - let value = Option::>::max_encoded_len() - .saturating_add(BalanceOf::::max_encoded_len()); - assert_eq!(key + value, 133); +fn mint_should_update_account_balance_works() { + new_test_ext().execute_with(|| { + let collection_id = 0; + let owner = account(1); + + assert_ok!(Nfts::force_create( + RuntimeOrigin::root(), + owner.clone(), + default_collection_config() + )); + + assert_ok!(Nfts::mint( + RuntimeOrigin::signed(owner.clone()), + collection_id, + 42, + owner.clone(), + None + )); + assert_eq!(AccountBalance::get(collection_id, &owner), 1); + + // Additive. + assert_ok!(Nfts::mint( + RuntimeOrigin::signed(owner.clone()), + collection_id, + 43, + owner.clone(), + None + )); + assert_eq!(AccountBalance::get(collection_id, &owner), 2); + + // Mint with witness data. + assert_ok!(Nfts::mint( + RuntimeOrigin::signed(account(1)), + collection_id, + 44, + account(2), + Some(MintWitness { mint_price: Some(1), ..Default::default() }) + )); + assert_eq!(AccountBalance::get(collection_id, account(2)), 1); + }); +} + +#[test] +fn burn_should_update_account_balance_works() { + new_test_ext().execute_with(|| { + let collection_id = 0; + let owner = account(1); + + Balances::make_free_balance_be(&owner, 100); + assert_ok!(Nfts::force_create( + RuntimeOrigin::root(), + owner.clone(), + collection_config_with_all_settings_enabled() + )); + assert_ok!(Nfts::mint( + RuntimeOrigin::signed(owner.clone()), + collection_id, + 42, + owner.clone(), + None, + )); + assert_ok!(Nfts::mint( + RuntimeOrigin::signed(owner.clone()), + collection_id, + 43, + owner.clone(), + None, + )); + + assert_ok!(Nfts::burn(RuntimeOrigin::signed(owner.clone()), collection_id, 42)); + assert_eq!(AccountBalance::get(collection_id, &owner), 1); + + assert_ok!(Nfts::burn(RuntimeOrigin::signed(owner.clone()), collection_id, 43)); + assert!(!AccountBalance::contains_key(collection_id, &owner)); + }); +} + +#[test] +fn transfer_should_update_account_balance_works() { + new_test_ext().execute_with(|| { + let collection_id = 0; + let dest = account(3); + let item_id = 42; + let owner = account(1); + + assert_ok!(Nfts::force_create( + RuntimeOrigin::root(), + owner.clone(), + default_collection_config() + )); + assert_ok!(Nfts::force_mint( + RuntimeOrigin::signed(owner.clone()), + collection_id, + item_id, + owner.clone(), + default_item_config() + )); + assert_ok!(Nfts::transfer( + RuntimeOrigin::signed(owner.clone()), + collection_id, + item_id, + dest.clone() + )); + assert!(!AccountBalance::contains_key(collection_id, &owner)); + assert_eq!(AccountBalance::get(collection_id, &dest), 1); + + assert_ok!(Nfts::approve_transfer( + RuntimeOrigin::signed(dest.clone()), + collection_id, + item_id, + owner.clone(), + None + )); + assert_ok!(Nfts::transfer( + RuntimeOrigin::signed(owner.clone()), + collection_id, + item_id, + account(4) + )); + assert!(!AccountBalance::contains_key(collection_id, &dest)); + assert_eq!(AccountBalance::get(collection_id, account(4)), 1); + }); +} + +#[test] +fn claim_swap_should_update_account_balance_works() { + new_test_ext().execute_with(|| { + System::set_block_number(1); + let user_1 = account(1); + let user_2 = account(2); + let collection_id = 0; + let item_1 = 1; + let item_2 = 2; + let price = 100; + let price_with_direction = + PriceWithDirection { amount: price, direction: PriceDirection::Receive.clone() }; + + Balances::make_free_balance_be(&user_1, 1000); + Balances::make_free_balance_be(&user_2, 1000); + + assert_ok!(Nfts::force_create( + RuntimeOrigin::root(), + user_1.clone(), + default_collection_config() + )); + + assert_ok!(Nfts::mint( + RuntimeOrigin::signed(user_1.clone()), + collection_id, + item_1, + user_1.clone(), + None, + )); + assert_ok!(Nfts::force_mint( + RuntimeOrigin::signed(user_1.clone()), + collection_id, + item_2, + user_2.clone(), + default_item_config(), + )); + assert_ok!(Nfts::create_swap( + RuntimeOrigin::signed(user_1.clone()), + collection_id, + item_1, + collection_id, + Some(item_2), + Some(price_with_direction.clone()), + 2 + )); + + assert_ok!(Nfts::claim_swap( + RuntimeOrigin::signed(user_2.clone()), + collection_id, + item_2, + collection_id, + item_1, + Some(price_with_direction), + )); + assert_eq!(AccountBalance::get(collection_id, &user_1), 1); + assert_eq!(AccountBalance::get(collection_id, &user_2), 1); + }); +} + +#[test] +fn buy_item_should_update_account_balance_works() { + new_test_ext().execute_with(|| { + let user_1 = account(1); + let user_2 = account(2); + let collection_id = 0; + let item_1 = 1; + let price_1 = 20; + let initial_balance = 100; + + Balances::make_free_balance_be(&user_1, initial_balance); + Balances::make_free_balance_be(&user_2, initial_balance); + + assert_ok!(Nfts::force_create( + RuntimeOrigin::root(), + user_1.clone(), + default_collection_config() + )); + + assert_ok!(Nfts::mint( + RuntimeOrigin::signed(user_1.clone()), + collection_id, + item_1, + user_1.clone(), + None + )); + + assert_ok!(Nfts::set_price( + RuntimeOrigin::signed(user_1.clone()), + collection_id, + item_1, + Some(price_1), + None, + )); + + assert_ok!(Nfts::buy_item( + RuntimeOrigin::signed(user_2.clone()), + collection_id, + item_1, + price_1 + 1, + )); + assert_eq!(AccountBalance::get(collection_id, &user_2), 1); + }); +} + +#[test] +fn destroy_with_collection_approvals_returns_error() { + new_test_ext().execute_with(|| { + let collection_id = 0; + let collection_owner = account(1); + let delegate = account(3); + let item_id = 42; + let item_owner = account(2); + + Balances::make_free_balance_be(&collection_owner, 100); + Balances::make_free_balance_be(&item_owner, 100); + assert_ok!(Nfts::create( + RuntimeOrigin::signed(collection_owner.clone()), + collection_owner.clone(), + collection_config_with_all_settings_enabled() + )); + + assert_ok!(Nfts::mint( + RuntimeOrigin::signed(collection_owner.clone()), + collection_id, + item_id, + item_owner.clone(), + None + )); + assert_ok!(Nfts::approve_collection_transfer( + RuntimeOrigin::signed(item_owner.clone()), + collection_id, + delegate.clone(), + None + )); + assert_ok!(Nfts::burn(RuntimeOrigin::signed(item_owner.clone()), collection_id, item_id)); + + // Throws error `CollectionApprovalsExist`. + assert_noop!( + Nfts::destroy( + RuntimeOrigin::signed(collection_owner.clone()), + collection_id, + Nfts::get_destroy_witness(&collection_id).unwrap() + ), + Error::::CollectionApprovalsExist + ); + assert_ok!(Nfts::cancel_collection_approval( + RuntimeOrigin::signed(item_owner.clone()), + collection_id, + delegate + )); + + // Successfully destroy the collection. + assert_ok!(Nfts::destroy( + RuntimeOrigin::signed(collection_owner.clone()), + collection_id, + Nfts::get_destroy_witness(&collection_id).unwrap() + )); + assert!(!ItemConfigOf::::contains_key(collection_id, item_id)); + assert_eq!(ItemConfigOf::::iter_prefix(collection_id).count() as u32, collection_id); + }); } diff --git a/runtime/devnet/src/config/assets.rs b/runtime/devnet/src/config/assets.rs index 0ff2a0cf..e46d7dbc 100644 --- a/runtime/devnet/src/config/assets.rs +++ b/runtime/devnet/src/config/assets.rs @@ -30,8 +30,8 @@ parameter_types! { parameter_types! { pub NftsPalletFeatures: PalletFeatures = PalletFeatures::all_enabled(); pub const NftsCollectionDeposit: Balance = 10 * UNIT; - // Key = 116 bytes (4+16+32+16+32+16), Value = 17 bytes (1+8+8) - pub const NftsCollectionApprovalDeposit: Balance = deposit(1, 133); + // Key = 116 bytes (4+16+32+16+32+16), Value = 21 bytes (1+4+16) + pub const NftsCollectionApprovalDeposit: Balance = deposit(1, 137); pub const NftsItemDeposit: Balance = UNIT / 100; pub const NftsMetadataDepositBase: Balance = deposit(1, 129); pub const NftsAttributeDepositBase: Balance = deposit(1, 0); @@ -123,3 +123,19 @@ impl pallet_assets::Config for Runtime { type StringLimit = AssetsStringLimit; type WeightInfo = pallet_assets::weights::SubstrateWeight; } + +#[cfg(test)] +mod tests { + use frame_support::traits::StorageInfoTrait; + + use super::*; + + #[test] + fn ensure_collection_approval_deposit() { + let max_size = pallet_nfts::CollectionApprovals::::storage_info() + .first() + .and_then(|info| info.max_size) + .unwrap_or_default(); + assert_eq!(deposit(1, max_size), NftsCollectionApprovalDeposit::get()); + } +} diff --git a/runtime/testnet/src/config/assets.rs b/runtime/testnet/src/config/assets.rs index 0ff2a0cf..e46d7dbc 100644 --- a/runtime/testnet/src/config/assets.rs +++ b/runtime/testnet/src/config/assets.rs @@ -30,8 +30,8 @@ parameter_types! { parameter_types! { pub NftsPalletFeatures: PalletFeatures = PalletFeatures::all_enabled(); pub const NftsCollectionDeposit: Balance = 10 * UNIT; - // Key = 116 bytes (4+16+32+16+32+16), Value = 17 bytes (1+8+8) - pub const NftsCollectionApprovalDeposit: Balance = deposit(1, 133); + // Key = 116 bytes (4+16+32+16+32+16), Value = 21 bytes (1+4+16) + pub const NftsCollectionApprovalDeposit: Balance = deposit(1, 137); pub const NftsItemDeposit: Balance = UNIT / 100; pub const NftsMetadataDepositBase: Balance = deposit(1, 129); pub const NftsAttributeDepositBase: Balance = deposit(1, 0); @@ -123,3 +123,19 @@ impl pallet_assets::Config for Runtime { type StringLimit = AssetsStringLimit; type WeightInfo = pallet_assets::weights::SubstrateWeight; } + +#[cfg(test)] +mod tests { + use frame_support::traits::StorageInfoTrait; + + use super::*; + + #[test] + fn ensure_collection_approval_deposit() { + let max_size = pallet_nfts::CollectionApprovals::::storage_info() + .first() + .and_then(|info| info.max_size) + .unwrap_or_default(); + assert_eq!(deposit(1, max_size), NftsCollectionApprovalDeposit::get()); + } +}