difftreelog
Merge pull request #299 from UniqueNetwork/feature/CORE-296
in: master
CORE-296 Add error "NotSufficientFounds" and test it.
3 files changed
pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -318,6 +318,9 @@
AddressIsZero,
/// Target collection doesn't supports this operation
UnsupportedOperation,
+
+ /// Not sufficient founds to perform action
+ NotSufficientFounds,
}
#[pallet::storage]
@@ -470,7 +473,7 @@
WithdrawReasons::TRANSFER,
ExistenceRequirement::KeepAlive,
)
- .map_err(|_| Error::<T>::NoPermission)?;
+ .map_err(|_| Error::<T>::NotSufficientFounds)?;
}
<CreatedCollectionCount<T>>::put(created_count);
pallets/unique/src/mock.rsdiffbeforeafterboth--- a/pallets/unique/src/mock.rs
+++ b/pallets/unique/src/mock.rs
@@ -108,7 +108,7 @@
}
parameter_types! {
- pub const CollectionCreationPrice: u32 = 0;
+ pub const CollectionCreationPrice: u32 = 100;
pub TreasuryAccountId: u64 = 1234;
pub EthereumChainId: u32 = 1111;
}
pallets/unique/src/tests.rsdiffbeforeafterboth1// Tests to be written here2use super::*;3use crate::mock::*;4use crate::{AccessMode, CollectionMode};5use up_data_structs::{6 COLLECTION_NUMBER_LIMIT, CollectionId, CreateItemData, CreateFungibleData, CreateNftData,7 CreateReFungibleData, MAX_DECIMAL_POINTS, COLLECTION_ADMINS_LIMIT, MetaUpdatePermission,8 TokenId, MAX_TOKEN_OWNERSHIP,9};10use frame_support::{assert_noop, assert_ok};11use sp_std::convert::TryInto;1213fn default_nft_data() -> CreateNftData {14 CreateNftData {15 const_data: vec![1, 2, 3].try_into().unwrap(),16 variable_data: vec![3, 2, 1].try_into().unwrap(),17 }18}1920fn default_fungible_data() -> CreateFungibleData {21 CreateFungibleData { value: 5 }22}2324fn default_re_fungible_data() -> CreateReFungibleData {25 CreateReFungibleData {26 const_data: vec![1, 2, 3].try_into().unwrap(),27 variable_data: vec![3, 2, 1].try_into().unwrap(),28 pieces: 1023,29 }30}3132fn create_test_collection_for_owner(33 mode: &CollectionMode,34 owner: u64,35 id: CollectionId,36) -> CollectionId {37 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();38 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();39 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();4041 let origin1 = Origin::signed(owner);42 assert_ok!(TemplateModule::create_collection(43 origin1,44 col_name1.try_into().unwrap(),45 col_desc1.try_into().unwrap(),46 token_prefix1.try_into().unwrap(),47 mode.clone()48 ));4950 let saved_col_name: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();51 let saved_description: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();52 let saved_prefix: Vec<u8> = b"token_prefix1\0".to_vec();53 assert_eq!(54 <pallet_common::CollectionById<Test>>::get(id)55 .unwrap()56 .owner,57 owner58 );59 assert_eq!(60 <pallet_common::CollectionById<Test>>::get(id).unwrap().name,61 saved_col_name62 );63 assert_eq!(64 <pallet_common::CollectionById<Test>>::get(id).unwrap().mode,65 *mode66 );67 assert_eq!(68 <pallet_common::CollectionById<Test>>::get(id)69 .unwrap()70 .description,71 saved_description72 );73 assert_eq!(74 <pallet_common::CollectionById<Test>>::get(id)75 .unwrap()76 .token_prefix,77 saved_prefix78 );79 id80}8182fn create_test_collection(mode: &CollectionMode, id: CollectionId) -> CollectionId {83 create_test_collection_for_owner(&mode, 1, id)84}8586fn create_test_item(collection_id: CollectionId, data: &CreateItemData) {87 let origin1 = Origin::signed(1);88 assert_ok!(TemplateModule::create_item(89 origin1,90 collection_id,91 account(1),92 data.clone()93 ));94}9596fn account(sub: u64) -> TestCrossAccountId {97 TestCrossAccountId::from_sub(sub)98}99100// Use cases tests region101// #region102103#[test]104fn set_version_schema() {105 new_test_ext().execute_with(|| {106 let origin1 = Origin::signed(1);107 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));108109 assert_ok!(TemplateModule::set_schema_version(110 origin1,111 collection_id,112 SchemaVersion::Unique113 ));114 assert_eq!(115 <pallet_common::CollectionById<Test>>::get(collection_id)116 .unwrap()117 .schema_version,118 SchemaVersion::Unique119 );120 });121}122123#[test]124fn create_fungible_collection_fails_with_large_decimal_numbers() {125 new_test_ext().execute_with(|| {126 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();127 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();128 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();129130 let origin1 = Origin::signed(1);131 assert_noop!(132 TemplateModule::create_collection(133 origin1,134 col_name1.try_into().unwrap(),135 col_desc1.try_into().unwrap(),136 token_prefix1.try_into().unwrap(),137 CollectionMode::Fungible(MAX_DECIMAL_POINTS + 1)138 ),139 Error::<Test>::CollectionDecimalPointLimitExceeded140 );141 });142}143144#[test]145fn create_nft_item() {146 new_test_ext().execute_with(|| {147 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));148149 let data = default_nft_data();150 create_test_item(collection_id, &data.clone().into());151152 let item = <pallet_nonfungible::TokenData<Test>>::get((collection_id, 1)).unwrap();153 assert_eq!(item.const_data, data.const_data.into_inner());154 assert_eq!(item.variable_data, data.variable_data.into_inner());155 });156}157158// Use cases tests region159// #region160#[test]161fn create_nft_multiple_items() {162 new_test_ext().execute_with(|| {163 create_test_collection(&CollectionMode::NFT, CollectionId(1));164165 let origin1 = Origin::signed(1);166167 let items_data = vec![default_nft_data(), default_nft_data(), default_nft_data()];168169 assert_ok!(TemplateModule::create_multiple_items(170 origin1,171 CollectionId(1),172 account(1),173 items_data174 .clone()175 .into_iter()176 .map(|d| { d.into() })177 .collect()178 ));179 for (index, data) in items_data.into_iter().enumerate() {180 let item = <pallet_nonfungible::TokenData<Test>>::get((181 CollectionId(1),182 TokenId((index + 1) as u32),183 ))184 .unwrap();185 assert_eq!(item.const_data.to_vec(), data.const_data.into_inner());186 assert_eq!(item.variable_data.to_vec(), data.variable_data.into_inner());187 }188 });189}190191#[test]192fn create_refungible_item() {193 new_test_ext().execute_with(|| {194 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));195196 let data = default_re_fungible_data();197 create_test_item(collection_id, &data.clone().into());198 let item = <pallet_refungible::TokenData<Test>>::get((collection_id, TokenId(1)));199 let balance =200 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1)));201 assert_eq!(item.const_data, data.const_data.into_inner());202 assert_eq!(item.variable_data, data.variable_data.into_inner());203 assert_eq!(balance, 1023);204 });205}206207#[test]208fn create_multiple_refungible_items() {209 new_test_ext().execute_with(|| {210 create_test_collection(&CollectionMode::ReFungible, CollectionId(1));211212 let origin1 = Origin::signed(1);213214 let items_data = vec![215 default_re_fungible_data(),216 default_re_fungible_data(),217 default_re_fungible_data(),218 ];219220 assert_ok!(TemplateModule::create_multiple_items(221 origin1,222 CollectionId(1),223 account(1),224 items_data225 .clone()226 .into_iter()227 .map(|d| { d.into() })228 .collect()229 ));230 for (index, data) in items_data.into_iter().enumerate() {231 let item = <pallet_refungible::TokenData<Test>>::get((232 CollectionId(1),233 TokenId((index + 1) as u32),234 ));235 let balance =236 <pallet_refungible::Balance<Test>>::get((CollectionId(1), TokenId(1), account(1)));237 assert_eq!(item.const_data.to_vec(), data.const_data.into_inner());238 assert_eq!(item.variable_data.to_vec(), data.variable_data.into_inner());239 assert_eq!(balance, 1023);240 }241 });242}243244#[test]245fn create_fungible_item() {246 new_test_ext().execute_with(|| {247 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));248249 let data = default_fungible_data();250 create_test_item(collection_id, &data.into());251252 assert_eq!(253 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),254 5255 );256 });257}258259//#[test]260// fn create_multiple_fungible_items() {261// new_test_ext().execute_with(|| {262// default_limits();263264// create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));265266// let origin1 = Origin::signed(1);267268// let items_data = vec![default_fungible_data(), default_fungible_data(), default_fungible_data()];269270// assert_ok!(TemplateModule::create_multiple_items(271// origin1.clone(),272// 1,273// 1,274// items_data.clone().into_iter().map(|d| { d.into() }).collect()275// ));276277// for (index, _) in items_data.iter().enumerate() {278// assert_eq!(TemplateModule::fungible_item_id(1, (index + 1) as TokenId).value, 5);279// }280// assert_eq!(TemplateModule::balance_count(1, 1), 3000);281// assert_eq!(TemplateModule::address_tokens(1, 1), [1, 2, 3]);282// });283// }284285#[test]286fn transfer_fungible_item() {287 new_test_ext().execute_with(|| {288 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));289290 let origin1 = Origin::signed(1);291 let origin2 = Origin::signed(2);292293 let data = default_fungible_data();294 create_test_item(collection_id, &data.into());295296 assert_eq!(297 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(1))),298 5299 );300301 // change owner scenario302 assert_ok!(TemplateModule::transfer(303 origin1,304 account(2),305 CollectionId(1),306 TokenId(0),307 5308 ));309 assert_eq!(310 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(1))),311 0312 );313314 // split item scenario315 assert_ok!(TemplateModule::transfer(316 origin2.clone(),317 account(3),318 CollectionId(1),319 TokenId(0),320 3321 ));322323 // split item and new owner has account scenario324 assert_ok!(TemplateModule::transfer(325 origin2,326 account(3),327 CollectionId(1),328 TokenId(0),329 1330 ));331 assert_eq!(332 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(2))),333 1334 );335 assert_eq!(336 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(3))),337 4338 );339 });340}341342#[test]343fn transfer_refungible_item() {344 new_test_ext().execute_with(|| {345 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));346347 // Create RFT 1 in 1023 pieces for account 1348 let data = default_re_fungible_data();349 create_test_item(collection_id, &data.clone().into());350 let item = <pallet_refungible::TokenData<Test>>::get((collection_id, TokenId(1)));351 assert_eq!(item.const_data, data.const_data.into_inner());352 assert_eq!(item.variable_data, data.variable_data.into_inner());353 assert_eq!(354 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),355 1356 );357 assert_eq!(358 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),359 1023360 );361 assert_eq!(362 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),363 true364 );365366 // Account 1 transfers all 1023 pieces of RFT 1 to account 2367 let origin1 = Origin::signed(1);368 let origin2 = Origin::signed(2);369 assert_ok!(TemplateModule::transfer(370 origin1,371 account(2),372 CollectionId(1),373 TokenId(1),374 1023375 ));376 assert_eq!(377 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(2))),378 1023379 );380 assert_eq!(381 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),382 0383 );384 assert_eq!(385 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(2))),386 1387 );388 assert_eq!(389 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),390 false391 );392 assert_eq!(393 <pallet_refungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),394 true395 );396397 // Account 2 transfers 500 pieces of RFT 1 to account 3398 assert_ok!(TemplateModule::transfer(399 origin2.clone(),400 account(3),401 CollectionId(1),402 TokenId(1),403 500404 ));405 assert_eq!(406 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(2))),407 523408 );409 assert_eq!(410 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(3))),411 500412 );413 assert_eq!(414 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(2))),415 1416 );417 assert_eq!(418 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(3))),419 1420 );421 assert_eq!(422 <pallet_refungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),423 true424 );425 assert_eq!(426 <pallet_refungible::Owned<Test>>::get((collection_id, account(3), TokenId(1))),427 true428 );429430 // Account 2 transfers 200 more pieces of RFT 1 to account 3 with pre-existing balance431 assert_ok!(TemplateModule::transfer(432 origin2,433 account(3),434 CollectionId(1),435 TokenId(1),436 200437 ));438 assert_eq!(439 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(2))),440 323441 );442 assert_eq!(443 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(3))),444 700445 );446 assert_eq!(447 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(2))),448 1449 );450 assert_eq!(451 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(3))),452 1453 );454 assert_eq!(455 <pallet_refungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),456 true457 );458 assert_eq!(459 <pallet_refungible::Owned<Test>>::get((collection_id, account(3), TokenId(1))),460 true461 );462 });463}464465#[test]466fn transfer_nft_item() {467 new_test_ext().execute_with(|| {468 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));469470 let data = default_nft_data();471 create_test_item(collection_id, &data.into());472 assert_eq!(473 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),474 1475 );476 assert_eq!(477 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),478 true479 );480481 let origin1 = Origin::signed(1);482 // default scenario483 assert_ok!(TemplateModule::transfer(484 origin1,485 account(2),486 CollectionId(1),487 TokenId(1),488 1489 ));490 assert_eq!(491 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),492 0493 );494 assert_eq!(495 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(2))),496 1497 );498 assert_eq!(499 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),500 false501 );502 assert_eq!(503 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),504 true505 );506 });507}508509#[test]510fn transfer_nft_item_wrong_value() {511 new_test_ext().execute_with(|| {512 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));513514 let data = default_nft_data();515 create_test_item(collection_id, &data.into());516 assert_eq!(517 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),518 1519 );520 assert_eq!(521 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),522 true523 );524525 let origin1 = Origin::signed(1);526527 assert_noop!(528 TemplateModule::transfer(origin1, account(2), CollectionId(1), TokenId(1), 2)529 .map_err(|e| e.error),530 <pallet_nonfungible::Error::<Test>>::NonfungibleItemsHaveNoAmount531 );532 });533}534535#[test]536fn transfer_nft_item_zero_value() {537 new_test_ext().execute_with(|| {538 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));539540 let data = default_nft_data();541 create_test_item(collection_id, &data.into());542 assert_eq!(543 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),544 1545 );546 assert_eq!(547 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),548 true549 );550551 let origin1 = Origin::signed(1);552553 // Transferring 0 amount works on NFT...554 assert_ok!(TemplateModule::transfer(555 origin1,556 account(2),557 CollectionId(1),558 TokenId(1),559 0560 ));561 // ... and results in no transfer562 assert_eq!(563 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),564 1565 );566 assert_eq!(567 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),568 true569 );570 });571}572573#[test]574fn nft_approve_and_transfer_from() {575 new_test_ext().execute_with(|| {576 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));577578 let data = default_nft_data();579 create_test_item(collection_id, &data.into());580581 let origin1 = Origin::signed(1);582 let origin2 = Origin::signed(2);583584 assert_eq!(585 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),586 1587 );588 assert_eq!(589 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),590 true591 );592593 // neg transfer_from594 assert_noop!(595 TemplateModule::transfer_from(596 origin2.clone(),597 account(1),598 account(2),599 CollectionId(1),600 TokenId(1),601 1602 )603 .map_err(|e| e.error),604 CommonError::<Test>::ApprovedValueTooLow605 );606607 // do approve608 assert_ok!(TemplateModule::approve(609 origin1,610 account(2),611 CollectionId(1),612 TokenId(1),613 1614 ));615 assert_eq!(616 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),617 account(2)618 );619620 assert_ok!(TemplateModule::transfer_from(621 origin2,622 account(1),623 account(3),624 CollectionId(1),625 TokenId(1),626 1627 ));628 assert!(629 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).is_none()630 );631 });632}633634#[test]635fn nft_approve_and_transfer_from_allow_list() {636 new_test_ext().execute_with(|| {637 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));638639 let origin1 = Origin::signed(1);640 let origin2 = Origin::signed(2);641642 // Create NFT 1 for account 1643 let data = default_nft_data();644 create_test_item(collection_id, &data.clone().into());645 assert_eq!(646 &<pallet_nonfungible::TokenData<Test>>::get((collection_id, TokenId(1)))647 .unwrap()648 .const_data,649 &data.const_data.into_inner()650 );651 assert_eq!(652 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),653 1654 );655 assert_eq!(656 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),657 true658 );659660 // Allow allow-list users to mint and add accounts 1, 2, and 3 to allow-list661 assert_ok!(TemplateModule::set_mint_permission(662 origin1.clone(),663 CollectionId(1),664 true665 ));666 assert_ok!(TemplateModule::set_public_access_mode(667 origin1.clone(),668 CollectionId(1),669 AccessMode::AllowList670 ));671 assert_ok!(TemplateModule::add_to_allow_list(672 origin1.clone(),673 CollectionId(1),674 account(1)675 ));676 assert_ok!(TemplateModule::add_to_allow_list(677 origin1.clone(),678 CollectionId(1),679 account(2)680 ));681 assert_ok!(TemplateModule::add_to_allow_list(682 origin1.clone(),683 CollectionId(1),684 account(3)685 ));686687 // Account 1 approves account 2 for NFT 1688 assert_ok!(TemplateModule::approve(689 origin1.clone(),690 account(2),691 CollectionId(1),692 TokenId(1),693 1694 ));695 assert_eq!(696 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),697 account(2)698 );699700 // Account 2 transfers NFT 1 from account 1 to account 3701 assert_ok!(TemplateModule::transfer_from(702 origin2,703 account(1),704 account(3),705 CollectionId(1),706 TokenId(1),707 1708 ));709 assert!(710 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).is_none()711 );712 });713}714715#[test]716fn refungible_approve_and_transfer_from() {717 new_test_ext().execute_with(|| {718 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));719720 let origin1 = Origin::signed(1);721 let origin2 = Origin::signed(2);722723 // Create RFT 1 in 1023 pieces for account 1724 let data = default_re_fungible_data();725 create_test_item(collection_id, &data.into());726727 assert_eq!(728 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),729 1730 );731 assert_eq!(732 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),733 1023734 );735 assert_eq!(736 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),737 true738 );739740 // Allow public minting, enable allow-list and add accounts 1, 2, 3 to allow-list741 assert_ok!(TemplateModule::set_mint_permission(742 origin1.clone(),743 CollectionId(1),744 true745 ));746 assert_ok!(TemplateModule::set_public_access_mode(747 origin1.clone(),748 CollectionId(1),749 AccessMode::AllowList750 ));751 assert_ok!(TemplateModule::add_to_allow_list(752 origin1.clone(),753 CollectionId(1),754 account(1)755 ));756 assert_ok!(TemplateModule::add_to_allow_list(757 origin1.clone(),758 CollectionId(1),759 account(2)760 ));761 assert_ok!(TemplateModule::add_to_allow_list(762 origin1.clone(),763 CollectionId(1),764 account(3)765 ));766767 // Account 1 approves account 2 for 1023 pieces of RFT 1768 assert_ok!(TemplateModule::approve(769 origin1,770 account(2),771 CollectionId(1),772 TokenId(1),773 1023774 ));775 assert_eq!(776 <pallet_refungible::Allowance<Test>>::get((777 CollectionId(1),778 TokenId(1),779 account(1),780 account(2)781 )),782 1023783 );784785 // Account 2 transfers 100 pieces of RFT 1 from account 1 to account 3786 assert_ok!(TemplateModule::transfer_from(787 origin2,788 account(1),789 account(3),790 CollectionId(1),791 TokenId(1),792 100793 ));794 assert_eq!(795 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),796 1797 );798 assert_eq!(799 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(3))),800 1801 );802 assert_eq!(803 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),804 923805 );806 assert_eq!(807 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(3))),808 100809 );810 assert_eq!(811 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),812 true813 );814 assert_eq!(815 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),816 true817 );818 assert_eq!(819 <pallet_refungible::Allowance<Test>>::get((820 CollectionId(1),821 TokenId(1),822 account(1),823 account(2)824 )),825 923826 );827 });828}829830#[test]831fn fungible_approve_and_transfer_from() {832 new_test_ext().execute_with(|| {833 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));834835 let data = default_fungible_data();836 create_test_item(collection_id, &data.into());837838 let origin1 = Origin::signed(1);839 let origin2 = Origin::signed(2);840841 assert_ok!(TemplateModule::set_mint_permission(842 origin1.clone(),843 CollectionId(1),844 true845 ));846 assert_ok!(TemplateModule::set_public_access_mode(847 origin1.clone(),848 CollectionId(1),849 AccessMode::AllowList850 ));851 assert_ok!(TemplateModule::add_to_allow_list(852 origin1.clone(),853 CollectionId(1),854 account(1)855 ));856 assert_ok!(TemplateModule::add_to_allow_list(857 origin1.clone(),858 CollectionId(1),859 account(2)860 ));861 assert_ok!(TemplateModule::add_to_allow_list(862 origin1.clone(),863 CollectionId(1),864 account(3)865 ));866867 // do approve868 assert_ok!(TemplateModule::approve(869 origin1.clone(),870 account(2),871 CollectionId(1),872 TokenId(0),873 5874 ));875 assert_eq!(876 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(2))),877 5878 );879 assert_ok!(TemplateModule::approve(880 origin1,881 account(3),882 CollectionId(1),883 TokenId(0),884 5885 ));886 assert_eq!(887 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(2))),888 5889 );890 assert_eq!(891 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(3))),892 5893 );894895 assert_ok!(TemplateModule::transfer_from(896 origin2.clone(),897 account(1),898 account(3),899 CollectionId(1),900 TokenId(0),901 4902 ));903904 assert_eq!(905 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(2))),906 1907 );908909 assert_noop!(910 TemplateModule::transfer_from(911 origin2,912 account(1),913 account(3),914 CollectionId(1),915 TokenId(0),916 4917 )918 .map_err(|e| e.error),919 CommonError::<Test>::ApprovedValueTooLow920 );921 });922}923924#[test]925fn change_collection_owner() {926 new_test_ext().execute_with(|| {927 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));928929 let origin1 = Origin::signed(1);930 assert_ok!(TemplateModule::change_collection_owner(931 origin1,932 collection_id,933 2934 ));935 assert_eq!(936 <pallet_common::CollectionById<Test>>::get(collection_id)937 .unwrap()938 .owner,939 2940 );941 });942}943944#[test]945fn destroy_collection() {946 new_test_ext().execute_with(|| {947 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));948949 let origin1 = Origin::signed(1);950 assert_ok!(TemplateModule::destroy_collection(origin1, collection_id));951 });952}953954#[test]955fn burn_nft_item() {956 new_test_ext().execute_with(|| {957 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));958959 let origin1 = Origin::signed(1);960961 let data = default_nft_data();962 create_test_item(collection_id, &data.into());963964 // check balance (collection with id = 1, user id = 1)965 assert_eq!(966 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),967 1968 );969970 // burn item971 assert_ok!(TemplateModule::burn_item(972 origin1.clone(),973 collection_id,974 TokenId(1),975 1976 ));977 assert_eq!(978 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),979 0980 );981 });982}983984#[test]985fn burn_same_nft_item_twice() {986 new_test_ext().execute_with(|| {987 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));988989 let origin1 = Origin::signed(1);990991 let data = default_nft_data();992 create_test_item(collection_id, &data.into());993994 // check balance (collection with id = 1, user id = 1)995 assert_eq!(996 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),997 1998 );9991000 // burn item1001 assert_ok!(TemplateModule::burn_item(1002 origin1.clone(),1003 collection_id,1004 TokenId(1),1005 11006 ));10071008 // burn item again1009 assert_noop!(1010 TemplateModule::burn_item(origin1, collection_id, TokenId(1), 1).map_err(|e| e.error),1011 CommonError::<Test>::TokenNotFound1012 );10131014 assert_eq!(1015 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1016 01017 );1018 });1019}10201021#[test]1022fn burn_fungible_item() {1023 new_test_ext().execute_with(|| {1024 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));10251026 let origin1 = Origin::signed(1);1027 assert_ok!(TemplateModule::add_collection_admin(1028 origin1.clone(),1029 collection_id,1030 account(2)1031 ));10321033 let data = default_fungible_data();1034 create_test_item(collection_id, &data.into());10351036 // check balance (collection with id = 1, user id = 1)1037 assert_eq!(1038 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),1039 51040 );10411042 // burn item1043 assert_ok!(TemplateModule::burn_item(1044 origin1.clone(),1045 CollectionId(1),1046 TokenId(0),1047 51048 ));1049 assert_noop!(1050 TemplateModule::burn_item(origin1, CollectionId(1), TokenId(0), 5).map_err(|e| e.error),1051 CommonError::<Test>::TokenValueTooLow1052 );10531054 assert_eq!(1055 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),1056 01057 );1058 });1059}10601061#[test]1062fn burn_fungible_item_with_token_id() {1063 new_test_ext().execute_with(|| {1064 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));10651066 let origin1 = Origin::signed(1);1067 assert_ok!(TemplateModule::add_collection_admin(1068 origin1.clone(),1069 collection_id,1070 account(2)1071 ));10721073 let data = default_fungible_data();1074 create_test_item(collection_id, &data.into());10751076 // check balance (collection with id = 1, user id = 1)1077 assert_eq!(1078 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),1079 51080 );10811082 // Try to burn item using Token ID1083 assert_noop!(1084 TemplateModule::burn_item(origin1, CollectionId(1), TokenId(1), 5).map_err(|e| e.error),1085 <pallet_fungible::Error::<Test>>::FungibleItemsHaveNoId1086 );1087 });1088}1089#[test]1090fn burn_refungible_item() {1091 new_test_ext().execute_with(|| {1092 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));1093 let origin1 = Origin::signed(1);10941095 assert_ok!(TemplateModule::set_mint_permission(1096 origin1.clone(),1097 collection_id,1098 true1099 ));1100 assert_ok!(TemplateModule::set_public_access_mode(1101 origin1.clone(),1102 collection_id,1103 AccessMode::AllowList1104 ));1105 assert_ok!(TemplateModule::add_to_allow_list(1106 origin1.clone(),1107 collection_id,1108 account(1)1109 ));11101111 assert_ok!(TemplateModule::add_collection_admin(1112 origin1.clone(),1113 collection_id,1114 account(2)1115 ));11161117 let data = default_re_fungible_data();1118 create_test_item(collection_id, &data.into());11191120 // check balance (collection with id = 1, user id = 2)1121 assert_eq!(1122 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),1123 11124 );1125 assert_eq!(1126 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),1127 10231128 );11291130 // burn item1131 assert_ok!(TemplateModule::burn_item(1132 origin1.clone(),1133 collection_id,1134 TokenId(1),1135 10231136 ));1137 assert_noop!(1138 TemplateModule::burn_item(origin1, collection_id, TokenId(1), 1023)1139 .map_err(|e| e.error),1140 CommonError::<Test>::TokenValueTooLow1141 );11421143 assert_eq!(1144 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),1145 01146 );1147 });1148}11491150#[test]1151fn add_collection_admin() {1152 new_test_ext().execute_with(|| {1153 let collection1_id =1154 create_test_collection_for_owner(&CollectionMode::NFT, 1, CollectionId(1));1155 let origin1 = Origin::signed(1);11561157 // Add collection admins1158 assert_ok!(TemplateModule::add_collection_admin(1159 origin1.clone(),1160 collection1_id,1161 account(2)1162 ));1163 assert_ok!(TemplateModule::add_collection_admin(1164 origin1,1165 collection1_id,1166 account(3)1167 ));11681169 // Owner is not an admin by default1170 assert_eq!(1171 <pallet_common::IsAdmin<Test>>::get((CollectionId(1), account(1))),1172 false1173 );1174 assert!(<pallet_common::IsAdmin<Test>>::get((1175 CollectionId(1),1176 account(2)1177 )));1178 assert!(<pallet_common::IsAdmin<Test>>::get((1179 CollectionId(1),1180 account(3)1181 )));1182 });1183}11841185#[test]1186fn remove_collection_admin() {1187 new_test_ext().execute_with(|| {1188 let collection1_id =1189 create_test_collection_for_owner(&CollectionMode::NFT, 1, CollectionId(1));1190 let origin1 = Origin::signed(1);1191 let origin2 = Origin::signed(2);11921193 // Add collection admins 2 and 31194 assert_ok!(TemplateModule::add_collection_admin(1195 origin1.clone(),1196 collection1_id,1197 account(2)1198 ));1199 assert_ok!(TemplateModule::add_collection_admin(1200 origin1,1201 collection1_id,1202 account(3)1203 ));12041205 assert!(<pallet_common::IsAdmin<Test>>::get((1206 CollectionId(1),1207 account(2)1208 )));1209 assert!(<pallet_common::IsAdmin<Test>>::get((1210 CollectionId(1),1211 account(3)1212 )));12131214 // remove admin 31215 assert_ok!(TemplateModule::remove_collection_admin(1216 origin2,1217 CollectionId(1),1218 account(3)1219 ));12201221 // 2 is still admin, 3 is not an admin anymore1222 assert!(<pallet_common::IsAdmin<Test>>::get((1223 CollectionId(1),1224 account(2)1225 )));1226 assert_eq!(1227 <pallet_common::IsAdmin<Test>>::get((CollectionId(1), account(3))),1228 false1229 );1230 });1231}12321233#[test]1234fn balance_of() {1235 new_test_ext().execute_with(|| {1236 let nft_collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1237 let fungible_collection_id =1238 create_test_collection(&CollectionMode::Fungible(3), CollectionId(2));1239 let re_fungible_collection_id =1240 create_test_collection(&CollectionMode::ReFungible, CollectionId(3));12411242 // check balance before1243 assert_eq!(1244 <pallet_nonfungible::AccountBalance<Test>>::get((nft_collection_id, account(1))),1245 01246 );1247 assert_eq!(1248 <pallet_fungible::Balance<Test>>::get((fungible_collection_id, account(1))),1249 01250 );1251 assert_eq!(1252 <pallet_refungible::AccountBalance<Test>>::get((re_fungible_collection_id, account(1))),1253 01254 );12551256 let nft_data = default_nft_data();1257 create_test_item(nft_collection_id, &nft_data.into());12581259 let fungible_data = default_fungible_data();1260 create_test_item(fungible_collection_id, &fungible_data.into());12611262 let re_fungible_data = default_re_fungible_data();1263 create_test_item(re_fungible_collection_id, &re_fungible_data.into());12641265 // check balance (collection with id = 1, user id = 1)1266 assert_eq!(1267 <pallet_nonfungible::AccountBalance<Test>>::get((nft_collection_id, account(1))),1268 11269 );1270 assert_eq!(1271 <pallet_fungible::Balance<Test>>::get((fungible_collection_id, account(1))),1272 51273 );1274 assert_eq!(1275 <pallet_refungible::AccountBalance<Test>>::get((re_fungible_collection_id, account(1))),1276 11277 );12781279 assert_eq!(1280 <pallet_nonfungible::Owned<Test>>::get((nft_collection_id, account(1), TokenId(1))),1281 true1282 );1283 assert_eq!(1284 <pallet_refungible::Owned<Test>>::get((1285 re_fungible_collection_id,1286 account(1),1287 TokenId(1)1288 )),1289 true1290 );1291 });1292}12931294#[test]1295fn approve() {1296 new_test_ext().execute_with(|| {1297 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));12981299 let data = default_nft_data();1300 create_test_item(collection_id, &data.into());13011302 let origin1 = Origin::signed(1);13031304 // approve1305 assert_ok!(TemplateModule::approve(1306 origin1,1307 account(2),1308 CollectionId(1),1309 TokenId(1),1310 11311 ));1312 assert_eq!(1313 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1314 account(2)1315 );1316 });1317}13181319#[test]1320fn transfer_from() {1321 new_test_ext().execute_with(|| {1322 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1323 let origin1 = Origin::signed(1);1324 let origin2 = Origin::signed(2);13251326 let data = default_nft_data();1327 create_test_item(collection_id, &data.into());13281329 // approve1330 assert_ok!(TemplateModule::approve(1331 origin1.clone(),1332 account(2),1333 CollectionId(1),1334 TokenId(1),1335 11336 ));1337 assert_eq!(1338 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1339 account(2)1340 );13411342 assert_ok!(TemplateModule::set_mint_permission(1343 origin1.clone(),1344 CollectionId(1),1345 true1346 ));1347 assert_ok!(TemplateModule::set_public_access_mode(1348 origin1.clone(),1349 CollectionId(1),1350 AccessMode::AllowList1351 ));1352 assert_ok!(TemplateModule::add_to_allow_list(1353 origin1.clone(),1354 CollectionId(1),1355 account(1)1356 ));1357 assert_ok!(TemplateModule::add_to_allow_list(1358 origin1.clone(),1359 CollectionId(1),1360 account(2)1361 ));1362 assert_ok!(TemplateModule::add_to_allow_list(1363 origin1,1364 CollectionId(1),1365 account(3)1366 ));13671368 assert_ok!(TemplateModule::transfer_from(1369 origin2,1370 account(1),1371 account(2),1372 CollectionId(1),1373 TokenId(1),1374 11375 ));13761377 // after transfer1378 assert_eq!(1379 <pallet_nonfungible::AccountBalance<Test>>::get((CollectionId(1), account(1))),1380 01381 );1382 assert_eq!(1383 <pallet_nonfungible::AccountBalance<Test>>::get((CollectionId(1), account(2))),1384 11385 );1386 });1387}13881389// #endregion13901391// Coverage tests region1392// #region13931394#[test]1395fn owner_can_add_address_to_allow_list() {1396 new_test_ext().execute_with(|| {1397 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));13981399 let origin1 = Origin::signed(1);1400 assert_ok!(TemplateModule::add_to_allow_list(1401 origin1,1402 collection_id,1403 account(2)1404 ));1405 assert!(<pallet_common::Allowlist<Test>>::get((1406 collection_id,1407 account(2)1408 )));1409 });1410}14111412#[test]1413fn admin_can_add_address_to_allow_list() {1414 new_test_ext().execute_with(|| {1415 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1416 let origin1 = Origin::signed(1);1417 let origin2 = Origin::signed(2);14181419 assert_ok!(TemplateModule::add_collection_admin(1420 origin1,1421 collection_id,1422 account(2)1423 ));1424 assert_ok!(TemplateModule::add_to_allow_list(1425 origin2,1426 collection_id,1427 account(3)1428 ));1429 assert!(<pallet_common::Allowlist<Test>>::get((1430 collection_id,1431 account(3)1432 )));1433 });1434}14351436#[test]1437fn nonprivileged_user_cannot_add_address_to_allow_list() {1438 new_test_ext().execute_with(|| {1439 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));14401441 let origin2 = Origin::signed(2);1442 assert_noop!(1443 TemplateModule::add_to_allow_list(origin2, collection_id, account(3)),1444 CommonError::<Test>::NoPermission1445 );1446 });1447}14481449#[test]1450fn nobody_can_add_address_to_allow_list_of_nonexisting_collection() {1451 new_test_ext().execute_with(|| {1452 let origin1 = Origin::signed(1);14531454 assert_noop!(1455 TemplateModule::add_to_allow_list(origin1, CollectionId(1), account(2)),1456 CommonError::<Test>::CollectionNotFound1457 );1458 });1459}14601461#[test]1462fn nobody_can_add_address_to_allow_list_of_deleted_collection() {1463 new_test_ext().execute_with(|| {1464 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));14651466 let origin1 = Origin::signed(1);1467 assert_ok!(TemplateModule::destroy_collection(1468 origin1.clone(),1469 collection_id1470 ));1471 assert_noop!(1472 TemplateModule::add_to_allow_list(origin1, collection_id, account(2)),1473 CommonError::<Test>::CollectionNotFound1474 );1475 });1476}14771478// If address is already added to allow list, nothing happens1479#[test]1480fn address_is_already_added_to_allow_list() {1481 new_test_ext().execute_with(|| {1482 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1483 let origin1 = Origin::signed(1);14841485 assert_ok!(TemplateModule::add_to_allow_list(1486 origin1.clone(),1487 collection_id,1488 account(2)1489 ));1490 assert_ok!(TemplateModule::add_to_allow_list(1491 origin1,1492 collection_id,1493 account(2)1494 ));1495 assert!(<pallet_common::Allowlist<Test>>::get((1496 collection_id,1497 account(2)1498 )));1499 });1500}15011502#[test]1503fn owner_can_remove_address_from_allow_list() {1504 new_test_ext().execute_with(|| {1505 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));15061507 let origin1 = Origin::signed(1);1508 assert_ok!(TemplateModule::add_to_allow_list(1509 origin1.clone(),1510 collection_id,1511 account(2)1512 ));1513 assert_ok!(TemplateModule::remove_from_allow_list(1514 origin1,1515 collection_id,1516 account(2)1517 ));1518 assert_eq!(1519 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1520 false1521 );1522 });1523}15241525#[test]1526fn admin_can_remove_address_from_allow_list() {1527 new_test_ext().execute_with(|| {1528 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1529 let origin1 = Origin::signed(1);1530 let origin2 = Origin::signed(2);15311532 // Owner adds admin1533 assert_ok!(TemplateModule::add_collection_admin(1534 origin1.clone(),1535 collection_id,1536 account(2)1537 ));15381539 // Owner adds address 3 to allow list1540 assert_ok!(TemplateModule::add_to_allow_list(1541 origin1,1542 collection_id,1543 account(3)1544 ));15451546 // Admin removes address 3 from allow list1547 assert_ok!(TemplateModule::remove_from_allow_list(1548 origin2,1549 collection_id,1550 account(3)1551 ));1552 assert_eq!(1553 <pallet_common::Allowlist<Test>>::get((collection_id, account(3))),1554 false1555 );1556 });1557}15581559#[test]1560fn nonprivileged_user_cannot_remove_address_from_allow_list() {1561 new_test_ext().execute_with(|| {1562 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1563 let origin1 = Origin::signed(1);1564 let origin2 = Origin::signed(2);15651566 assert_ok!(TemplateModule::add_to_allow_list(1567 origin1,1568 collection_id,1569 account(2)1570 ));1571 assert_noop!(1572 TemplateModule::remove_from_allow_list(origin2, collection_id, account(2)),1573 CommonError::<Test>::NoPermission1574 );1575 assert!(<pallet_common::Allowlist<Test>>::get((1576 collection_id,1577 account(2)1578 )));1579 });1580}15811582#[test]1583fn nobody_can_remove_address_from_allow_list_of_nonexisting_collection() {1584 new_test_ext().execute_with(|| {1585 let origin1 = Origin::signed(1);15861587 assert_noop!(1588 TemplateModule::remove_from_allow_list(origin1, CollectionId(1), account(2)),1589 CommonError::<Test>::CollectionNotFound1590 );1591 });1592}15931594#[test]1595fn nobody_can_remove_address_from_allow_list_of_deleted_collection() {1596 new_test_ext().execute_with(|| {1597 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1598 let origin1 = Origin::signed(1);1599 let origin2 = Origin::signed(2);16001601 // Add account 2 to allow list1602 assert_ok!(TemplateModule::add_to_allow_list(1603 origin1.clone(),1604 collection_id,1605 account(2)1606 ));16071608 // Account 2 is in collection allow-list1609 assert!(<pallet_common::Allowlist<Test>>::get((1610 collection_id,1611 account(2)1612 )));16131614 // Destroy collection1615 assert_ok!(TemplateModule::destroy_collection(origin1, collection_id));16161617 // Attempt to remove account 2 from collection allow-list => error1618 assert_noop!(1619 TemplateModule::remove_from_allow_list(origin2, collection_id, account(2)),1620 CommonError::<Test>::CollectionNotFound1621 );16221623 // Account 2 is not found in collection allow-list anyway1624 assert_eq!(1625 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1626 false1627 );1628 });1629}16301631// If address is already removed from allow list, nothing happens1632#[test]1633fn address_is_already_removed_from_allow_list() {1634 new_test_ext().execute_with(|| {1635 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1636 let origin1 = Origin::signed(1);16371638 assert_ok!(TemplateModule::add_to_allow_list(1639 origin1.clone(),1640 collection_id,1641 account(2)1642 ));1643 assert_ok!(TemplateModule::remove_from_allow_list(1644 origin1.clone(),1645 collection_id,1646 account(2)1647 ));1648 assert_eq!(1649 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1650 false1651 );1652 assert_ok!(TemplateModule::remove_from_allow_list(1653 origin1,1654 collection_id,1655 account(2)1656 ));1657 assert_eq!(1658 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1659 false1660 );1661 });1662}16631664// If Public Access mode is set to AllowList, tokens can’t be transferred from a non-allowlisted address with transfer or transferFrom (2 tests)1665#[test]1666fn allow_list_test_1() {1667 new_test_ext().execute_with(|| {1668 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));16691670 let origin1 = Origin::signed(1);16711672 let data = default_nft_data();1673 create_test_item(collection_id, &data.into());16741675 assert_ok!(TemplateModule::set_public_access_mode(1676 origin1.clone(),1677 collection_id,1678 AccessMode::AllowList1679 ));1680 assert_ok!(TemplateModule::add_to_allow_list(1681 origin1.clone(),1682 collection_id,1683 account(2)1684 ));16851686 assert_noop!(1687 TemplateModule::transfer(origin1, account(3), CollectionId(1), TokenId(1), 1)1688 .map_err(|e| e.error),1689 CommonError::<Test>::AddressNotInAllowlist1690 );1691 });1692}16931694#[test]1695fn allow_list_test_2() {1696 new_test_ext().execute_with(|| {1697 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1698 let origin1 = Origin::signed(1);16991700 let data = default_nft_data();1701 create_test_item(collection_id, &data.into());17021703 assert_ok!(TemplateModule::set_public_access_mode(1704 origin1.clone(),1705 collection_id,1706 AccessMode::AllowList1707 ));1708 assert_ok!(TemplateModule::add_to_allow_list(1709 origin1.clone(),1710 collection_id,1711 account(1)1712 ));1713 assert_ok!(TemplateModule::add_to_allow_list(1714 origin1.clone(),1715 collection_id,1716 account(2)1717 ));17181719 // do approve1720 assert_ok!(TemplateModule::approve(1721 origin1.clone(),1722 account(1),1723 collection_id,1724 TokenId(1),1725 11726 ));1727 assert_eq!(1728 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1729 account(1)1730 );17311732 assert_ok!(TemplateModule::remove_from_allow_list(1733 origin1.clone(),1734 collection_id,1735 account(1)1736 ));17371738 assert_noop!(1739 TemplateModule::transfer_from(1740 origin1,1741 account(1),1742 account(3),1743 CollectionId(1),1744 TokenId(1),1745 11746 )1747 .map_err(|e| e.error),1748 CommonError::<Test>::AddressNotInAllowlist1749 );1750 });1751}17521753// If Public Access mode is set to AllowList, tokens can’t be transferred to a non-allowlisted address with transfer or transferFrom (2 tests)1754#[test]1755fn allow_list_test_3() {1756 new_test_ext().execute_with(|| {1757 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));17581759 let origin1 = Origin::signed(1);17601761 let data = default_nft_data();1762 create_test_item(collection_id, &data.into());17631764 assert_ok!(TemplateModule::set_public_access_mode(1765 origin1.clone(),1766 collection_id,1767 AccessMode::AllowList1768 ));1769 assert_ok!(TemplateModule::add_to_allow_list(1770 origin1.clone(),1771 collection_id,1772 account(1)1773 ));17741775 assert_noop!(1776 TemplateModule::transfer(origin1, account(3), collection_id, TokenId(1), 1)1777 .map_err(|e| e.error),1778 CommonError::<Test>::AddressNotInAllowlist1779 );1780 });1781}17821783#[test]1784fn allow_list_test_4() {1785 new_test_ext().execute_with(|| {1786 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));17871788 let origin1 = Origin::signed(1);17891790 let data = default_nft_data();1791 create_test_item(collection_id, &data.into());17921793 assert_ok!(TemplateModule::set_public_access_mode(1794 origin1.clone(),1795 collection_id,1796 AccessMode::AllowList1797 ));1798 assert_ok!(TemplateModule::add_to_allow_list(1799 origin1.clone(),1800 collection_id,1801 account(1)1802 ));1803 assert_ok!(TemplateModule::add_to_allow_list(1804 origin1.clone(),1805 collection_id,1806 account(2)1807 ));18081809 // do approve1810 assert_ok!(TemplateModule::approve(1811 origin1.clone(),1812 account(1),1813 collection_id,1814 TokenId(1),1815 11816 ));1817 assert_eq!(1818 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1819 account(1)1820 );18211822 assert_ok!(TemplateModule::remove_from_allow_list(1823 origin1.clone(),1824 collection_id,1825 account(2)1826 ));18271828 assert_noop!(1829 TemplateModule::transfer_from(1830 origin1,1831 account(1),1832 account(3),1833 collection_id,1834 TokenId(1),1835 11836 )1837 .map_err(|e| e.error),1838 CommonError::<Test>::AddressNotInAllowlist1839 );1840 });1841}18421843// If Public Access mode is set to AllowList, tokens can’t be destroyed by a non-allowlisted address (even if it owned them before enabling AllowList mode)1844#[test]1845fn allow_list_test_5() {1846 new_test_ext().execute_with(|| {1847 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));18481849 let origin1 = Origin::signed(1);18501851 let data = default_nft_data();1852 create_test_item(collection_id, &data.into());18531854 assert_ok!(TemplateModule::set_public_access_mode(1855 origin1.clone(),1856 collection_id,1857 AccessMode::AllowList1858 ));1859 assert_noop!(1860 TemplateModule::burn_item(origin1.clone(), CollectionId(1), TokenId(1), 1)1861 .map_err(|e| e.error),1862 CommonError::<Test>::AddressNotInAllowlist1863 );1864 });1865}18661867// If Public Access mode is set to AllowList, token transfers can’t be Approved by a non-allowlisted address (see Approve method).1868#[test]1869fn allow_list_test_6() {1870 new_test_ext().execute_with(|| {1871 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));18721873 let origin1 = Origin::signed(1);18741875 let data = default_nft_data();1876 create_test_item(collection_id, &data.into());18771878 assert_ok!(TemplateModule::set_public_access_mode(1879 origin1.clone(),1880 collection_id,1881 AccessMode::AllowList1882 ));18831884 // do approve1885 assert_noop!(1886 TemplateModule::approve(origin1, account(1), CollectionId(1), TokenId(1), 1)1887 .map_err(|e| e.error),1888 CommonError::<Test>::AddressNotInAllowlist1889 );1890 });1891}18921893// If Public Access mode is set to AllowList, tokens can be transferred from a allowlisted address with transfer or transferFrom (2 tests) and1894// tokens can be transferred from a allowlisted address with transfer or transferFrom (2 tests)1895#[test]1896fn allow_list_test_7() {1897 new_test_ext().execute_with(|| {1898 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));18991900 let data = default_nft_data();1901 create_test_item(collection_id, &data.into());19021903 let origin1 = Origin::signed(1);19041905 assert_ok!(TemplateModule::set_public_access_mode(1906 origin1.clone(),1907 collection_id,1908 AccessMode::AllowList1909 ));1910 assert_ok!(TemplateModule::add_to_allow_list(1911 origin1.clone(),1912 collection_id,1913 account(1)1914 ));1915 assert_ok!(TemplateModule::add_to_allow_list(1916 origin1.clone(),1917 collection_id,1918 account(2)1919 ));19201921 assert_ok!(TemplateModule::transfer(1922 origin1,1923 account(2),1924 CollectionId(1),1925 TokenId(1),1926 11927 ));1928 });1929}19301931#[test]1932fn allow_list_test_8() {1933 new_test_ext().execute_with(|| {1934 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));19351936 // Create NFT for account 11937 let data = default_nft_data();1938 create_test_item(collection_id, &data.into());19391940 let origin1 = Origin::signed(1);19411942 // Toggle Allow List mode and add accounts 1 and 21943 assert_ok!(TemplateModule::set_public_access_mode(1944 origin1.clone(),1945 collection_id,1946 AccessMode::AllowList1947 ));1948 assert_ok!(TemplateModule::add_to_allow_list(1949 origin1.clone(),1950 collection_id,1951 account(1)1952 ));1953 assert_ok!(TemplateModule::add_to_allow_list(1954 origin1.clone(),1955 collection_id,1956 account(2)1957 ));19581959 // Sself-approve account 1 for NFT 11960 assert_ok!(TemplateModule::approve(1961 origin1.clone(),1962 account(1),1963 CollectionId(1),1964 TokenId(1),1965 11966 ));1967 assert_eq!(1968 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1969 account(1)1970 );19711972 // Transfer from 1 to 21973 assert_ok!(TemplateModule::transfer_from(1974 origin1,1975 account(1),1976 account(2),1977 CollectionId(1),1978 TokenId(1),1979 11980 ));1981 });1982}19831984// If Public Access mode is set to AllowList, and Mint Permission is set to false, tokens can be created by owner.1985#[test]1986fn allow_list_test_9() {1987 new_test_ext().execute_with(|| {1988 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1989 let origin1 = Origin::signed(1);19901991 assert_ok!(TemplateModule::set_public_access_mode(1992 origin1.clone(),1993 collection_id,1994 AccessMode::AllowList1995 ));1996 assert_ok!(TemplateModule::set_mint_permission(1997 origin1,1998 collection_id,1999 false2000 ));20012002 let data = default_nft_data();2003 create_test_item(collection_id, &data.into());2004 });2005}20062007// If Public Access mode is set to AllowList, and Mint Permission is set to false, tokens can be created by admin.2008#[test]2009fn allow_list_test_10() {2010 new_test_ext().execute_with(|| {2011 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));20122013 let origin1 = Origin::signed(1);2014 let origin2 = Origin::signed(2);20152016 assert_ok!(TemplateModule::set_public_access_mode(2017 origin1.clone(),2018 collection_id,2019 AccessMode::AllowList2020 ));2021 assert_ok!(TemplateModule::set_mint_permission(2022 origin1.clone(),2023 collection_id,2024 false2025 ));20262027 assert_ok!(TemplateModule::add_collection_admin(2028 origin1,2029 collection_id,2030 account(2)2031 ));20322033 assert_ok!(TemplateModule::create_item(2034 origin2,2035 collection_id,2036 account(2),2037 default_nft_data().into()2038 ));2039 });2040}20412042// If Public Access mode is set to AllowList, and Mint Permission is set to false, tokens cannot be created by non-privileged and allow listed address.2043#[test]2044fn allow_list_test_11() {2045 new_test_ext().execute_with(|| {2046 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));20472048 let origin1 = Origin::signed(1);2049 let origin2 = Origin::signed(2);20502051 assert_ok!(TemplateModule::set_public_access_mode(2052 origin1.clone(),2053 collection_id,2054 AccessMode::AllowList2055 ));2056 assert_ok!(TemplateModule::set_mint_permission(2057 origin1.clone(),2058 collection_id,2059 false2060 ));2061 assert_ok!(TemplateModule::add_to_allow_list(2062 origin1,2063 collection_id,2064 account(2)2065 ));20662067 assert_noop!(2068 TemplateModule::create_item(2069 origin2,2070 CollectionId(1),2071 account(2),2072 default_nft_data().into()2073 )2074 .map_err(|e| e.error),2075 CommonError::<Test>::PublicMintingNotAllowed2076 );2077 });2078}20792080// If Public Access mode is set to AllowList, and Mint Permission is set to false, tokens cannot be created by non-privileged and non-allow listed address.2081#[test]2082fn allow_list_test_12() {2083 new_test_ext().execute_with(|| {2084 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));20852086 let origin1 = Origin::signed(1);2087 let origin2 = Origin::signed(2);20882089 assert_ok!(TemplateModule::set_public_access_mode(2090 origin1.clone(),2091 collection_id,2092 AccessMode::AllowList2093 ));2094 assert_ok!(TemplateModule::set_mint_permission(2095 origin1,2096 collection_id,2097 false2098 ));20992100 assert_noop!(2101 TemplateModule::create_item(2102 origin2,2103 CollectionId(1),2104 account(2),2105 default_nft_data().into()2106 )2107 .map_err(|e| e.error),2108 CommonError::<Test>::PublicMintingNotAllowed2109 );2110 });2111}21122113// If Public Access mode is set to AllowList, and Mint Permission is set to true, tokens can be created by owner.2114#[test]2115fn allow_list_test_13() {2116 new_test_ext().execute_with(|| {2117 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));21182119 let origin1 = Origin::signed(1);21202121 assert_ok!(TemplateModule::set_public_access_mode(2122 origin1.clone(),2123 collection_id,2124 AccessMode::AllowList2125 ));2126 assert_ok!(TemplateModule::set_mint_permission(2127 origin1,2128 collection_id,2129 true2130 ));21312132 let data = default_nft_data();2133 create_test_item(collection_id, &data.into());2134 });2135}21362137// If Public Access mode is set to AllowList, and Mint Permission is set to true, tokens can be created by admin.2138#[test]2139fn allow_list_test_14() {2140 new_test_ext().execute_with(|| {2141 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));21422143 let origin1 = Origin::signed(1);2144 let origin2 = Origin::signed(2);21452146 assert_ok!(TemplateModule::set_public_access_mode(2147 origin1.clone(),2148 collection_id,2149 AccessMode::AllowList2150 ));2151 assert_ok!(TemplateModule::set_mint_permission(2152 origin1.clone(),2153 collection_id,2154 true2155 ));21562157 assert_ok!(TemplateModule::add_collection_admin(2158 origin1,2159 collection_id,2160 account(2)2161 ));21622163 assert_ok!(TemplateModule::create_item(2164 origin2,2165 collection_id,2166 account(2),2167 default_nft_data().into()2168 ));2169 });2170}21712172// If Public Access mode is set to AllowList, and Mint Permission is set to true, tokens cannot be created by non-privileged and non-allow listed address.2173#[test]2174fn allow_list_test_15() {2175 new_test_ext().execute_with(|| {2176 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));21772178 let origin1 = Origin::signed(1);2179 let origin2 = Origin::signed(2);21802181 assert_ok!(TemplateModule::set_public_access_mode(2182 origin1.clone(),2183 collection_id,2184 AccessMode::AllowList2185 ));2186 assert_ok!(TemplateModule::set_mint_permission(2187 origin1,2188 collection_id,2189 true2190 ));21912192 assert_noop!(2193 TemplateModule::create_item(2194 origin2,2195 collection_id,2196 account(2),2197 default_nft_data().into()2198 )2199 .map_err(|e| e.error),2200 CommonError::<Test>::AddressNotInAllowlist2201 );2202 });2203}22042205// If Public Access mode is set to AllowList, and Mint Permission is set to true, tokens can be created by non-privileged and allow listed address.2206#[test]2207fn allow_list_test_16() {2208 new_test_ext().execute_with(|| {2209 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));22102211 let origin1 = Origin::signed(1);2212 let origin2 = Origin::signed(2);22132214 assert_ok!(TemplateModule::set_public_access_mode(2215 origin1.clone(),2216 collection_id,2217 AccessMode::AllowList2218 ));2219 assert_ok!(TemplateModule::set_mint_permission(2220 origin1.clone(),2221 collection_id,2222 true2223 ));2224 assert_ok!(TemplateModule::add_to_allow_list(2225 origin1,2226 collection_id,2227 account(2)2228 ));22292230 assert_ok!(TemplateModule::create_item(2231 origin2,2232 collection_id,2233 account(2),2234 default_nft_data().into()2235 ));2236 });2237}22382239// Total number of collections. Positive test2240#[test]2241fn total_number_collections_bound() {2242 new_test_ext().execute_with(|| {2243 create_test_collection(&CollectionMode::NFT, CollectionId(1));2244 });2245}22462247#[test]2248fn create_max_collections() {2249 new_test_ext().execute_with(|| {2250 for i in 1..COLLECTION_NUMBER_LIMIT {2251 create_test_collection(&CollectionMode::NFT, CollectionId(i));2252 }2253 });2254}22552256// Total number of collections. Negative test2257#[test]2258fn total_number_collections_bound_neg() {2259 new_test_ext().execute_with(|| {2260 let origin1 = Origin::signed(1);22612262 for i in 1..=COLLECTION_NUMBER_LIMIT {2263 create_test_collection(&CollectionMode::NFT, CollectionId(i));2264 }22652266 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();2267 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();2268 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();22692270 // 11-th collection in chain. Expects error2271 assert_noop!(2272 TemplateModule::create_collection(2273 origin1,2274 col_name1.try_into().unwrap(),2275 col_desc1.try_into().unwrap(),2276 token_prefix1.try_into().unwrap(),2277 CollectionMode::NFT2278 ),2279 CommonError::<Test>::TotalCollectionsLimitExceeded2280 );2281 });2282}22832284// Owned tokens by a single address. Positive test2285#[test]2286fn owned_tokens_bound() {2287 new_test_ext().execute_with(|| {2288 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));22892290 let data = default_nft_data();2291 create_test_item(collection_id, &data.clone().into());2292 create_test_item(collection_id, &data.into());2293 });2294}22952296// Owned tokens by a single address. Negotive test2297#[test]2298fn owned_tokens_bound_neg() {2299 new_test_ext().execute_with(|| {2300 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23012302 let origin1 = Origin::signed(1);23032304 for _ in 1..=MAX_TOKEN_OWNERSHIP {2305 let data = default_nft_data();2306 create_test_item(collection_id, &data.clone().into());2307 }23082309 let data = default_nft_data();2310 assert_noop!(2311 TemplateModule::create_item(origin1, CollectionId(1), account(1), data.into())2312 .map_err(|e| e.error),2313 CommonError::<Test>::AccountTokenLimitExceeded2314 );2315 });2316}23172318// Number of collection admins. Positive test2319#[test]2320fn collection_admins_bound() {2321 new_test_ext().execute_with(|| {2322 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23232324 let origin1 = Origin::signed(1);23252326 assert_ok!(TemplateModule::add_collection_admin(2327 origin1.clone(),2328 collection_id,2329 account(2)2330 ));2331 assert_ok!(TemplateModule::add_collection_admin(2332 origin1,2333 collection_id,2334 account(3)2335 ));2336 });2337}23382339// Number of collection admins. Negotive test2340#[test]2341fn collection_admins_bound_neg() {2342 new_test_ext().execute_with(|| {2343 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23442345 let origin1 = Origin::signed(1);23462347 for i in 0..COLLECTION_ADMINS_LIMIT {2348 assert_ok!(TemplateModule::add_collection_admin(2349 origin1.clone(),2350 collection_id,2351 account((2 + i).into())2352 ));2353 }2354 assert_noop!(2355 TemplateModule::add_collection_admin(2356 origin1,2357 collection_id,2358 account((3 + COLLECTION_ADMINS_LIMIT).into())2359 ),2360 CommonError::<Test>::CollectionAdminCountExceeded2361 );2362 });2363}2364// #endregion23652366#[test]2367fn set_const_on_chain_schema() {2368 new_test_ext().execute_with(|| {2369 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23702371 let origin1 = Origin::signed(1);2372 assert_ok!(TemplateModule::set_const_on_chain_schema(2373 origin1,2374 collection_id,2375 b"test const on chain schema".to_vec().try_into().unwrap()2376 ));23772378 assert_eq!(2379 <pallet_common::CollectionById<Test>>::get(collection_id)2380 .unwrap()2381 .const_on_chain_schema,2382 b"test const on chain schema".to_vec()2383 );2384 assert_eq!(2385 <pallet_common::CollectionById<Test>>::get(collection_id)2386 .unwrap()2387 .variable_on_chain_schema,2388 b"".to_vec()2389 );2390 });2391}23922393#[test]2394fn set_variable_on_chain_schema() {2395 new_test_ext().execute_with(|| {2396 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23972398 let origin1 = Origin::signed(1);2399 assert_ok!(TemplateModule::set_variable_on_chain_schema(2400 origin1,2401 collection_id,2402 b"test variable on chain schema"2403 .to_vec()2404 .try_into()2405 .unwrap()2406 ));24072408 assert_eq!(2409 <pallet_common::CollectionById<Test>>::get(collection_id)2410 .unwrap()2411 .const_on_chain_schema,2412 b"".to_vec()2413 );2414 assert_eq!(2415 <pallet_common::CollectionById<Test>>::get(collection_id)2416 .unwrap()2417 .variable_on_chain_schema,2418 b"test variable on chain schema".to_vec()2419 );2420 });2421}24222423#[test]2424fn set_variable_meta_data_on_nft_token_stores_variable_meta_data() {2425 new_test_ext().execute_with(|| {2426 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));24272428 let origin1 = Origin::signed(1);24292430 let data = default_nft_data();2431 create_test_item(CollectionId(1), &data.into());24322433 let variable_data = b"test data".to_vec();2434 assert_ok!(TemplateModule::set_variable_meta_data(2435 origin1,2436 collection_id,2437 TokenId(1),2438 variable_data.clone().try_into().unwrap()2439 ));24402441 assert_eq!(2442 <pallet_nonfungible::TokenData<Test>>::get((collection_id, 1))2443 .unwrap()2444 .variable_data,2445 variable_data2446 );2447 });2448}24492450#[test]2451fn set_variable_meta_data_on_re_fungible_token_stores_variable_meta_data() {2452 new_test_ext().execute_with(|| {2453 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));24542455 let origin1 = Origin::signed(1);24562457 let data = default_re_fungible_data();2458 create_test_item(collection_id, &data.into());24592460 let variable_data = b"test data".to_vec();2461 assert_ok!(TemplateModule::set_variable_meta_data(2462 origin1,2463 collection_id,2464 TokenId(1),2465 variable_data.clone().try_into().unwrap()2466 ));24672468 assert_eq!(2469 <pallet_refungible::TokenData<Test>>::get((collection_id, TokenId(1))).variable_data,2470 variable_data2471 );2472 });2473}24742475#[test]2476fn set_variable_meta_data_on_fungible_token_fails() {2477 new_test_ext().execute_with(|| {2478 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));24792480 let origin1 = Origin::signed(1);24812482 let data = default_fungible_data();2483 create_test_item(collection_id, &data.into());24842485 let variable_data = b"test data".to_vec();2486 assert_noop!(2487 TemplateModule::set_variable_meta_data(2488 origin1,2489 collection_id,2490 TokenId(0),2491 variable_data.try_into().unwrap()2492 )2493 .map_err(|e| e.error),2494 <pallet_fungible::Error<Test>>::FungibleItemsDontHaveData2495 );2496 });2497}24982499#[test]2500fn set_variable_meta_data_on_nft_with_item_owner_permission_flag() {2501 new_test_ext().execute_with(|| {2502 //default_limits();25032504 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));25052506 let origin1 = Origin::signed(1);25072508 let data = default_nft_data();2509 create_test_item(collection_id, &data.into());25102511 assert_ok!(TemplateModule::set_meta_update_permission_flag(2512 origin1.clone(),2513 collection_id,2514 MetaUpdatePermission::ItemOwner,2515 ));25162517 let variable_data = b"ten chars.".to_vec();2518 assert_ok!(TemplateModule::set_variable_meta_data(2519 origin1,2520 collection_id,2521 TokenId(1),2522 variable_data.clone().try_into().unwrap()2523 ));25242525 assert_eq!(2526 <pallet_nonfungible::TokenData<Test>>::get((collection_id, TokenId(1)))2527 .unwrap()2528 .variable_data,2529 variable_data2530 );2531 });2532}25332534#[test]2535fn collection_transfer_flag_works() {2536 new_test_ext().execute_with(|| {2537 let origin1 = Origin::signed(1);25382539 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));2540 assert_ok!(TemplateModule::set_transfers_enabled_flag(2541 origin1,2542 collection_id,2543 true2544 ));25452546 let data = default_nft_data();2547 create_test_item(collection_id, &data.into());2548 assert_eq!(2549 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2550 12551 );2552 assert_eq!(2553 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2554 true2555 );25562557 let origin1 = Origin::signed(1);25582559 // default scenario2560 assert_ok!(TemplateModule::transfer(2561 origin1,2562 account(2),2563 collection_id,2564 TokenId(1),2565 12566 ));2567 assert_eq!(2568 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2569 false2570 );2571 assert_eq!(2572 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),2573 true2574 );2575 assert_eq!(2576 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2577 02578 );2579 assert_eq!(2580 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(2))),2581 12582 );2583 });2584}25852586#[test]2587fn set_variable_meta_data_on_nft_with_admin_flag() {2588 new_test_ext().execute_with(|| {2589 // default_limits();25902591 let collection_id =2592 create_test_collection_for_owner(&CollectionMode::NFT, 2, CollectionId(1));25932594 let origin1 = Origin::signed(1);2595 let origin2 = Origin::signed(2);25962597 assert_ok!(TemplateModule::set_mint_permission(2598 origin2.clone(),2599 collection_id,2600 true2601 ));2602 assert_ok!(TemplateModule::add_to_allow_list(2603 origin2.clone(),2604 collection_id,2605 account(1)2606 ));26072608 assert_ok!(TemplateModule::add_collection_admin(2609 origin2.clone(),2610 collection_id,2611 account(1)2612 ));26132614 let data = default_nft_data();2615 create_test_item(collection_id, &data.into());26162617 assert_ok!(TemplateModule::set_meta_update_permission_flag(2618 origin2.clone(),2619 collection_id,2620 MetaUpdatePermission::Admin,2621 ));26222623 let variable_data = b"test.".to_vec();2624 assert_ok!(TemplateModule::set_variable_meta_data(2625 origin1,2626 collection_id,2627 TokenId(1),2628 variable_data.clone().try_into().unwrap()2629 ));26302631 assert_eq!(2632 <pallet_nonfungible::TokenData<Test>>::get((collection_id, 1))2633 .unwrap()2634 .variable_data,2635 variable_data2636 );2637 });2638}26392640#[test]2641fn set_variable_meta_data_on_nft_with_admin_flag_neg() {2642 new_test_ext().execute_with(|| {2643 // default_limits();26442645 let collection_id =2646 create_test_collection_for_owner(&CollectionMode::NFT, 2, CollectionId(1));26472648 let origin1 = Origin::signed(1);2649 let origin2 = Origin::signed(2);26502651 assert_ok!(TemplateModule::set_mint_permission(2652 origin2.clone(),2653 collection_id,2654 true2655 ));2656 assert_ok!(TemplateModule::add_to_allow_list(2657 origin2.clone(),2658 collection_id,2659 account(1)2660 ));26612662 let data = default_nft_data();2663 create_test_item(collection_id, &data.into());26642665 assert_ok!(TemplateModule::set_meta_update_permission_flag(2666 origin2.clone(),2667 collection_id,2668 MetaUpdatePermission::Admin,2669 ));26702671 let variable_data = b"test.".to_vec();2672 assert_noop!(2673 TemplateModule::set_variable_meta_data(2674 origin1,2675 collection_id,2676 TokenId(1),2677 variable_data.try_into().unwrap()2678 )2679 .map_err(|e| e.error),2680 CommonError::<Test>::NoPermission2681 );2682 });2683}26842685#[test]2686fn set_variable_meta_flag_after_freeze() {2687 new_test_ext().execute_with(|| {2688 // default_limits();26892690 let collection_id =2691 create_test_collection_for_owner(&CollectionMode::NFT, 2, CollectionId(1));26922693 let origin2 = Origin::signed(2);26942695 assert_ok!(TemplateModule::set_meta_update_permission_flag(2696 origin2.clone(),2697 collection_id,2698 MetaUpdatePermission::None,2699 ));2700 assert_noop!(2701 TemplateModule::set_meta_update_permission_flag(2702 origin2.clone(),2703 collection_id,2704 MetaUpdatePermission::Admin2705 ),2706 CommonError::<Test>::MetadataFlagFrozen2707 );2708 });2709}27102711#[test]2712fn set_variable_meta_data_on_nft_with_none_flag_neg() {2713 new_test_ext().execute_with(|| {2714 // default_limits();27152716 let collection_id =2717 create_test_collection_for_owner(&CollectionMode::NFT, 1, CollectionId(1));2718 let origin1 = Origin::signed(1);27192720 let data = default_nft_data();2721 create_test_item(collection_id, &data.into());27222723 assert_ok!(TemplateModule::set_meta_update_permission_flag(2724 origin1.clone(),2725 collection_id,2726 MetaUpdatePermission::None,2727 ));27282729 let variable_data = b"test.".to_vec();2730 assert_noop!(2731 TemplateModule::set_variable_meta_data(2732 origin1.clone(),2733 collection_id,2734 TokenId(1),2735 variable_data.try_into().unwrap()2736 )2737 .map_err(|e| e.error),2738 CommonError::<Test>::NoPermission2739 );2740 });2741}27422743#[test]2744fn collection_transfer_flag_works_neg() {2745 new_test_ext().execute_with(|| {2746 let origin1 = Origin::signed(1);27472748 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));2749 assert_ok!(TemplateModule::set_transfers_enabled_flag(2750 origin1,2751 collection_id,2752 false2753 ));27542755 let data = default_nft_data();2756 create_test_item(collection_id, &data.into());2757 assert_eq!(2758 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2759 12760 );2761 assert_eq!(2762 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2763 true2764 );27652766 let origin1 = Origin::signed(1);27672768 // default scenario2769 assert_noop!(2770 TemplateModule::transfer(origin1, account(2), CollectionId(1), TokenId(1), 1)2771 .map_err(|e| e.error),2772 CommonError::<Test>::TransferNotAllowed2773 );2774 assert_eq!(2775 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2776 12777 );2778 assert_eq!(2779 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(2))),2780 02781 );2782 assert_eq!(2783 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2784 true2785 );2786 assert_eq!(2787 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),2788 false2789 );2790 });2791}1// Tests to be written here2use super::*;3use crate::mock::*;4use crate::{AccessMode, CollectionMode};5use up_data_structs::{6 COLLECTION_NUMBER_LIMIT, CollectionId, CreateItemData, CreateFungibleData, CreateNftData,7 CreateReFungibleData, MAX_DECIMAL_POINTS, COLLECTION_ADMINS_LIMIT, MetaUpdatePermission,8 TokenId, MAX_TOKEN_OWNERSHIP,9};10use frame_support::{assert_noop, assert_ok, assert_err};11use sp_std::convert::TryInto;12use pallet_balances;1314fn add_balance(user: u64, value: u64) {15 const DONOR_USER: u64 = 999;16 assert_ok!(<pallet_balances::Pallet<Test>>::set_balance(17 Origin::root(),18 DONOR_USER,19 value,20 021 ));22 assert_ok!(<pallet_balances::Pallet<Test>>::force_transfer(23 Origin::root(),24 DONOR_USER,25 user,26 value27 ));28}2930fn default_nft_data() -> CreateNftData {31 CreateNftData {32 const_data: vec![1, 2, 3].try_into().unwrap(),33 variable_data: vec![3, 2, 1].try_into().unwrap(),34 }35}3637fn default_fungible_data() -> CreateFungibleData {38 CreateFungibleData { value: 5 }39}4041fn default_re_fungible_data() -> CreateReFungibleData {42 CreateReFungibleData {43 const_data: vec![1, 2, 3].try_into().unwrap(),44 variable_data: vec![3, 2, 1].try_into().unwrap(),45 pieces: 1023,46 }47}4849fn create_test_collection_for_owner(50 mode: &CollectionMode,51 owner: u64,52 id: CollectionId,53) -> CollectionId {54 add_balance(owner, CollectionCreationPrice::get() as u64 + 1);5556 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();57 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();58 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();5960 let origin1 = Origin::signed(owner);61 assert_ok!(TemplateModule::create_collection(62 origin1,63 col_name1.try_into().unwrap(),64 col_desc1.try_into().unwrap(),65 token_prefix1.try_into().unwrap(),66 mode.clone()67 ));6869 let saved_col_name: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();70 let saved_description: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();71 let saved_prefix: Vec<u8> = b"token_prefix1\0".to_vec();72 assert_eq!(73 <pallet_common::CollectionById<Test>>::get(id)74 .unwrap()75 .owner,76 owner77 );78 assert_eq!(79 <pallet_common::CollectionById<Test>>::get(id).unwrap().name,80 saved_col_name81 );82 assert_eq!(83 <pallet_common::CollectionById<Test>>::get(id).unwrap().mode,84 *mode85 );86 assert_eq!(87 <pallet_common::CollectionById<Test>>::get(id)88 .unwrap()89 .description,90 saved_description91 );92 assert_eq!(93 <pallet_common::CollectionById<Test>>::get(id)94 .unwrap()95 .token_prefix,96 saved_prefix97 );98 id99}100101fn create_test_collection(mode: &CollectionMode, id: CollectionId) -> CollectionId {102 create_test_collection_for_owner(&mode, 1, id)103}104105fn create_test_item(collection_id: CollectionId, data: &CreateItemData) {106 let origin1 = Origin::signed(1);107 assert_ok!(TemplateModule::create_item(108 origin1,109 collection_id,110 account(1),111 data.clone()112 ));113}114115fn account(sub: u64) -> TestCrossAccountId {116 TestCrossAccountId::from_sub(sub)117}118119// Use cases tests region120// #region121122#[test]123fn set_version_schema() {124 new_test_ext().execute_with(|| {125 let origin1 = Origin::signed(1);126 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));127128 assert_ok!(TemplateModule::set_schema_version(129 origin1,130 collection_id,131 SchemaVersion::Unique132 ));133 assert_eq!(134 <pallet_common::CollectionById<Test>>::get(collection_id)135 .unwrap()136 .schema_version,137 SchemaVersion::Unique138 );139 });140}141142#[test]143fn check_not_sufficient_founds() {144 new_test_ext().execute_with(|| {145 let acc: u64 = 1;146 <pallet_balances::Pallet<Test>>::set_balance(Origin::root(), acc, 0, 0).unwrap();147148 let name: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();149 let description: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();150 let token_prefix: Vec<u8> = b"token_prefix1\0".to_vec();151152 let data: CreateCollectionData<<Test as system::Config>::AccountId> =153 CreateCollectionData {154 name: name.try_into().unwrap(),155 description: description.try_into().unwrap(),156 token_prefix: token_prefix.try_into().unwrap(),157 mode: CollectionMode::NFT,158 ..Default::default()159 };160161 let result = TemplateModule::create_collection_ex(Origin::signed(acc), data);162 assert_err!(result, <CommonError<Test>>::NotSufficientFounds);163 });164}165166#[test]167fn create_fungible_collection_fails_with_large_decimal_numbers() {168 new_test_ext().execute_with(|| {169 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();170 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();171 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();172173 let origin1 = Origin::signed(1);174 assert_noop!(175 TemplateModule::create_collection(176 origin1,177 col_name1.try_into().unwrap(),178 col_desc1.try_into().unwrap(),179 token_prefix1.try_into().unwrap(),180 CollectionMode::Fungible(MAX_DECIMAL_POINTS + 1)181 ),182 Error::<Test>::CollectionDecimalPointLimitExceeded183 );184 });185}186187#[test]188fn create_nft_item() {189 new_test_ext().execute_with(|| {190 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));191192 let data = default_nft_data();193 create_test_item(collection_id, &data.clone().into());194195 let item = <pallet_nonfungible::TokenData<Test>>::get((collection_id, 1)).unwrap();196 assert_eq!(item.const_data, data.const_data.into_inner());197 assert_eq!(item.variable_data, data.variable_data.into_inner());198 });199}200201// Use cases tests region202// #region203#[test]204fn create_nft_multiple_items() {205 new_test_ext().execute_with(|| {206 create_test_collection(&CollectionMode::NFT, CollectionId(1));207208 let origin1 = Origin::signed(1);209210 let items_data = vec![default_nft_data(), default_nft_data(), default_nft_data()];211212 assert_ok!(TemplateModule::create_multiple_items(213 origin1,214 CollectionId(1),215 account(1),216 items_data217 .clone()218 .into_iter()219 .map(|d| { d.into() })220 .collect()221 ));222 for (index, data) in items_data.into_iter().enumerate() {223 let item = <pallet_nonfungible::TokenData<Test>>::get((224 CollectionId(1),225 TokenId((index + 1) as u32),226 ))227 .unwrap();228 assert_eq!(item.const_data.to_vec(), data.const_data.into_inner());229 assert_eq!(item.variable_data.to_vec(), data.variable_data.into_inner());230 }231 });232}233234#[test]235fn create_refungible_item() {236 new_test_ext().execute_with(|| {237 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));238239 let data = default_re_fungible_data();240 create_test_item(collection_id, &data.clone().into());241 let item = <pallet_refungible::TokenData<Test>>::get((collection_id, TokenId(1)));242 let balance =243 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1)));244 assert_eq!(item.const_data, data.const_data.into_inner());245 assert_eq!(item.variable_data, data.variable_data.into_inner());246 assert_eq!(balance, 1023);247 });248}249250#[test]251fn create_multiple_refungible_items() {252 new_test_ext().execute_with(|| {253 create_test_collection(&CollectionMode::ReFungible, CollectionId(1));254255 let origin1 = Origin::signed(1);256257 let items_data = vec![258 default_re_fungible_data(),259 default_re_fungible_data(),260 default_re_fungible_data(),261 ];262263 assert_ok!(TemplateModule::create_multiple_items(264 origin1,265 CollectionId(1),266 account(1),267 items_data268 .clone()269 .into_iter()270 .map(|d| { d.into() })271 .collect()272 ));273 for (index, data) in items_data.into_iter().enumerate() {274 let item = <pallet_refungible::TokenData<Test>>::get((275 CollectionId(1),276 TokenId((index + 1) as u32),277 ));278 let balance =279 <pallet_refungible::Balance<Test>>::get((CollectionId(1), TokenId(1), account(1)));280 assert_eq!(item.const_data.to_vec(), data.const_data.into_inner());281 assert_eq!(item.variable_data.to_vec(), data.variable_data.into_inner());282 assert_eq!(balance, 1023);283 }284 });285}286287#[test]288fn create_fungible_item() {289 new_test_ext().execute_with(|| {290 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));291292 let data = default_fungible_data();293 create_test_item(collection_id, &data.into());294295 assert_eq!(296 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),297 5298 );299 });300}301302//#[test]303// fn create_multiple_fungible_items() {304// new_test_ext().execute_with(|| {305// default_limits();306307// create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));308309// let origin1 = Origin::signed(1);310311// let items_data = vec![default_fungible_data(), default_fungible_data(), default_fungible_data()];312313// assert_ok!(TemplateModule::create_multiple_items(314// origin1.clone(),315// 1,316// 1,317// items_data.clone().into_iter().map(|d| { d.into() }).collect()318// ));319320// for (index, _) in items_data.iter().enumerate() {321// assert_eq!(TemplateModule::fungible_item_id(1, (index + 1) as TokenId).value, 5);322// }323// assert_eq!(TemplateModule::balance_count(1, 1), 3000);324// assert_eq!(TemplateModule::address_tokens(1, 1), [1, 2, 3]);325// });326// }327328#[test]329fn transfer_fungible_item() {330 new_test_ext().execute_with(|| {331 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));332333 let origin1 = Origin::signed(1);334 let origin2 = Origin::signed(2);335336 let data = default_fungible_data();337 create_test_item(collection_id, &data.into());338339 assert_eq!(340 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(1))),341 5342 );343344 // change owner scenario345 assert_ok!(TemplateModule::transfer(346 origin1,347 account(2),348 CollectionId(1),349 TokenId(0),350 5351 ));352 assert_eq!(353 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(1))),354 0355 );356357 // split item scenario358 assert_ok!(TemplateModule::transfer(359 origin2.clone(),360 account(3),361 CollectionId(1),362 TokenId(0),363 3364 ));365366 // split item and new owner has account scenario367 assert_ok!(TemplateModule::transfer(368 origin2,369 account(3),370 CollectionId(1),371 TokenId(0),372 1373 ));374 assert_eq!(375 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(2))),376 1377 );378 assert_eq!(379 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(3))),380 4381 );382 });383}384385#[test]386fn transfer_refungible_item() {387 new_test_ext().execute_with(|| {388 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));389390 // Create RFT 1 in 1023 pieces for account 1391 let data = default_re_fungible_data();392 create_test_item(collection_id, &data.clone().into());393 let item = <pallet_refungible::TokenData<Test>>::get((collection_id, TokenId(1)));394 assert_eq!(item.const_data, data.const_data.into_inner());395 assert_eq!(item.variable_data, data.variable_data.into_inner());396 assert_eq!(397 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),398 1399 );400 assert_eq!(401 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),402 1023403 );404 assert_eq!(405 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),406 true407 );408409 // Account 1 transfers all 1023 pieces of RFT 1 to account 2410 let origin1 = Origin::signed(1);411 let origin2 = Origin::signed(2);412 assert_ok!(TemplateModule::transfer(413 origin1,414 account(2),415 CollectionId(1),416 TokenId(1),417 1023418 ));419 assert_eq!(420 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(2))),421 1023422 );423 assert_eq!(424 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),425 0426 );427 assert_eq!(428 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(2))),429 1430 );431 assert_eq!(432 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),433 false434 );435 assert_eq!(436 <pallet_refungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),437 true438 );439440 // Account 2 transfers 500 pieces of RFT 1 to account 3441 assert_ok!(TemplateModule::transfer(442 origin2.clone(),443 account(3),444 CollectionId(1),445 TokenId(1),446 500447 ));448 assert_eq!(449 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(2))),450 523451 );452 assert_eq!(453 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(3))),454 500455 );456 assert_eq!(457 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(2))),458 1459 );460 assert_eq!(461 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(3))),462 1463 );464 assert_eq!(465 <pallet_refungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),466 true467 );468 assert_eq!(469 <pallet_refungible::Owned<Test>>::get((collection_id, account(3), TokenId(1))),470 true471 );472473 // Account 2 transfers 200 more pieces of RFT 1 to account 3 with pre-existing balance474 assert_ok!(TemplateModule::transfer(475 origin2,476 account(3),477 CollectionId(1),478 TokenId(1),479 200480 ));481 assert_eq!(482 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(2))),483 323484 );485 assert_eq!(486 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(3))),487 700488 );489 assert_eq!(490 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(2))),491 1492 );493 assert_eq!(494 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(3))),495 1496 );497 assert_eq!(498 <pallet_refungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),499 true500 );501 assert_eq!(502 <pallet_refungible::Owned<Test>>::get((collection_id, account(3), TokenId(1))),503 true504 );505 });506}507508#[test]509fn transfer_nft_item() {510 new_test_ext().execute_with(|| {511 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));512513 let data = default_nft_data();514 create_test_item(collection_id, &data.into());515 assert_eq!(516 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),517 1518 );519 assert_eq!(520 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),521 true522 );523524 let origin1 = Origin::signed(1);525 // default scenario526 assert_ok!(TemplateModule::transfer(527 origin1,528 account(2),529 CollectionId(1),530 TokenId(1),531 1532 ));533 assert_eq!(534 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),535 0536 );537 assert_eq!(538 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(2))),539 1540 );541 assert_eq!(542 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),543 false544 );545 assert_eq!(546 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),547 true548 );549 });550}551552#[test]553fn transfer_nft_item_wrong_value() {554 new_test_ext().execute_with(|| {555 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));556557 let data = default_nft_data();558 create_test_item(collection_id, &data.into());559 assert_eq!(560 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),561 1562 );563 assert_eq!(564 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),565 true566 );567568 let origin1 = Origin::signed(1);569570 assert_noop!(571 TemplateModule::transfer(origin1, account(2), CollectionId(1), TokenId(1), 2)572 .map_err(|e| e.error),573 <pallet_nonfungible::Error::<Test>>::NonfungibleItemsHaveNoAmount574 );575 });576}577578#[test]579fn transfer_nft_item_zero_value() {580 new_test_ext().execute_with(|| {581 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));582583 let data = default_nft_data();584 create_test_item(collection_id, &data.into());585 assert_eq!(586 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),587 1588 );589 assert_eq!(590 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),591 true592 );593594 let origin1 = Origin::signed(1);595596 // Transferring 0 amount works on NFT...597 assert_ok!(TemplateModule::transfer(598 origin1,599 account(2),600 CollectionId(1),601 TokenId(1),602 0603 ));604 // ... and results in no transfer605 assert_eq!(606 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),607 1608 );609 assert_eq!(610 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),611 true612 );613 });614}615616#[test]617fn nft_approve_and_transfer_from() {618 new_test_ext().execute_with(|| {619 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));620621 let data = default_nft_data();622 create_test_item(collection_id, &data.into());623624 let origin1 = Origin::signed(1);625 let origin2 = Origin::signed(2);626627 assert_eq!(628 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),629 1630 );631 assert_eq!(632 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),633 true634 );635636 // neg transfer_from637 assert_noop!(638 TemplateModule::transfer_from(639 origin2.clone(),640 account(1),641 account(2),642 CollectionId(1),643 TokenId(1),644 1645 )646 .map_err(|e| e.error),647 CommonError::<Test>::ApprovedValueTooLow648 );649650 // do approve651 assert_ok!(TemplateModule::approve(652 origin1,653 account(2),654 CollectionId(1),655 TokenId(1),656 1657 ));658 assert_eq!(659 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),660 account(2)661 );662663 assert_ok!(TemplateModule::transfer_from(664 origin2,665 account(1),666 account(3),667 CollectionId(1),668 TokenId(1),669 1670 ));671 assert!(672 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).is_none()673 );674 });675}676677#[test]678fn nft_approve_and_transfer_from_allow_list() {679 new_test_ext().execute_with(|| {680 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));681682 let origin1 = Origin::signed(1);683 let origin2 = Origin::signed(2);684685 // Create NFT 1 for account 1686 let data = default_nft_data();687 create_test_item(collection_id, &data.clone().into());688 assert_eq!(689 &<pallet_nonfungible::TokenData<Test>>::get((collection_id, TokenId(1)))690 .unwrap()691 .const_data,692 &data.const_data.into_inner()693 );694 assert_eq!(695 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),696 1697 );698 assert_eq!(699 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),700 true701 );702703 // Allow allow-list users to mint and add accounts 1, 2, and 3 to allow-list704 assert_ok!(TemplateModule::set_mint_permission(705 origin1.clone(),706 CollectionId(1),707 true708 ));709 assert_ok!(TemplateModule::set_public_access_mode(710 origin1.clone(),711 CollectionId(1),712 AccessMode::AllowList713 ));714 assert_ok!(TemplateModule::add_to_allow_list(715 origin1.clone(),716 CollectionId(1),717 account(1)718 ));719 assert_ok!(TemplateModule::add_to_allow_list(720 origin1.clone(),721 CollectionId(1),722 account(2)723 ));724 assert_ok!(TemplateModule::add_to_allow_list(725 origin1.clone(),726 CollectionId(1),727 account(3)728 ));729730 // Account 1 approves account 2 for NFT 1731 assert_ok!(TemplateModule::approve(732 origin1.clone(),733 account(2),734 CollectionId(1),735 TokenId(1),736 1737 ));738 assert_eq!(739 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),740 account(2)741 );742743 // Account 2 transfers NFT 1 from account 1 to account 3744 assert_ok!(TemplateModule::transfer_from(745 origin2,746 account(1),747 account(3),748 CollectionId(1),749 TokenId(1),750 1751 ));752 assert!(753 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).is_none()754 );755 });756}757758#[test]759fn refungible_approve_and_transfer_from() {760 new_test_ext().execute_with(|| {761 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));762763 let origin1 = Origin::signed(1);764 let origin2 = Origin::signed(2);765766 // Create RFT 1 in 1023 pieces for account 1767 let data = default_re_fungible_data();768 create_test_item(collection_id, &data.into());769770 assert_eq!(771 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),772 1773 );774 assert_eq!(775 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),776 1023777 );778 assert_eq!(779 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),780 true781 );782783 // Allow public minting, enable allow-list and add accounts 1, 2, 3 to allow-list784 assert_ok!(TemplateModule::set_mint_permission(785 origin1.clone(),786 CollectionId(1),787 true788 ));789 assert_ok!(TemplateModule::set_public_access_mode(790 origin1.clone(),791 CollectionId(1),792 AccessMode::AllowList793 ));794 assert_ok!(TemplateModule::add_to_allow_list(795 origin1.clone(),796 CollectionId(1),797 account(1)798 ));799 assert_ok!(TemplateModule::add_to_allow_list(800 origin1.clone(),801 CollectionId(1),802 account(2)803 ));804 assert_ok!(TemplateModule::add_to_allow_list(805 origin1.clone(),806 CollectionId(1),807 account(3)808 ));809810 // Account 1 approves account 2 for 1023 pieces of RFT 1811 assert_ok!(TemplateModule::approve(812 origin1,813 account(2),814 CollectionId(1),815 TokenId(1),816 1023817 ));818 assert_eq!(819 <pallet_refungible::Allowance<Test>>::get((820 CollectionId(1),821 TokenId(1),822 account(1),823 account(2)824 )),825 1023826 );827828 // Account 2 transfers 100 pieces of RFT 1 from account 1 to account 3829 assert_ok!(TemplateModule::transfer_from(830 origin2,831 account(1),832 account(3),833 CollectionId(1),834 TokenId(1),835 100836 ));837 assert_eq!(838 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),839 1840 );841 assert_eq!(842 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(3))),843 1844 );845 assert_eq!(846 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),847 923848 );849 assert_eq!(850 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(3))),851 100852 );853 assert_eq!(854 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),855 true856 );857 assert_eq!(858 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),859 true860 );861 assert_eq!(862 <pallet_refungible::Allowance<Test>>::get((863 CollectionId(1),864 TokenId(1),865 account(1),866 account(2)867 )),868 923869 );870 });871}872873#[test]874fn fungible_approve_and_transfer_from() {875 new_test_ext().execute_with(|| {876 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));877878 let data = default_fungible_data();879 create_test_item(collection_id, &data.into());880881 let origin1 = Origin::signed(1);882 let origin2 = Origin::signed(2);883884 assert_ok!(TemplateModule::set_mint_permission(885 origin1.clone(),886 CollectionId(1),887 true888 ));889 assert_ok!(TemplateModule::set_public_access_mode(890 origin1.clone(),891 CollectionId(1),892 AccessMode::AllowList893 ));894 assert_ok!(TemplateModule::add_to_allow_list(895 origin1.clone(),896 CollectionId(1),897 account(1)898 ));899 assert_ok!(TemplateModule::add_to_allow_list(900 origin1.clone(),901 CollectionId(1),902 account(2)903 ));904 assert_ok!(TemplateModule::add_to_allow_list(905 origin1.clone(),906 CollectionId(1),907 account(3)908 ));909910 // do approve911 assert_ok!(TemplateModule::approve(912 origin1.clone(),913 account(2),914 CollectionId(1),915 TokenId(0),916 5917 ));918 assert_eq!(919 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(2))),920 5921 );922 assert_ok!(TemplateModule::approve(923 origin1,924 account(3),925 CollectionId(1),926 TokenId(0),927 5928 ));929 assert_eq!(930 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(2))),931 5932 );933 assert_eq!(934 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(3))),935 5936 );937938 assert_ok!(TemplateModule::transfer_from(939 origin2.clone(),940 account(1),941 account(3),942 CollectionId(1),943 TokenId(0),944 4945 ));946947 assert_eq!(948 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(2))),949 1950 );951952 assert_noop!(953 TemplateModule::transfer_from(954 origin2,955 account(1),956 account(3),957 CollectionId(1),958 TokenId(0),959 4960 )961 .map_err(|e| e.error),962 CommonError::<Test>::ApprovedValueTooLow963 );964 });965}966967#[test]968fn change_collection_owner() {969 new_test_ext().execute_with(|| {970 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));971972 let origin1 = Origin::signed(1);973 assert_ok!(TemplateModule::change_collection_owner(974 origin1,975 collection_id,976 2977 ));978 assert_eq!(979 <pallet_common::CollectionById<Test>>::get(collection_id)980 .unwrap()981 .owner,982 2983 );984 });985}986987#[test]988fn destroy_collection() {989 new_test_ext().execute_with(|| {990 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));991992 let origin1 = Origin::signed(1);993 assert_ok!(TemplateModule::destroy_collection(origin1, collection_id));994 });995}996997#[test]998fn burn_nft_item() {999 new_test_ext().execute_with(|| {1000 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));10011002 let origin1 = Origin::signed(1);10031004 let data = default_nft_data();1005 create_test_item(collection_id, &data.into());10061007 // check balance (collection with id = 1, user id = 1)1008 assert_eq!(1009 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1010 11011 );10121013 // burn item1014 assert_ok!(TemplateModule::burn_item(1015 origin1.clone(),1016 collection_id,1017 TokenId(1),1018 11019 ));1020 assert_eq!(1021 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1022 01023 );1024 });1025}10261027#[test]1028fn burn_same_nft_item_twice() {1029 new_test_ext().execute_with(|| {1030 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));10311032 let origin1 = Origin::signed(1);10331034 let data = default_nft_data();1035 create_test_item(collection_id, &data.into());10361037 // check balance (collection with id = 1, user id = 1)1038 assert_eq!(1039 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1040 11041 );10421043 // burn item1044 assert_ok!(TemplateModule::burn_item(1045 origin1.clone(),1046 collection_id,1047 TokenId(1),1048 11049 ));10501051 // burn item again1052 assert_noop!(1053 TemplateModule::burn_item(origin1, collection_id, TokenId(1), 1).map_err(|e| e.error),1054 CommonError::<Test>::TokenNotFound1055 );10561057 assert_eq!(1058 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1059 01060 );1061 });1062}10631064#[test]1065fn burn_fungible_item() {1066 new_test_ext().execute_with(|| {1067 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));10681069 let origin1 = Origin::signed(1);1070 assert_ok!(TemplateModule::add_collection_admin(1071 origin1.clone(),1072 collection_id,1073 account(2)1074 ));10751076 let data = default_fungible_data();1077 create_test_item(collection_id, &data.into());10781079 // check balance (collection with id = 1, user id = 1)1080 assert_eq!(1081 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),1082 51083 );10841085 // burn item1086 assert_ok!(TemplateModule::burn_item(1087 origin1.clone(),1088 CollectionId(1),1089 TokenId(0),1090 51091 ));1092 assert_noop!(1093 TemplateModule::burn_item(origin1, CollectionId(1), TokenId(0), 5).map_err(|e| e.error),1094 CommonError::<Test>::TokenValueTooLow1095 );10961097 assert_eq!(1098 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),1099 01100 );1101 });1102}11031104#[test]1105fn burn_fungible_item_with_token_id() {1106 new_test_ext().execute_with(|| {1107 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));11081109 let origin1 = Origin::signed(1);1110 assert_ok!(TemplateModule::add_collection_admin(1111 origin1.clone(),1112 collection_id,1113 account(2)1114 ));11151116 let data = default_fungible_data();1117 create_test_item(collection_id, &data.into());11181119 // check balance (collection with id = 1, user id = 1)1120 assert_eq!(1121 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),1122 51123 );11241125 // Try to burn item using Token ID1126 assert_noop!(1127 TemplateModule::burn_item(origin1, CollectionId(1), TokenId(1), 5).map_err(|e| e.error),1128 <pallet_fungible::Error::<Test>>::FungibleItemsHaveNoId1129 );1130 });1131}1132#[test]1133fn burn_refungible_item() {1134 new_test_ext().execute_with(|| {1135 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));1136 let origin1 = Origin::signed(1);11371138 assert_ok!(TemplateModule::set_mint_permission(1139 origin1.clone(),1140 collection_id,1141 true1142 ));1143 assert_ok!(TemplateModule::set_public_access_mode(1144 origin1.clone(),1145 collection_id,1146 AccessMode::AllowList1147 ));1148 assert_ok!(TemplateModule::add_to_allow_list(1149 origin1.clone(),1150 collection_id,1151 account(1)1152 ));11531154 assert_ok!(TemplateModule::add_collection_admin(1155 origin1.clone(),1156 collection_id,1157 account(2)1158 ));11591160 let data = default_re_fungible_data();1161 create_test_item(collection_id, &data.into());11621163 // check balance (collection with id = 1, user id = 2)1164 assert_eq!(1165 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),1166 11167 );1168 assert_eq!(1169 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),1170 10231171 );11721173 // burn item1174 assert_ok!(TemplateModule::burn_item(1175 origin1.clone(),1176 collection_id,1177 TokenId(1),1178 10231179 ));1180 assert_noop!(1181 TemplateModule::burn_item(origin1, collection_id, TokenId(1), 1023)1182 .map_err(|e| e.error),1183 CommonError::<Test>::TokenValueTooLow1184 );11851186 assert_eq!(1187 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),1188 01189 );1190 });1191}11921193#[test]1194fn add_collection_admin() {1195 new_test_ext().execute_with(|| {1196 let collection1_id =1197 create_test_collection_for_owner(&CollectionMode::NFT, 1, CollectionId(1));1198 let origin1 = Origin::signed(1);11991200 // Add collection admins1201 assert_ok!(TemplateModule::add_collection_admin(1202 origin1.clone(),1203 collection1_id,1204 account(2)1205 ));1206 assert_ok!(TemplateModule::add_collection_admin(1207 origin1,1208 collection1_id,1209 account(3)1210 ));12111212 // Owner is not an admin by default1213 assert_eq!(1214 <pallet_common::IsAdmin<Test>>::get((CollectionId(1), account(1))),1215 false1216 );1217 assert!(<pallet_common::IsAdmin<Test>>::get((1218 CollectionId(1),1219 account(2)1220 )));1221 assert!(<pallet_common::IsAdmin<Test>>::get((1222 CollectionId(1),1223 account(3)1224 )));1225 });1226}12271228#[test]1229fn remove_collection_admin() {1230 new_test_ext().execute_with(|| {1231 let collection1_id =1232 create_test_collection_for_owner(&CollectionMode::NFT, 1, CollectionId(1));1233 let origin1 = Origin::signed(1);1234 let origin2 = Origin::signed(2);12351236 // Add collection admins 2 and 31237 assert_ok!(TemplateModule::add_collection_admin(1238 origin1.clone(),1239 collection1_id,1240 account(2)1241 ));1242 assert_ok!(TemplateModule::add_collection_admin(1243 origin1,1244 collection1_id,1245 account(3)1246 ));12471248 assert!(<pallet_common::IsAdmin<Test>>::get((1249 CollectionId(1),1250 account(2)1251 )));1252 assert!(<pallet_common::IsAdmin<Test>>::get((1253 CollectionId(1),1254 account(3)1255 )));12561257 // remove admin 31258 assert_ok!(TemplateModule::remove_collection_admin(1259 origin2,1260 CollectionId(1),1261 account(3)1262 ));12631264 // 2 is still admin, 3 is not an admin anymore1265 assert!(<pallet_common::IsAdmin<Test>>::get((1266 CollectionId(1),1267 account(2)1268 )));1269 assert_eq!(1270 <pallet_common::IsAdmin<Test>>::get((CollectionId(1), account(3))),1271 false1272 );1273 });1274}12751276#[test]1277fn balance_of() {1278 new_test_ext().execute_with(|| {1279 let nft_collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1280 let fungible_collection_id =1281 create_test_collection(&CollectionMode::Fungible(3), CollectionId(2));1282 let re_fungible_collection_id =1283 create_test_collection(&CollectionMode::ReFungible, CollectionId(3));12841285 // check balance before1286 assert_eq!(1287 <pallet_nonfungible::AccountBalance<Test>>::get((nft_collection_id, account(1))),1288 01289 );1290 assert_eq!(1291 <pallet_fungible::Balance<Test>>::get((fungible_collection_id, account(1))),1292 01293 );1294 assert_eq!(1295 <pallet_refungible::AccountBalance<Test>>::get((re_fungible_collection_id, account(1))),1296 01297 );12981299 let nft_data = default_nft_data();1300 create_test_item(nft_collection_id, &nft_data.into());13011302 let fungible_data = default_fungible_data();1303 create_test_item(fungible_collection_id, &fungible_data.into());13041305 let re_fungible_data = default_re_fungible_data();1306 create_test_item(re_fungible_collection_id, &re_fungible_data.into());13071308 // check balance (collection with id = 1, user id = 1)1309 assert_eq!(1310 <pallet_nonfungible::AccountBalance<Test>>::get((nft_collection_id, account(1))),1311 11312 );1313 assert_eq!(1314 <pallet_fungible::Balance<Test>>::get((fungible_collection_id, account(1))),1315 51316 );1317 assert_eq!(1318 <pallet_refungible::AccountBalance<Test>>::get((re_fungible_collection_id, account(1))),1319 11320 );13211322 assert_eq!(1323 <pallet_nonfungible::Owned<Test>>::get((nft_collection_id, account(1), TokenId(1))),1324 true1325 );1326 assert_eq!(1327 <pallet_refungible::Owned<Test>>::get((1328 re_fungible_collection_id,1329 account(1),1330 TokenId(1)1331 )),1332 true1333 );1334 });1335}13361337#[test]1338fn approve() {1339 new_test_ext().execute_with(|| {1340 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));13411342 let data = default_nft_data();1343 create_test_item(collection_id, &data.into());13441345 let origin1 = Origin::signed(1);13461347 // approve1348 assert_ok!(TemplateModule::approve(1349 origin1,1350 account(2),1351 CollectionId(1),1352 TokenId(1),1353 11354 ));1355 assert_eq!(1356 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1357 account(2)1358 );1359 });1360}13611362#[test]1363fn transfer_from() {1364 new_test_ext().execute_with(|| {1365 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1366 let origin1 = Origin::signed(1);1367 let origin2 = Origin::signed(2);13681369 let data = default_nft_data();1370 create_test_item(collection_id, &data.into());13711372 // approve1373 assert_ok!(TemplateModule::approve(1374 origin1.clone(),1375 account(2),1376 CollectionId(1),1377 TokenId(1),1378 11379 ));1380 assert_eq!(1381 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1382 account(2)1383 );13841385 assert_ok!(TemplateModule::set_mint_permission(1386 origin1.clone(),1387 CollectionId(1),1388 true1389 ));1390 assert_ok!(TemplateModule::set_public_access_mode(1391 origin1.clone(),1392 CollectionId(1),1393 AccessMode::AllowList1394 ));1395 assert_ok!(TemplateModule::add_to_allow_list(1396 origin1.clone(),1397 CollectionId(1),1398 account(1)1399 ));1400 assert_ok!(TemplateModule::add_to_allow_list(1401 origin1.clone(),1402 CollectionId(1),1403 account(2)1404 ));1405 assert_ok!(TemplateModule::add_to_allow_list(1406 origin1,1407 CollectionId(1),1408 account(3)1409 ));14101411 assert_ok!(TemplateModule::transfer_from(1412 origin2,1413 account(1),1414 account(2),1415 CollectionId(1),1416 TokenId(1),1417 11418 ));14191420 // after transfer1421 assert_eq!(1422 <pallet_nonfungible::AccountBalance<Test>>::get((CollectionId(1), account(1))),1423 01424 );1425 assert_eq!(1426 <pallet_nonfungible::AccountBalance<Test>>::get((CollectionId(1), account(2))),1427 11428 );1429 });1430}14311432// #endregion14331434// Coverage tests region1435// #region14361437#[test]1438fn owner_can_add_address_to_allow_list() {1439 new_test_ext().execute_with(|| {1440 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));14411442 let origin1 = Origin::signed(1);1443 assert_ok!(TemplateModule::add_to_allow_list(1444 origin1,1445 collection_id,1446 account(2)1447 ));1448 assert!(<pallet_common::Allowlist<Test>>::get((1449 collection_id,1450 account(2)1451 )));1452 });1453}14541455#[test]1456fn admin_can_add_address_to_allow_list() {1457 new_test_ext().execute_with(|| {1458 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1459 let origin1 = Origin::signed(1);1460 let origin2 = Origin::signed(2);14611462 assert_ok!(TemplateModule::add_collection_admin(1463 origin1,1464 collection_id,1465 account(2)1466 ));1467 assert_ok!(TemplateModule::add_to_allow_list(1468 origin2,1469 collection_id,1470 account(3)1471 ));1472 assert!(<pallet_common::Allowlist<Test>>::get((1473 collection_id,1474 account(3)1475 )));1476 });1477}14781479#[test]1480fn nonprivileged_user_cannot_add_address_to_allow_list() {1481 new_test_ext().execute_with(|| {1482 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));14831484 let origin2 = Origin::signed(2);1485 assert_noop!(1486 TemplateModule::add_to_allow_list(origin2, collection_id, account(3)),1487 CommonError::<Test>::NoPermission1488 );1489 });1490}14911492#[test]1493fn nobody_can_add_address_to_allow_list_of_nonexisting_collection() {1494 new_test_ext().execute_with(|| {1495 let origin1 = Origin::signed(1);14961497 assert_noop!(1498 TemplateModule::add_to_allow_list(origin1, CollectionId(1), account(2)),1499 CommonError::<Test>::CollectionNotFound1500 );1501 });1502}15031504#[test]1505fn nobody_can_add_address_to_allow_list_of_deleted_collection() {1506 new_test_ext().execute_with(|| {1507 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));15081509 let origin1 = Origin::signed(1);1510 assert_ok!(TemplateModule::destroy_collection(1511 origin1.clone(),1512 collection_id1513 ));1514 assert_noop!(1515 TemplateModule::add_to_allow_list(origin1, collection_id, account(2)),1516 CommonError::<Test>::CollectionNotFound1517 );1518 });1519}15201521// If address is already added to allow list, nothing happens1522#[test]1523fn address_is_already_added_to_allow_list() {1524 new_test_ext().execute_with(|| {1525 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1526 let origin1 = Origin::signed(1);15271528 assert_ok!(TemplateModule::add_to_allow_list(1529 origin1.clone(),1530 collection_id,1531 account(2)1532 ));1533 assert_ok!(TemplateModule::add_to_allow_list(1534 origin1,1535 collection_id,1536 account(2)1537 ));1538 assert!(<pallet_common::Allowlist<Test>>::get((1539 collection_id,1540 account(2)1541 )));1542 });1543}15441545#[test]1546fn owner_can_remove_address_from_allow_list() {1547 new_test_ext().execute_with(|| {1548 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));15491550 let origin1 = Origin::signed(1);1551 assert_ok!(TemplateModule::add_to_allow_list(1552 origin1.clone(),1553 collection_id,1554 account(2)1555 ));1556 assert_ok!(TemplateModule::remove_from_allow_list(1557 origin1,1558 collection_id,1559 account(2)1560 ));1561 assert_eq!(1562 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1563 false1564 );1565 });1566}15671568#[test]1569fn admin_can_remove_address_from_allow_list() {1570 new_test_ext().execute_with(|| {1571 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1572 let origin1 = Origin::signed(1);1573 let origin2 = Origin::signed(2);15741575 // Owner adds admin1576 assert_ok!(TemplateModule::add_collection_admin(1577 origin1.clone(),1578 collection_id,1579 account(2)1580 ));15811582 // Owner adds address 3 to allow list1583 assert_ok!(TemplateModule::add_to_allow_list(1584 origin1,1585 collection_id,1586 account(3)1587 ));15881589 // Admin removes address 3 from allow list1590 assert_ok!(TemplateModule::remove_from_allow_list(1591 origin2,1592 collection_id,1593 account(3)1594 ));1595 assert_eq!(1596 <pallet_common::Allowlist<Test>>::get((collection_id, account(3))),1597 false1598 );1599 });1600}16011602#[test]1603fn nonprivileged_user_cannot_remove_address_from_allow_list() {1604 new_test_ext().execute_with(|| {1605 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1606 let origin1 = Origin::signed(1);1607 let origin2 = Origin::signed(2);16081609 assert_ok!(TemplateModule::add_to_allow_list(1610 origin1,1611 collection_id,1612 account(2)1613 ));1614 assert_noop!(1615 TemplateModule::remove_from_allow_list(origin2, collection_id, account(2)),1616 CommonError::<Test>::NoPermission1617 );1618 assert!(<pallet_common::Allowlist<Test>>::get((1619 collection_id,1620 account(2)1621 )));1622 });1623}16241625#[test]1626fn nobody_can_remove_address_from_allow_list_of_nonexisting_collection() {1627 new_test_ext().execute_with(|| {1628 let origin1 = Origin::signed(1);16291630 assert_noop!(1631 TemplateModule::remove_from_allow_list(origin1, CollectionId(1), account(2)),1632 CommonError::<Test>::CollectionNotFound1633 );1634 });1635}16361637#[test]1638fn nobody_can_remove_address_from_allow_list_of_deleted_collection() {1639 new_test_ext().execute_with(|| {1640 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1641 let origin1 = Origin::signed(1);1642 let origin2 = Origin::signed(2);16431644 // Add account 2 to allow list1645 assert_ok!(TemplateModule::add_to_allow_list(1646 origin1.clone(),1647 collection_id,1648 account(2)1649 ));16501651 // Account 2 is in collection allow-list1652 assert!(<pallet_common::Allowlist<Test>>::get((1653 collection_id,1654 account(2)1655 )));16561657 // Destroy collection1658 assert_ok!(TemplateModule::destroy_collection(origin1, collection_id));16591660 // Attempt to remove account 2 from collection allow-list => error1661 assert_noop!(1662 TemplateModule::remove_from_allow_list(origin2, collection_id, account(2)),1663 CommonError::<Test>::CollectionNotFound1664 );16651666 // Account 2 is not found in collection allow-list anyway1667 assert_eq!(1668 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1669 false1670 );1671 });1672}16731674// If address is already removed from allow list, nothing happens1675#[test]1676fn address_is_already_removed_from_allow_list() {1677 new_test_ext().execute_with(|| {1678 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1679 let origin1 = Origin::signed(1);16801681 assert_ok!(TemplateModule::add_to_allow_list(1682 origin1.clone(),1683 collection_id,1684 account(2)1685 ));1686 assert_ok!(TemplateModule::remove_from_allow_list(1687 origin1.clone(),1688 collection_id,1689 account(2)1690 ));1691 assert_eq!(1692 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1693 false1694 );1695 assert_ok!(TemplateModule::remove_from_allow_list(1696 origin1,1697 collection_id,1698 account(2)1699 ));1700 assert_eq!(1701 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1702 false1703 );1704 });1705}17061707// If Public Access mode is set to AllowList, tokens can’t be transferred from a non-allowlisted address with transfer or transferFrom (2 tests)1708#[test]1709fn allow_list_test_1() {1710 new_test_ext().execute_with(|| {1711 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));17121713 let origin1 = Origin::signed(1);17141715 let data = default_nft_data();1716 create_test_item(collection_id, &data.into());17171718 assert_ok!(TemplateModule::set_public_access_mode(1719 origin1.clone(),1720 collection_id,1721 AccessMode::AllowList1722 ));1723 assert_ok!(TemplateModule::add_to_allow_list(1724 origin1.clone(),1725 collection_id,1726 account(2)1727 ));17281729 assert_noop!(1730 TemplateModule::transfer(origin1, account(3), CollectionId(1), TokenId(1), 1)1731 .map_err(|e| e.error),1732 CommonError::<Test>::AddressNotInAllowlist1733 );1734 });1735}17361737#[test]1738fn allow_list_test_2() {1739 new_test_ext().execute_with(|| {1740 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1741 let origin1 = Origin::signed(1);17421743 let data = default_nft_data();1744 create_test_item(collection_id, &data.into());17451746 assert_ok!(TemplateModule::set_public_access_mode(1747 origin1.clone(),1748 collection_id,1749 AccessMode::AllowList1750 ));1751 assert_ok!(TemplateModule::add_to_allow_list(1752 origin1.clone(),1753 collection_id,1754 account(1)1755 ));1756 assert_ok!(TemplateModule::add_to_allow_list(1757 origin1.clone(),1758 collection_id,1759 account(2)1760 ));17611762 // do approve1763 assert_ok!(TemplateModule::approve(1764 origin1.clone(),1765 account(1),1766 collection_id,1767 TokenId(1),1768 11769 ));1770 assert_eq!(1771 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1772 account(1)1773 );17741775 assert_ok!(TemplateModule::remove_from_allow_list(1776 origin1.clone(),1777 collection_id,1778 account(1)1779 ));17801781 assert_noop!(1782 TemplateModule::transfer_from(1783 origin1,1784 account(1),1785 account(3),1786 CollectionId(1),1787 TokenId(1),1788 11789 )1790 .map_err(|e| e.error),1791 CommonError::<Test>::AddressNotInAllowlist1792 );1793 });1794}17951796// If Public Access mode is set to AllowList, tokens can’t be transferred to a non-allowlisted address with transfer or transferFrom (2 tests)1797#[test]1798fn allow_list_test_3() {1799 new_test_ext().execute_with(|| {1800 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));18011802 let origin1 = Origin::signed(1);18031804 let data = default_nft_data();1805 create_test_item(collection_id, &data.into());18061807 assert_ok!(TemplateModule::set_public_access_mode(1808 origin1.clone(),1809 collection_id,1810 AccessMode::AllowList1811 ));1812 assert_ok!(TemplateModule::add_to_allow_list(1813 origin1.clone(),1814 collection_id,1815 account(1)1816 ));18171818 assert_noop!(1819 TemplateModule::transfer(origin1, account(3), collection_id, TokenId(1), 1)1820 .map_err(|e| e.error),1821 CommonError::<Test>::AddressNotInAllowlist1822 );1823 });1824}18251826#[test]1827fn allow_list_test_4() {1828 new_test_ext().execute_with(|| {1829 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));18301831 let origin1 = Origin::signed(1);18321833 let data = default_nft_data();1834 create_test_item(collection_id, &data.into());18351836 assert_ok!(TemplateModule::set_public_access_mode(1837 origin1.clone(),1838 collection_id,1839 AccessMode::AllowList1840 ));1841 assert_ok!(TemplateModule::add_to_allow_list(1842 origin1.clone(),1843 collection_id,1844 account(1)1845 ));1846 assert_ok!(TemplateModule::add_to_allow_list(1847 origin1.clone(),1848 collection_id,1849 account(2)1850 ));18511852 // do approve1853 assert_ok!(TemplateModule::approve(1854 origin1.clone(),1855 account(1),1856 collection_id,1857 TokenId(1),1858 11859 ));1860 assert_eq!(1861 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1862 account(1)1863 );18641865 assert_ok!(TemplateModule::remove_from_allow_list(1866 origin1.clone(),1867 collection_id,1868 account(2)1869 ));18701871 assert_noop!(1872 TemplateModule::transfer_from(1873 origin1,1874 account(1),1875 account(3),1876 collection_id,1877 TokenId(1),1878 11879 )1880 .map_err(|e| e.error),1881 CommonError::<Test>::AddressNotInAllowlist1882 );1883 });1884}18851886// If Public Access mode is set to AllowList, tokens can’t be destroyed by a non-allowlisted address (even if it owned them before enabling AllowList mode)1887#[test]1888fn allow_list_test_5() {1889 new_test_ext().execute_with(|| {1890 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));18911892 let origin1 = Origin::signed(1);18931894 let data = default_nft_data();1895 create_test_item(collection_id, &data.into());18961897 assert_ok!(TemplateModule::set_public_access_mode(1898 origin1.clone(),1899 collection_id,1900 AccessMode::AllowList1901 ));1902 assert_noop!(1903 TemplateModule::burn_item(origin1.clone(), CollectionId(1), TokenId(1), 1)1904 .map_err(|e| e.error),1905 CommonError::<Test>::AddressNotInAllowlist1906 );1907 });1908}19091910// If Public Access mode is set to AllowList, token transfers can’t be Approved by a non-allowlisted address (see Approve method).1911#[test]1912fn allow_list_test_6() {1913 new_test_ext().execute_with(|| {1914 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));19151916 let origin1 = Origin::signed(1);19171918 let data = default_nft_data();1919 create_test_item(collection_id, &data.into());19201921 assert_ok!(TemplateModule::set_public_access_mode(1922 origin1.clone(),1923 collection_id,1924 AccessMode::AllowList1925 ));19261927 // do approve1928 assert_noop!(1929 TemplateModule::approve(origin1, account(1), CollectionId(1), TokenId(1), 1)1930 .map_err(|e| e.error),1931 CommonError::<Test>::AddressNotInAllowlist1932 );1933 });1934}19351936// If Public Access mode is set to AllowList, tokens can be transferred from a allowlisted address with transfer or transferFrom (2 tests) and1937// tokens can be transferred from a allowlisted address with transfer or transferFrom (2 tests)1938#[test]1939fn allow_list_test_7() {1940 new_test_ext().execute_with(|| {1941 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));19421943 let data = default_nft_data();1944 create_test_item(collection_id, &data.into());19451946 let origin1 = Origin::signed(1);19471948 assert_ok!(TemplateModule::set_public_access_mode(1949 origin1.clone(),1950 collection_id,1951 AccessMode::AllowList1952 ));1953 assert_ok!(TemplateModule::add_to_allow_list(1954 origin1.clone(),1955 collection_id,1956 account(1)1957 ));1958 assert_ok!(TemplateModule::add_to_allow_list(1959 origin1.clone(),1960 collection_id,1961 account(2)1962 ));19631964 assert_ok!(TemplateModule::transfer(1965 origin1,1966 account(2),1967 CollectionId(1),1968 TokenId(1),1969 11970 ));1971 });1972}19731974#[test]1975fn allow_list_test_8() {1976 new_test_ext().execute_with(|| {1977 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));19781979 // Create NFT for account 11980 let data = default_nft_data();1981 create_test_item(collection_id, &data.into());19821983 let origin1 = Origin::signed(1);19841985 // Toggle Allow List mode and add accounts 1 and 21986 assert_ok!(TemplateModule::set_public_access_mode(1987 origin1.clone(),1988 collection_id,1989 AccessMode::AllowList1990 ));1991 assert_ok!(TemplateModule::add_to_allow_list(1992 origin1.clone(),1993 collection_id,1994 account(1)1995 ));1996 assert_ok!(TemplateModule::add_to_allow_list(1997 origin1.clone(),1998 collection_id,1999 account(2)2000 ));20012002 // Sself-approve account 1 for NFT 12003 assert_ok!(TemplateModule::approve(2004 origin1.clone(),2005 account(1),2006 CollectionId(1),2007 TokenId(1),2008 12009 ));2010 assert_eq!(2011 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),2012 account(1)2013 );20142015 // Transfer from 1 to 22016 assert_ok!(TemplateModule::transfer_from(2017 origin1,2018 account(1),2019 account(2),2020 CollectionId(1),2021 TokenId(1),2022 12023 ));2024 });2025}20262027// If Public Access mode is set to AllowList, and Mint Permission is set to false, tokens can be created by owner.2028#[test]2029fn allow_list_test_9() {2030 new_test_ext().execute_with(|| {2031 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));2032 let origin1 = Origin::signed(1);20332034 assert_ok!(TemplateModule::set_public_access_mode(2035 origin1.clone(),2036 collection_id,2037 AccessMode::AllowList2038 ));2039 assert_ok!(TemplateModule::set_mint_permission(2040 origin1,2041 collection_id,2042 false2043 ));20442045 let data = default_nft_data();2046 create_test_item(collection_id, &data.into());2047 });2048}20492050// If Public Access mode is set to AllowList, and Mint Permission is set to false, tokens can be created by admin.2051#[test]2052fn allow_list_test_10() {2053 new_test_ext().execute_with(|| {2054 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));20552056 let origin1 = Origin::signed(1);2057 let origin2 = Origin::signed(2);20582059 assert_ok!(TemplateModule::set_public_access_mode(2060 origin1.clone(),2061 collection_id,2062 AccessMode::AllowList2063 ));2064 assert_ok!(TemplateModule::set_mint_permission(2065 origin1.clone(),2066 collection_id,2067 false2068 ));20692070 assert_ok!(TemplateModule::add_collection_admin(2071 origin1,2072 collection_id,2073 account(2)2074 ));20752076 assert_ok!(TemplateModule::create_item(2077 origin2,2078 collection_id,2079 account(2),2080 default_nft_data().into()2081 ));2082 });2083}20842085// If Public Access mode is set to AllowList, and Mint Permission is set to false, tokens cannot be created by non-privileged and allow listed address.2086#[test]2087fn allow_list_test_11() {2088 new_test_ext().execute_with(|| {2089 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));20902091 let origin1 = Origin::signed(1);2092 let origin2 = Origin::signed(2);20932094 assert_ok!(TemplateModule::set_public_access_mode(2095 origin1.clone(),2096 collection_id,2097 AccessMode::AllowList2098 ));2099 assert_ok!(TemplateModule::set_mint_permission(2100 origin1.clone(),2101 collection_id,2102 false2103 ));2104 assert_ok!(TemplateModule::add_to_allow_list(2105 origin1,2106 collection_id,2107 account(2)2108 ));21092110 assert_noop!(2111 TemplateModule::create_item(2112 origin2,2113 CollectionId(1),2114 account(2),2115 default_nft_data().into()2116 )2117 .map_err(|e| e.error),2118 CommonError::<Test>::PublicMintingNotAllowed2119 );2120 });2121}21222123// If Public Access mode is set to AllowList, and Mint Permission is set to false, tokens cannot be created by non-privileged and non-allow listed address.2124#[test]2125fn allow_list_test_12() {2126 new_test_ext().execute_with(|| {2127 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));21282129 let origin1 = Origin::signed(1);2130 let origin2 = Origin::signed(2);21312132 assert_ok!(TemplateModule::set_public_access_mode(2133 origin1.clone(),2134 collection_id,2135 AccessMode::AllowList2136 ));2137 assert_ok!(TemplateModule::set_mint_permission(2138 origin1,2139 collection_id,2140 false2141 ));21422143 assert_noop!(2144 TemplateModule::create_item(2145 origin2,2146 CollectionId(1),2147 account(2),2148 default_nft_data().into()2149 )2150 .map_err(|e| e.error),2151 CommonError::<Test>::PublicMintingNotAllowed2152 );2153 });2154}21552156// If Public Access mode is set to AllowList, and Mint Permission is set to true, tokens can be created by owner.2157#[test]2158fn allow_list_test_13() {2159 new_test_ext().execute_with(|| {2160 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));21612162 let origin1 = Origin::signed(1);21632164 assert_ok!(TemplateModule::set_public_access_mode(2165 origin1.clone(),2166 collection_id,2167 AccessMode::AllowList2168 ));2169 assert_ok!(TemplateModule::set_mint_permission(2170 origin1,2171 collection_id,2172 true2173 ));21742175 let data = default_nft_data();2176 create_test_item(collection_id, &data.into());2177 });2178}21792180// If Public Access mode is set to AllowList, and Mint Permission is set to true, tokens can be created by admin.2181#[test]2182fn allow_list_test_14() {2183 new_test_ext().execute_with(|| {2184 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));21852186 let origin1 = Origin::signed(1);2187 let origin2 = Origin::signed(2);21882189 assert_ok!(TemplateModule::set_public_access_mode(2190 origin1.clone(),2191 collection_id,2192 AccessMode::AllowList2193 ));2194 assert_ok!(TemplateModule::set_mint_permission(2195 origin1.clone(),2196 collection_id,2197 true2198 ));21992200 assert_ok!(TemplateModule::add_collection_admin(2201 origin1,2202 collection_id,2203 account(2)2204 ));22052206 assert_ok!(TemplateModule::create_item(2207 origin2,2208 collection_id,2209 account(2),2210 default_nft_data().into()2211 ));2212 });2213}22142215// If Public Access mode is set to AllowList, and Mint Permission is set to true, tokens cannot be created by non-privileged and non-allow listed address.2216#[test]2217fn allow_list_test_15() {2218 new_test_ext().execute_with(|| {2219 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));22202221 let origin1 = Origin::signed(1);2222 let origin2 = Origin::signed(2);22232224 assert_ok!(TemplateModule::set_public_access_mode(2225 origin1.clone(),2226 collection_id,2227 AccessMode::AllowList2228 ));2229 assert_ok!(TemplateModule::set_mint_permission(2230 origin1,2231 collection_id,2232 true2233 ));22342235 assert_noop!(2236 TemplateModule::create_item(2237 origin2,2238 collection_id,2239 account(2),2240 default_nft_data().into()2241 )2242 .map_err(|e| e.error),2243 CommonError::<Test>::AddressNotInAllowlist2244 );2245 });2246}22472248// If Public Access mode is set to AllowList, and Mint Permission is set to true, tokens can be created by non-privileged and allow listed address.2249#[test]2250fn allow_list_test_16() {2251 new_test_ext().execute_with(|| {2252 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));22532254 let origin1 = Origin::signed(1);2255 let origin2 = Origin::signed(2);22562257 assert_ok!(TemplateModule::set_public_access_mode(2258 origin1.clone(),2259 collection_id,2260 AccessMode::AllowList2261 ));2262 assert_ok!(TemplateModule::set_mint_permission(2263 origin1.clone(),2264 collection_id,2265 true2266 ));2267 assert_ok!(TemplateModule::add_to_allow_list(2268 origin1,2269 collection_id,2270 account(2)2271 ));22722273 assert_ok!(TemplateModule::create_item(2274 origin2,2275 collection_id,2276 account(2),2277 default_nft_data().into()2278 ));2279 });2280}22812282// Total number of collections. Positive test2283#[test]2284fn total_number_collections_bound() {2285 new_test_ext().execute_with(|| {2286 create_test_collection(&CollectionMode::NFT, CollectionId(1));2287 });2288}22892290#[test]2291fn create_max_collections() {2292 new_test_ext().execute_with(|| {2293 for i in 1..COLLECTION_NUMBER_LIMIT {2294 create_test_collection(&CollectionMode::NFT, CollectionId(i));2295 }2296 });2297}22982299// Total number of collections. Negative test2300#[test]2301fn total_number_collections_bound_neg() {2302 new_test_ext().execute_with(|| {2303 let origin1 = Origin::signed(1);23042305 for i in 1..=COLLECTION_NUMBER_LIMIT {2306 create_test_collection(&CollectionMode::NFT, CollectionId(i));2307 }23082309 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();2310 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();2311 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();23122313 // 11-th collection in chain. Expects error2314 assert_noop!(2315 TemplateModule::create_collection(2316 origin1,2317 col_name1.try_into().unwrap(),2318 col_desc1.try_into().unwrap(),2319 token_prefix1.try_into().unwrap(),2320 CollectionMode::NFT2321 ),2322 CommonError::<Test>::TotalCollectionsLimitExceeded2323 );2324 });2325}23262327// Owned tokens by a single address. Positive test2328#[test]2329fn owned_tokens_bound() {2330 new_test_ext().execute_with(|| {2331 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23322333 let data = default_nft_data();2334 create_test_item(collection_id, &data.clone().into());2335 create_test_item(collection_id, &data.into());2336 });2337}23382339// Owned tokens by a single address. Negotive test2340#[test]2341fn owned_tokens_bound_neg() {2342 new_test_ext().execute_with(|| {2343 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23442345 let origin1 = Origin::signed(1);23462347 for _ in 1..=MAX_TOKEN_OWNERSHIP {2348 let data = default_nft_data();2349 create_test_item(collection_id, &data.clone().into());2350 }23512352 let data = default_nft_data();2353 assert_noop!(2354 TemplateModule::create_item(origin1, CollectionId(1), account(1), data.into())2355 .map_err(|e| e.error),2356 CommonError::<Test>::AccountTokenLimitExceeded2357 );2358 });2359}23602361// Number of collection admins. Positive test2362#[test]2363fn collection_admins_bound() {2364 new_test_ext().execute_with(|| {2365 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23662367 let origin1 = Origin::signed(1);23682369 assert_ok!(TemplateModule::add_collection_admin(2370 origin1.clone(),2371 collection_id,2372 account(2)2373 ));2374 assert_ok!(TemplateModule::add_collection_admin(2375 origin1,2376 collection_id,2377 account(3)2378 ));2379 });2380}23812382// Number of collection admins. Negotive test2383#[test]2384fn collection_admins_bound_neg() {2385 new_test_ext().execute_with(|| {2386 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23872388 let origin1 = Origin::signed(1);23892390 for i in 0..COLLECTION_ADMINS_LIMIT {2391 assert_ok!(TemplateModule::add_collection_admin(2392 origin1.clone(),2393 collection_id,2394 account((2 + i).into())2395 ));2396 }2397 assert_noop!(2398 TemplateModule::add_collection_admin(2399 origin1,2400 collection_id,2401 account((3 + COLLECTION_ADMINS_LIMIT).into())2402 ),2403 CommonError::<Test>::CollectionAdminCountExceeded2404 );2405 });2406}2407// #endregion24082409#[test]2410fn set_const_on_chain_schema() {2411 new_test_ext().execute_with(|| {2412 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));24132414 let origin1 = Origin::signed(1);2415 assert_ok!(TemplateModule::set_const_on_chain_schema(2416 origin1,2417 collection_id,2418 b"test const on chain schema".to_vec().try_into().unwrap()2419 ));24202421 assert_eq!(2422 <pallet_common::CollectionById<Test>>::get(collection_id)2423 .unwrap()2424 .const_on_chain_schema,2425 b"test const on chain schema".to_vec()2426 );2427 assert_eq!(2428 <pallet_common::CollectionById<Test>>::get(collection_id)2429 .unwrap()2430 .variable_on_chain_schema,2431 b"".to_vec()2432 );2433 });2434}24352436#[test]2437fn set_variable_on_chain_schema() {2438 new_test_ext().execute_with(|| {2439 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));24402441 let origin1 = Origin::signed(1);2442 assert_ok!(TemplateModule::set_variable_on_chain_schema(2443 origin1,2444 collection_id,2445 b"test variable on chain schema"2446 .to_vec()2447 .try_into()2448 .unwrap()2449 ));24502451 assert_eq!(2452 <pallet_common::CollectionById<Test>>::get(collection_id)2453 .unwrap()2454 .const_on_chain_schema,2455 b"".to_vec()2456 );2457 assert_eq!(2458 <pallet_common::CollectionById<Test>>::get(collection_id)2459 .unwrap()2460 .variable_on_chain_schema,2461 b"test variable on chain schema".to_vec()2462 );2463 });2464}24652466#[test]2467fn set_variable_meta_data_on_nft_token_stores_variable_meta_data() {2468 new_test_ext().execute_with(|| {2469 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));24702471 let origin1 = Origin::signed(1);24722473 let data = default_nft_data();2474 create_test_item(CollectionId(1), &data.into());24752476 let variable_data = b"test data".to_vec();2477 assert_ok!(TemplateModule::set_variable_meta_data(2478 origin1,2479 collection_id,2480 TokenId(1),2481 variable_data.clone().try_into().unwrap()2482 ));24832484 assert_eq!(2485 <pallet_nonfungible::TokenData<Test>>::get((collection_id, 1))2486 .unwrap()2487 .variable_data,2488 variable_data2489 );2490 });2491}24922493#[test]2494fn set_variable_meta_data_on_re_fungible_token_stores_variable_meta_data() {2495 new_test_ext().execute_with(|| {2496 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));24972498 let origin1 = Origin::signed(1);24992500 let data = default_re_fungible_data();2501 create_test_item(collection_id, &data.into());25022503 let variable_data = b"test data".to_vec();2504 assert_ok!(TemplateModule::set_variable_meta_data(2505 origin1,2506 collection_id,2507 TokenId(1),2508 variable_data.clone().try_into().unwrap()2509 ));25102511 assert_eq!(2512 <pallet_refungible::TokenData<Test>>::get((collection_id, TokenId(1))).variable_data,2513 variable_data2514 );2515 });2516}25172518#[test]2519fn set_variable_meta_data_on_fungible_token_fails() {2520 new_test_ext().execute_with(|| {2521 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));25222523 let origin1 = Origin::signed(1);25242525 let data = default_fungible_data();2526 create_test_item(collection_id, &data.into());25272528 let variable_data = b"test data".to_vec();2529 assert_noop!(2530 TemplateModule::set_variable_meta_data(2531 origin1,2532 collection_id,2533 TokenId(0),2534 variable_data.try_into().unwrap()2535 )2536 .map_err(|e| e.error),2537 <pallet_fungible::Error<Test>>::FungibleItemsDontHaveData2538 );2539 });2540}25412542#[test]2543fn set_variable_meta_data_on_nft_with_item_owner_permission_flag() {2544 new_test_ext().execute_with(|| {2545 //default_limits();25462547 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));25482549 let origin1 = Origin::signed(1);25502551 let data = default_nft_data();2552 create_test_item(collection_id, &data.into());25532554 assert_ok!(TemplateModule::set_meta_update_permission_flag(2555 origin1.clone(),2556 collection_id,2557 MetaUpdatePermission::ItemOwner,2558 ));25592560 let variable_data = b"ten chars.".to_vec();2561 assert_ok!(TemplateModule::set_variable_meta_data(2562 origin1,2563 collection_id,2564 TokenId(1),2565 variable_data.clone().try_into().unwrap()2566 ));25672568 assert_eq!(2569 <pallet_nonfungible::TokenData<Test>>::get((collection_id, TokenId(1)))2570 .unwrap()2571 .variable_data,2572 variable_data2573 );2574 });2575}25762577#[test]2578fn collection_transfer_flag_works() {2579 new_test_ext().execute_with(|| {2580 let origin1 = Origin::signed(1);25812582 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));2583 assert_ok!(TemplateModule::set_transfers_enabled_flag(2584 origin1,2585 collection_id,2586 true2587 ));25882589 let data = default_nft_data();2590 create_test_item(collection_id, &data.into());2591 assert_eq!(2592 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2593 12594 );2595 assert_eq!(2596 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2597 true2598 );25992600 let origin1 = Origin::signed(1);26012602 // default scenario2603 assert_ok!(TemplateModule::transfer(2604 origin1,2605 account(2),2606 collection_id,2607 TokenId(1),2608 12609 ));2610 assert_eq!(2611 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2612 false2613 );2614 assert_eq!(2615 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),2616 true2617 );2618 assert_eq!(2619 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2620 02621 );2622 assert_eq!(2623 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(2))),2624 12625 );2626 });2627}26282629#[test]2630fn set_variable_meta_data_on_nft_with_admin_flag() {2631 new_test_ext().execute_with(|| {2632 // default_limits();26332634 let collection_id =2635 create_test_collection_for_owner(&CollectionMode::NFT, 2, CollectionId(1));26362637 let origin1 = Origin::signed(1);2638 let origin2 = Origin::signed(2);26392640 assert_ok!(TemplateModule::set_mint_permission(2641 origin2.clone(),2642 collection_id,2643 true2644 ));2645 assert_ok!(TemplateModule::add_to_allow_list(2646 origin2.clone(),2647 collection_id,2648 account(1)2649 ));26502651 assert_ok!(TemplateModule::add_collection_admin(2652 origin2.clone(),2653 collection_id,2654 account(1)2655 ));26562657 let data = default_nft_data();2658 create_test_item(collection_id, &data.into());26592660 assert_ok!(TemplateModule::set_meta_update_permission_flag(2661 origin2.clone(),2662 collection_id,2663 MetaUpdatePermission::Admin,2664 ));26652666 let variable_data = b"test.".to_vec();2667 assert_ok!(TemplateModule::set_variable_meta_data(2668 origin1,2669 collection_id,2670 TokenId(1),2671 variable_data.clone().try_into().unwrap()2672 ));26732674 assert_eq!(2675 <pallet_nonfungible::TokenData<Test>>::get((collection_id, 1))2676 .unwrap()2677 .variable_data,2678 variable_data2679 );2680 });2681}26822683#[test]2684fn set_variable_meta_data_on_nft_with_admin_flag_neg() {2685 new_test_ext().execute_with(|| {2686 // default_limits();26872688 let collection_id =2689 create_test_collection_for_owner(&CollectionMode::NFT, 2, CollectionId(1));26902691 let origin1 = Origin::signed(1);2692 let origin2 = Origin::signed(2);26932694 assert_ok!(TemplateModule::set_mint_permission(2695 origin2.clone(),2696 collection_id,2697 true2698 ));2699 assert_ok!(TemplateModule::add_to_allow_list(2700 origin2.clone(),2701 collection_id,2702 account(1)2703 ));27042705 let data = default_nft_data();2706 create_test_item(collection_id, &data.into());27072708 assert_ok!(TemplateModule::set_meta_update_permission_flag(2709 origin2.clone(),2710 collection_id,2711 MetaUpdatePermission::Admin,2712 ));27132714 let variable_data = b"test.".to_vec();2715 assert_noop!(2716 TemplateModule::set_variable_meta_data(2717 origin1,2718 collection_id,2719 TokenId(1),2720 variable_data.try_into().unwrap()2721 )2722 .map_err(|e| e.error),2723 CommonError::<Test>::NoPermission2724 );2725 });2726}27272728#[test]2729fn set_variable_meta_flag_after_freeze() {2730 new_test_ext().execute_with(|| {2731 // default_limits();27322733 let collection_id =2734 create_test_collection_for_owner(&CollectionMode::NFT, 2, CollectionId(1));27352736 let origin2 = Origin::signed(2);27372738 assert_ok!(TemplateModule::set_meta_update_permission_flag(2739 origin2.clone(),2740 collection_id,2741 MetaUpdatePermission::None,2742 ));2743 assert_noop!(2744 TemplateModule::set_meta_update_permission_flag(2745 origin2.clone(),2746 collection_id,2747 MetaUpdatePermission::Admin2748 ),2749 CommonError::<Test>::MetadataFlagFrozen2750 );2751 });2752}27532754#[test]2755fn set_variable_meta_data_on_nft_with_none_flag_neg() {2756 new_test_ext().execute_with(|| {2757 // default_limits();27582759 let collection_id =2760 create_test_collection_for_owner(&CollectionMode::NFT, 1, CollectionId(1));2761 let origin1 = Origin::signed(1);27622763 let data = default_nft_data();2764 create_test_item(collection_id, &data.into());27652766 assert_ok!(TemplateModule::set_meta_update_permission_flag(2767 origin1.clone(),2768 collection_id,2769 MetaUpdatePermission::None,2770 ));27712772 let variable_data = b"test.".to_vec();2773 assert_noop!(2774 TemplateModule::set_variable_meta_data(2775 origin1.clone(),2776 collection_id,2777 TokenId(1),2778 variable_data.try_into().unwrap()2779 )2780 .map_err(|e| e.error),2781 CommonError::<Test>::NoPermission2782 );2783 });2784}27852786#[test]2787fn collection_transfer_flag_works_neg() {2788 new_test_ext().execute_with(|| {2789 let origin1 = Origin::signed(1);27902791 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));2792 assert_ok!(TemplateModule::set_transfers_enabled_flag(2793 origin1,2794 collection_id,2795 false2796 ));27972798 let data = default_nft_data();2799 create_test_item(collection_id, &data.into());2800 assert_eq!(2801 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2802 12803 );2804 assert_eq!(2805 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2806 true2807 );28082809 let origin1 = Origin::signed(1);28102811 // default scenario2812 assert_noop!(2813 TemplateModule::transfer(origin1, account(2), CollectionId(1), TokenId(1), 1)2814 .map_err(|e| e.error),2815 CommonError::<Test>::TransferNotAllowed2816 );2817 assert_eq!(2818 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2819 12820 );2821 assert_eq!(2822 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(2))),2823 02824 );2825 assert_eq!(2826 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2827 true2828 );2829 assert_eq!(2830 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),2831 false2832 );2833 });2834}