123456789101112131415161718use crate::{Test, TestCrossAccountId, CollectionCreationPrice, RuntimeOrigin, Unique, new_test_ext};19use up_data_structs::{20 COLLECTION_NUMBER_LIMIT, CollectionId, CreateItemData, CreateFungibleData, CreateNftData,21 CreateReFungibleData, MAX_DECIMAL_POINTS, COLLECTION_ADMINS_LIMIT, TokenId,22 MAX_TOKEN_OWNERSHIP, CreateCollectionData, CollectionMode, AccessMode, CollectionPermissions,23 PropertyKeyPermission, PropertyPermission, Property, CollectionPropertiesVec,24 CollectionPropertiesPermissionsVec,25};26use frame_support::{assert_noop, assert_ok, assert_err};27use sp_std::convert::TryInto;28use pallet_evm::account::CrossAccountId;29use pallet_common::Error as CommonError;30use pallet_unique::Error as UniqueError;3132fn add_balance(user: u64, value: u64) {33 const DONOR_USER: u64 = 999;34 assert_ok!(<pallet_balances::Pallet<Test>>::force_set_balance(35 RuntimeOrigin::root(),36 DONOR_USER,37 value,38 ));39 assert_ok!(<pallet_balances::Pallet<Test>>::force_transfer(40 RuntimeOrigin::root(),41 DONOR_USER,42 user,43 value44 ));45}4647fn default_nft_data() -> CreateNftData {48 CreateNftData {49 properties: vec![Property {50 key: b"test-prop".to_vec().try_into().unwrap(),51 value: b"test-nft-prop".to_vec().try_into().unwrap(),52 }]53 .try_into()54 .unwrap(),55 }56}5758fn default_fungible_data() -> CreateFungibleData {59 CreateFungibleData { value: 5 }60}6162fn default_re_fungible_data() -> CreateReFungibleData {63 CreateReFungibleData {64 pieces: 1023,65 properties: vec![Property {66 key: b"test-prop".to_vec().try_into().unwrap(),67 value: b"test-nft-prop".to_vec().try_into().unwrap(),68 }]69 .try_into()70 .unwrap(),71 }72}7374fn create_test_collection_for_owner(75 mode: &CollectionMode,76 owner: u64,77 id: CollectionId,78) -> CollectionId {79 add_balance(owner, CollectionCreationPrice::get() as u64 + 1);8081 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();82 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();83 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();84 let token_property_permissions: CollectionPropertiesPermissionsVec =85 vec![PropertyKeyPermission {86 key: b"test-prop".to_vec().try_into().unwrap(),87 permission: PropertyPermission {88 mutable: true,89 collection_admin: false,90 token_owner: true,91 },92 }]93 .try_into()94 .unwrap();95 let properties: CollectionPropertiesVec = vec![Property {96 key: b"test-collection-prop".to_vec().try_into().unwrap(),97 value: b"test-collection-value".to_vec().try_into().unwrap(),98 }]99 .try_into()100 .unwrap();101102 let data = CreateCollectionData {103 name: col_name1.try_into().unwrap(),104 description: col_desc1.try_into().unwrap(),105 token_prefix: token_prefix1.try_into().unwrap(),106 mode: mode.clone(),107 token_property_permissions: token_property_permissions.clone(),108 properties: properties.clone(),109 ..Default::default()110 };111112 let origin1 = RuntimeOrigin::signed(owner);113 assert_ok!(Unique::create_collection_ex(origin1, data));114115 let saved_col_name: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();116 let saved_description: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();117 let saved_prefix: Vec<u8> = b"token_prefix1\0".to_vec();118 assert_eq!(119 <pallet_common::CollectionById<Test>>::get(id)120 .unwrap()121 .owner,122 owner123 );124 assert_eq!(125 <pallet_common::CollectionById<Test>>::get(id).unwrap().name,126 saved_col_name127 );128 assert_eq!(129 <pallet_common::CollectionById<Test>>::get(id).unwrap().mode,130 *mode131 );132 assert_eq!(133 <pallet_common::CollectionById<Test>>::get(id)134 .unwrap()135 .description,136 saved_description137 );138 assert_eq!(139 <pallet_common::CollectionById<Test>>::get(id)140 .unwrap()141 .token_prefix,142 saved_prefix143 );144 assert_eq!(145 get_collection_property_permissions(id).as_slice(),146 token_property_permissions.as_slice()147 );148 assert_eq!(149 get_collection_properties(id).as_slice(),150 properties.as_slice()151 );152 id153}154155fn get_collection_property_permissions(collection_id: CollectionId) -> Vec<PropertyKeyPermission> {156 <pallet_common::Pallet<Test>>::property_permissions(collection_id)157 .into_iter()158 .map(|(key, permission)| PropertyKeyPermission { key, permission })159 .collect()160}161162fn get_collection_properties(collection_id: CollectionId) -> Vec<Property> {163 <pallet_common::Pallet<Test>>::collection_properties(collection_id)164 .into_iter()165 .map(|(key, value)| Property { key, value })166 .collect()167}168169fn get_token_properties(collection_id: CollectionId, token_id: TokenId) -> Vec<Property> {170 <pallet_nonfungible::Pallet<Test>>::token_properties((collection_id, token_id))171 .unwrap_or_default()172 .into_iter()173 .map(|(key, value)| Property { key, value })174 .collect()175}176177fn create_test_collection(mode: &CollectionMode, id: CollectionId) -> CollectionId {178 create_test_collection_for_owner(&mode, 1, id)179}180181fn create_test_item(collection_id: CollectionId, data: &CreateItemData) {182 let origin1 = RuntimeOrigin::signed(1);183 assert_ok!(Unique::create_item(184 origin1,185 collection_id,186 account(1),187 data.clone()188 ));189}190191fn account(sub: u64) -> TestCrossAccountId {192 TestCrossAccountId::from_sub(sub)193}194195196197198#[test]199fn check_not_sufficient_founds() {200 new_test_ext().execute_with(|| {201 let acc: u64 = 1;202 <pallet_balances::Pallet<Test>>::force_set_balance(RuntimeOrigin::root(), acc, 0).unwrap();203204 let name: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();205 let description: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();206 let token_prefix: Vec<u8> = b"token_prefix1\0".to_vec();207208 let data = CreateCollectionData {209 name: name.try_into().unwrap(),210 description: description.try_into().unwrap(),211 token_prefix: token_prefix.try_into().unwrap(),212 mode: CollectionMode::NFT,213 ..Default::default()214 };215216 let result = Unique::create_collection_ex(RuntimeOrigin::signed(acc), data);217 assert_err!(result, <CommonError<Test>>::NotSufficientFounds);218 });219}220221#[test]222fn create_fungible_collection_fails_with_large_decimal_numbers() {223 new_test_ext().execute_with(|| {224 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();225 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();226 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();227228 let data = CreateCollectionData {229 name: col_name1.try_into().unwrap(),230 description: col_desc1.try_into().unwrap(),231 token_prefix: token_prefix1.try_into().unwrap(),232 mode: CollectionMode::Fungible(MAX_DECIMAL_POINTS + 1),233 ..Default::default()234 };235236 let origin1 = RuntimeOrigin::signed(1);237 assert_noop!(238 Unique::create_collection_ex(origin1, data),239 UniqueError::<Test>::CollectionDecimalPointLimitExceeded240 );241 });242}243244#[test]245fn create_nft_item() {246 new_test_ext().execute_with(|| {247 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));248249 let data = default_nft_data();250 create_test_item(collection_id, &data.clone().into());251252 assert_eq!(253 get_token_properties(collection_id, TokenId(1)).as_slice(),254 data.properties.as_slice(),255 );256 });257}258259260261#[test]262fn create_nft_multiple_items() {263 new_test_ext().execute_with(|| {264 create_test_collection(&CollectionMode::NFT, CollectionId(1));265266 let origin1 = RuntimeOrigin::signed(1);267268 let items_data = vec![default_nft_data(), default_nft_data(), default_nft_data()];269270 assert_ok!(Unique::create_multiple_items(271 origin1,272 CollectionId(1),273 account(1),274 items_data275 .clone()276 .into_iter()277 .map(|d| { d.into() })278 .collect()279 ));280 for (index, data) in items_data.into_iter().enumerate() {281 assert_eq!(282 get_token_properties(CollectionId(1), TokenId(index as u32 + 1)).as_slice(),283 data.properties.as_slice()284 );285 }286 });287}288289#[test]290fn create_refungible_item() {291 new_test_ext().execute_with(|| {292 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));293294 let data = default_re_fungible_data();295 create_test_item(collection_id, &data.clone().into());296 let balance =297 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1)));298 assert_eq!(balance, 1023);299 });300}301302#[test]303fn create_multiple_refungible_items() {304 new_test_ext().execute_with(|| {305 create_test_collection(&CollectionMode::ReFungible, CollectionId(1));306307 let origin1 = RuntimeOrigin::signed(1);308309 let items_data = vec![310 default_re_fungible_data(),311 default_re_fungible_data(),312 default_re_fungible_data(),313 ];314315 assert_ok!(Unique::create_multiple_items(316 origin1,317 CollectionId(1),318 account(1),319 items_data320 .clone()321 .into_iter()322 .map(|d| { d.into() })323 .collect()324 ));325 for (index, _data) in items_data.into_iter().enumerate() {326 let balance = <pallet_refungible::Balance<Test>>::get((327 CollectionId(1),328 TokenId((index + 1) as u32),329 account(1),330 ));331 assert_eq!(balance, 1023);332 }333 });334}335336#[test]337fn create_fungible_item() {338 new_test_ext().execute_with(|| {339 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));340341 let data = default_fungible_data();342 create_test_item(collection_id, &data.into());343344 assert_eq!(345 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),346 5347 );348 });349}350351352353354355356357358359360361362363364365366367368369370371372373374375376377#[test]378fn transfer_fungible_item() {379 new_test_ext().execute_with(|| {380 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));381382 let origin1 = RuntimeOrigin::signed(1);383 let origin2 = RuntimeOrigin::signed(2);384385 let data = default_fungible_data();386 create_test_item(collection_id, &data.into());387388 assert_eq!(389 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(1))),390 5391 );392393 394 assert_ok!(Unique::transfer(395 origin1,396 account(2),397 CollectionId(1),398 TokenId(0),399 5400 ));401 assert_eq!(402 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(1))),403 0404 );405406 407 assert_ok!(Unique::transfer(408 origin2.clone(),409 account(3),410 CollectionId(1),411 TokenId(0),412 3413 ));414415 416 assert_ok!(Unique::transfer(417 origin2,418 account(3),419 CollectionId(1),420 TokenId(0),421 1422 ));423 assert_eq!(424 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(2))),425 1426 );427 assert_eq!(428 <pallet_fungible::Balance<Test>>::get((CollectionId(1), account(3))),429 4430 );431 });432}433434#[test]435fn transfer_refungible_item() {436 new_test_ext().execute_with(|| {437 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));438439 440 let data = default_re_fungible_data();441 create_test_item(collection_id, &data.clone().into());442 assert_eq!(443 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),444 1445 );446 assert_eq!(447 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),448 1023449 );450 assert_eq!(451 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),452 true453 );454455 456 let origin1 = RuntimeOrigin::signed(1);457 let origin2 = RuntimeOrigin::signed(2);458 assert_ok!(Unique::transfer(459 origin1,460 account(2),461 CollectionId(1),462 TokenId(1),463 1023464 ));465 assert_eq!(466 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(2))),467 1023468 );469 assert_eq!(470 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),471 0472 );473 assert_eq!(474 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(2))),475 1476 );477 assert_eq!(478 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),479 false480 );481 assert_eq!(482 <pallet_refungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),483 true484 );485486 487 assert_ok!(Unique::transfer(488 origin2.clone(),489 account(3),490 CollectionId(1),491 TokenId(1),492 500493 ));494 assert_eq!(495 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(2))),496 523497 );498 assert_eq!(499 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(3))),500 500501 );502 assert_eq!(503 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(2))),504 1505 );506 assert_eq!(507 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(3))),508 1509 );510 assert_eq!(511 <pallet_refungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),512 true513 );514 assert_eq!(515 <pallet_refungible::Owned<Test>>::get((collection_id, account(3), TokenId(1))),516 true517 );518519 520 assert_ok!(Unique::transfer(521 origin2,522 account(3),523 CollectionId(1),524 TokenId(1),525 200526 ));527 assert_eq!(528 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(2))),529 323530 );531 assert_eq!(532 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(3))),533 700534 );535 assert_eq!(536 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(2))),537 1538 );539 assert_eq!(540 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(3))),541 1542 );543 assert_eq!(544 <pallet_refungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),545 true546 );547 assert_eq!(548 <pallet_refungible::Owned<Test>>::get((collection_id, account(3), TokenId(1))),549 true550 );551 });552}553554#[test]555fn transfer_nft_item() {556 new_test_ext().execute_with(|| {557 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));558559 let data = default_nft_data();560 create_test_item(collection_id, &data.into());561 assert_eq!(562 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),563 1564 );565 assert_eq!(566 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),567 true568 );569570 let origin1 = RuntimeOrigin::signed(1);571 572 assert_ok!(Unique::transfer(573 origin1,574 account(2),575 CollectionId(1),576 TokenId(1),577 1578 ));579 assert_eq!(580 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),581 0582 );583 assert_eq!(584 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(2))),585 1586 );587 assert_eq!(588 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),589 false590 );591 assert_eq!(592 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),593 true594 );595 });596}597598#[test]599fn transfer_nft_item_wrong_value() {600 new_test_ext().execute_with(|| {601 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));602603 let data = default_nft_data();604 create_test_item(collection_id, &data.into());605 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 );613614 let origin1 = RuntimeOrigin::signed(1);615616 assert_noop!(617 Unique::transfer(origin1, account(2), CollectionId(1), TokenId(1), 2)618 .map_err(|e| e.error),619 <pallet_nonfungible::Error::<Test>>::NonfungibleItemsHaveNoAmount620 );621 });622}623624#[test]625fn transfer_nft_item_zero_value() {626 new_test_ext().execute_with(|| {627 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));628629 let data = default_nft_data();630 create_test_item(collection_id, &data.into());631 assert_eq!(632 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),633 1634 );635 assert_eq!(636 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),637 true638 );639640 let origin1 = RuntimeOrigin::signed(1);641642 643 assert_ok!(Unique::transfer(644 origin1,645 account(2),646 CollectionId(1),647 TokenId(1),648 0649 ));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 );659 });660}661662#[test]663fn nft_approve_and_transfer_from() {664 new_test_ext().execute_with(|| {665 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));666667 let data = default_nft_data();668 create_test_item(collection_id, &data.into());669670 let origin1 = RuntimeOrigin::signed(1);671 let origin2 = RuntimeOrigin::signed(2);672673 assert_eq!(674 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),675 1676 );677 assert_eq!(678 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),679 true680 );681682 683 assert_noop!(684 Unique::transfer_from(685 origin2.clone(),686 account(1),687 account(2),688 CollectionId(1),689 TokenId(1),690 1691 )692 .map_err(|e| e.error),693 CommonError::<Test>::ApprovedValueTooLow694 );695696 697 assert_ok!(Unique::approve(698 origin1,699 account(2),700 CollectionId(1),701 TokenId(1),702 1703 ));704 assert_eq!(705 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),706 account(2)707 );708709 assert_ok!(Unique::transfer_from(710 origin2,711 account(1),712 account(3),713 CollectionId(1),714 TokenId(1),715 1716 ));717 assert!(718 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).is_none()719 );720 });721}722723#[test]724fn nft_approve_and_transfer_from_allow_list() {725 new_test_ext().execute_with(|| {726 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));727728 let origin1 = RuntimeOrigin::signed(1);729 let origin2 = RuntimeOrigin::signed(2);730731 732 let data = default_nft_data();733 create_test_item(collection_id, &data.clone().into());734 assert_eq!(735 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),736 1737 );738 assert_eq!(739 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),740 true741 );742743 744 assert_ok!(Unique::set_collection_permissions(745 origin1.clone(),746 CollectionId(1),747 CollectionPermissions {748 mint_mode: Some(true),749 access: Some(AccessMode::AllowList),750 nesting: None,751 }752 ));753 assert_ok!(Unique::add_to_allow_list(754 origin1.clone(),755 CollectionId(1),756 account(1)757 ));758 assert_ok!(Unique::add_to_allow_list(759 origin1.clone(),760 CollectionId(1),761 account(2)762 ));763 assert_ok!(Unique::add_to_allow_list(764 origin1.clone(),765 CollectionId(1),766 account(3)767 ));768769 770 assert_ok!(Unique::approve(771 origin1.clone(),772 account(2),773 CollectionId(1),774 TokenId(1),775 1776 ));777 assert_eq!(778 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),779 account(2)780 );781782 783 assert_ok!(Unique::transfer_from(784 origin2,785 account(1),786 account(3),787 CollectionId(1),788 TokenId(1),789 1790 ));791 assert!(792 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).is_none()793 );794 });795}796797#[test]798fn refungible_approve_and_transfer_from() {799 new_test_ext().execute_with(|| {800 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));801802 let origin1 = RuntimeOrigin::signed(1);803 let origin2 = RuntimeOrigin::signed(2);804805 806 let data = default_re_fungible_data();807 create_test_item(collection_id, &data.into());808809 assert_eq!(810 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),811 1812 );813 assert_eq!(814 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),815 1023816 );817 assert_eq!(818 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),819 true820 );821822 823 assert_ok!(Unique::set_collection_permissions(824 origin1.clone(),825 CollectionId(1),826 CollectionPermissions {827 mint_mode: Some(true),828 access: Some(AccessMode::AllowList),829 nesting: None,830 }831 ));832 assert_ok!(Unique::add_to_allow_list(833 origin1.clone(),834 CollectionId(1),835 account(1)836 ));837 assert_ok!(Unique::add_to_allow_list(838 origin1.clone(),839 CollectionId(1),840 account(2)841 ));842 assert_ok!(Unique::add_to_allow_list(843 origin1.clone(),844 CollectionId(1),845 account(3)846 ));847848 849 assert_ok!(Unique::approve(850 origin1,851 account(2),852 CollectionId(1),853 TokenId(1),854 1023855 ));856 assert_eq!(857 <pallet_refungible::Allowance<Test>>::get((858 CollectionId(1),859 TokenId(1),860 account(1),861 account(2)862 )),863 1023864 );865866 867 assert_ok!(Unique::transfer_from(868 origin2,869 account(1),870 account(3),871 CollectionId(1),872 TokenId(1),873 100874 ));875 assert_eq!(876 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),877 1878 );879 assert_eq!(880 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(3))),881 1882 );883 assert_eq!(884 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),885 923886 );887 assert_eq!(888 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(3))),889 100890 );891 assert_eq!(892 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),893 true894 );895 assert_eq!(896 <pallet_refungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),897 true898 );899 assert_eq!(900 <pallet_refungible::Allowance<Test>>::get((901 CollectionId(1),902 TokenId(1),903 account(1),904 account(2)905 )),906 923907 );908 });909}910911#[test]912fn fungible_approve_and_transfer_from() {913 new_test_ext().execute_with(|| {914 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));915916 let data = default_fungible_data();917 create_test_item(collection_id, &data.into());918919 let origin1 = RuntimeOrigin::signed(1);920 let origin2 = RuntimeOrigin::signed(2);921922 assert_ok!(Unique::set_collection_permissions(923 origin1.clone(),924 CollectionId(1),925 CollectionPermissions {926 mint_mode: Some(true),927 access: Some(AccessMode::AllowList),928 nesting: None,929 }930 ));931 assert_ok!(Unique::add_to_allow_list(932 origin1.clone(),933 CollectionId(1),934 account(1)935 ));936 assert_ok!(Unique::add_to_allow_list(937 origin1.clone(),938 CollectionId(1),939 account(2)940 ));941 assert_ok!(Unique::add_to_allow_list(942 origin1.clone(),943 CollectionId(1),944 account(3)945 ));946947 948 assert_ok!(Unique::approve(949 origin1.clone(),950 account(2),951 CollectionId(1),952 TokenId(0),953 5954 ));955 assert_eq!(956 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(2))),957 5958 );959 assert_ok!(Unique::approve(960 origin1,961 account(3),962 CollectionId(1),963 TokenId(0),964 5965 ));966 assert_eq!(967 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(2))),968 5969 );970 assert_eq!(971 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(3))),972 5973 );974975 assert_ok!(Unique::transfer_from(976 origin2.clone(),977 account(1),978 account(3),979 CollectionId(1),980 TokenId(0),981 4982 ));983984 assert_eq!(985 <pallet_fungible::Allowance<Test>>::get((CollectionId(1), account(1), account(2))),986 1987 );988989 assert_noop!(990 Unique::transfer_from(991 origin2,992 account(1),993 account(3),994 CollectionId(1),995 TokenId(0),996 4997 )998 .map_err(|e| e.error),999 CommonError::<Test>::ApprovedValueTooLow1000 );1001 });1002}10031004#[test]1005fn change_collection_owner() {1006 new_test_ext().execute_with(|| {1007 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));10081009 let origin1 = RuntimeOrigin::signed(1);1010 assert_ok!(Unique::change_collection_owner(origin1, collection_id, 2));1011 assert_eq!(1012 <pallet_common::CollectionById<Test>>::get(collection_id)1013 .unwrap()1014 .owner,1015 21016 );1017 });1018}10191020#[test]1021fn destroy_collection() {1022 new_test_ext().execute_with(|| {1023 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));10241025 let origin1 = RuntimeOrigin::signed(1);1026 assert_ok!(Unique::destroy_collection(origin1, collection_id));1027 });1028}10291030#[test]1031fn burn_nft_item() {1032 new_test_ext().execute_with(|| {1033 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));10341035 let origin1 = RuntimeOrigin::signed(1);10361037 let data = default_nft_data();1038 create_test_item(collection_id, &data.into());10391040 1041 assert_eq!(1042 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1043 11044 );10451046 1047 assert_ok!(Unique::burn_item(1048 origin1.clone(),1049 collection_id,1050 TokenId(1),1051 11052 ));1053 assert_eq!(1054 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1055 01056 );1057 });1058}10591060#[test]1061fn burn_same_nft_item_twice() {1062 new_test_ext().execute_with(|| {1063 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));10641065 let origin1 = RuntimeOrigin::signed(1);10661067 let data = default_nft_data();1068 create_test_item(collection_id, &data.into());10691070 1071 assert_eq!(1072 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1073 11074 );10751076 1077 assert_ok!(Unique::burn_item(1078 origin1.clone(),1079 collection_id,1080 TokenId(1),1081 11082 ));10831084 1085 assert_noop!(1086 Unique::burn_item(origin1, collection_id, TokenId(1), 1).map_err(|e| e.error),1087 CommonError::<Test>::TokenNotFound1088 );10891090 assert_eq!(1091 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),1092 01093 );1094 });1095}10961097#[test]1098fn burn_fungible_item() {1099 new_test_ext().execute_with(|| {1100 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));11011102 let origin1 = RuntimeOrigin::signed(1);1103 assert_ok!(Unique::add_collection_admin(1104 origin1.clone(),1105 collection_id,1106 account(2)1107 ));11081109 let data = default_fungible_data();1110 create_test_item(collection_id, &data.into());11111112 1113 assert_eq!(1114 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),1115 51116 );11171118 1119 assert_ok!(Unique::burn_item(1120 origin1.clone(),1121 CollectionId(1),1122 TokenId(0),1123 51124 ));1125 assert_noop!(1126 Unique::burn_item(origin1, CollectionId(1), TokenId(0), 5).map_err(|e| e.error),1127 CommonError::<Test>::TokenValueTooLow1128 );11291130 assert_eq!(1131 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),1132 01133 );1134 });1135}11361137#[test]1138fn burn_fungible_item_with_token_id() {1139 new_test_ext().execute_with(|| {1140 let collection_id = create_test_collection(&CollectionMode::Fungible(3), CollectionId(1));11411142 let origin1 = RuntimeOrigin::signed(1);1143 assert_ok!(Unique::add_collection_admin(1144 origin1.clone(),1145 collection_id,1146 account(2)1147 ));11481149 let data = default_fungible_data();1150 create_test_item(collection_id, &data.into());11511152 1153 assert_eq!(1154 <pallet_fungible::Balance<Test>>::get((collection_id, account(1))),1155 51156 );11571158 1159 assert_noop!(1160 Unique::burn_item(origin1, CollectionId(1), TokenId(1), 5).map_err(|e| e.error),1161 <pallet_fungible::Error::<Test>>::FungibleItemsHaveNoId1162 );1163 });1164}1165#[test]1166fn burn_refungible_item() {1167 new_test_ext().execute_with(|| {1168 let collection_id = create_test_collection(&CollectionMode::ReFungible, CollectionId(1));1169 let origin1 = RuntimeOrigin::signed(1);11701171 assert_ok!(Unique::set_collection_permissions(1172 origin1.clone(),1173 collection_id,1174 CollectionPermissions {1175 mint_mode: Some(true),1176 access: Some(AccessMode::AllowList),1177 nesting: None,1178 }1179 ));1180 assert_ok!(Unique::add_to_allow_list(1181 origin1.clone(),1182 collection_id,1183 account(1)1184 ));11851186 assert_ok!(Unique::add_collection_admin(1187 origin1.clone(),1188 collection_id,1189 account(2)1190 ));11911192 let data = default_re_fungible_data();1193 create_test_item(collection_id, &data.into());11941195 1196 assert_eq!(1197 <pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),1198 11199 );1200 assert_eq!(1201 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),1202 10231203 );12041205 1206 assert_ok!(Unique::burn_item(1207 origin1.clone(),1208 collection_id,1209 TokenId(1),1210 10231211 ));1212 assert_noop!(1213 Unique::burn_item(origin1, collection_id, TokenId(1), 1023).map_err(|e| e.error),1214 CommonError::<Test>::TokenValueTooLow1215 );12161217 assert_eq!(1218 <pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1))),1219 01220 );1221 });1222}12231224#[test]1225fn add_collection_admin() {1226 new_test_ext().execute_with(|| {1227 let collection1_id =1228 create_test_collection_for_owner(&CollectionMode::NFT, 1, CollectionId(1));1229 let origin1 = RuntimeOrigin::signed(1);12301231 1232 assert_ok!(Unique::add_collection_admin(1233 origin1.clone(),1234 collection1_id,1235 account(2)1236 ));1237 assert_ok!(Unique::add_collection_admin(1238 origin1,1239 collection1_id,1240 account(3)1241 ));12421243 1244 assert_eq!(1245 <pallet_common::IsAdmin<Test>>::get((CollectionId(1), account(1))),1246 false1247 );1248 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 )));1256 });1257}12581259#[test]1260fn remove_collection_admin() {1261 new_test_ext().execute_with(|| {1262 let collection1_id =1263 create_test_collection_for_owner(&CollectionMode::NFT, 1, CollectionId(1));1264 let origin1 = RuntimeOrigin::signed(1);12651266 1267 assert_ok!(Unique::add_collection_admin(1268 origin1.clone(),1269 collection1_id,1270 account(2)1271 ));1272 assert_ok!(Unique::add_collection_admin(1273 origin1.clone(),1274 collection1_id,1275 account(3)1276 ));12771278 assert!(<pallet_common::IsAdmin<Test>>::get((1279 CollectionId(1),1280 account(2)1281 )));1282 assert!(<pallet_common::IsAdmin<Test>>::get((1283 CollectionId(1),1284 account(3)1285 )));12861287 1288 assert_ok!(Unique::remove_collection_admin(1289 origin1,1290 CollectionId(1),1291 account(3)1292 ));12931294 1295 assert!(<pallet_common::IsAdmin<Test>>::get((1296 CollectionId(1),1297 account(2)1298 )));1299 assert_eq!(1300 <pallet_common::IsAdmin<Test>>::get((CollectionId(1), account(3))),1301 false1302 );1303 });1304}13051306#[test]1307fn balance_of() {1308 new_test_ext().execute_with(|| {1309 let nft_collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1310 let fungible_collection_id =1311 create_test_collection(&CollectionMode::Fungible(3), CollectionId(2));1312 let re_fungible_collection_id =1313 create_test_collection(&CollectionMode::ReFungible, CollectionId(3));13141315 1316 assert_eq!(1317 <pallet_nonfungible::AccountBalance<Test>>::get((nft_collection_id, account(1))),1318 01319 );1320 assert_eq!(1321 <pallet_fungible::Balance<Test>>::get((fungible_collection_id, account(1))),1322 01323 );1324 assert_eq!(1325 <pallet_refungible::AccountBalance<Test>>::get((re_fungible_collection_id, account(1))),1326 01327 );13281329 let nft_data = default_nft_data();1330 create_test_item(nft_collection_id, &nft_data.into());13311332 let fungible_data = default_fungible_data();1333 create_test_item(fungible_collection_id, &fungible_data.into());13341335 let re_fungible_data = default_re_fungible_data();1336 create_test_item(re_fungible_collection_id, &re_fungible_data.into());13371338 1339 assert_eq!(1340 <pallet_nonfungible::AccountBalance<Test>>::get((nft_collection_id, account(1))),1341 11342 );1343 assert_eq!(1344 <pallet_fungible::Balance<Test>>::get((fungible_collection_id, account(1))),1345 51346 );1347 assert_eq!(1348 <pallet_refungible::AccountBalance<Test>>::get((re_fungible_collection_id, account(1))),1349 11350 );13511352 assert_eq!(1353 <pallet_nonfungible::Owned<Test>>::get((nft_collection_id, account(1), TokenId(1))),1354 true1355 );1356 assert_eq!(1357 <pallet_refungible::Owned<Test>>::get((1358 re_fungible_collection_id,1359 account(1),1360 TokenId(1)1361 )),1362 true1363 );1364 });1365}13661367#[test]1368fn approve() {1369 new_test_ext().execute_with(|| {1370 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));13711372 let data = default_nft_data();1373 create_test_item(collection_id, &data.into());13741375 let origin1 = RuntimeOrigin::signed(1);13761377 1378 assert_ok!(Unique::approve(1379 origin1,1380 account(2),1381 CollectionId(1),1382 TokenId(1),1383 11384 ));1385 assert_eq!(1386 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1387 account(2)1388 );1389 });1390}13911392#[test]1393fn transfer_from() {1394 new_test_ext().execute_with(|| {1395 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1396 let origin1 = RuntimeOrigin::signed(1);1397 let origin2 = RuntimeOrigin::signed(2);13981399 let data = default_nft_data();1400 create_test_item(collection_id, &data.into());14011402 1403 assert_ok!(Unique::approve(1404 origin1.clone(),1405 account(2),1406 CollectionId(1),1407 TokenId(1),1408 11409 ));1410 assert_eq!(1411 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1412 account(2)1413 );14141415 assert_ok!(Unique::set_collection_permissions(1416 origin1.clone(),1417 CollectionId(1),1418 CollectionPermissions {1419 mint_mode: Some(true),1420 access: Some(AccessMode::AllowList),1421 nesting: None,1422 }1423 ));1424 assert_ok!(Unique::add_to_allow_list(1425 origin1.clone(),1426 CollectionId(1),1427 account(1)1428 ));1429 assert_ok!(Unique::add_to_allow_list(1430 origin1.clone(),1431 CollectionId(1),1432 account(2)1433 ));1434 assert_ok!(Unique::add_to_allow_list(1435 origin1,1436 CollectionId(1),1437 account(3)1438 ));14391440 assert_ok!(Unique::transfer_from(1441 origin2,1442 account(1),1443 account(2),1444 CollectionId(1),1445 TokenId(1),1446 11447 ));14481449 1450 assert_eq!(1451 <pallet_nonfungible::AccountBalance<Test>>::get((CollectionId(1), account(1))),1452 01453 );1454 assert_eq!(1455 <pallet_nonfungible::AccountBalance<Test>>::get((CollectionId(1), account(2))),1456 11457 );1458 });1459}1460146114621463146414651466#[test]1467fn owner_can_add_address_to_allow_list() {1468 new_test_ext().execute_with(|| {1469 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));14701471 let origin1 = RuntimeOrigin::signed(1);1472 assert_ok!(Unique::add_to_allow_list(1473 origin1,1474 collection_id,1475 account(2)1476 ));1477 assert!(<pallet_common::Allowlist<Test>>::get((1478 collection_id,1479 account(2)1480 )));1481 });1482}14831484#[test]1485fn admin_can_add_address_to_allow_list() {1486 new_test_ext().execute_with(|| {1487 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1488 let origin1 = RuntimeOrigin::signed(1);1489 let origin2 = RuntimeOrigin::signed(2);14901491 assert_ok!(Unique::add_collection_admin(1492 origin1,1493 collection_id,1494 account(2)1495 ));1496 assert_ok!(Unique::add_to_allow_list(1497 origin2,1498 collection_id,1499 account(3)1500 ));1501 assert!(<pallet_common::Allowlist<Test>>::get((1502 collection_id,1503 account(3)1504 )));1505 });1506}15071508#[test]1509fn nonprivileged_user_cannot_add_address_to_allow_list() {1510 new_test_ext().execute_with(|| {1511 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));15121513 let origin2 = RuntimeOrigin::signed(2);1514 assert_noop!(1515 Unique::add_to_allow_list(origin2, collection_id, account(3)),1516 CommonError::<Test>::NoPermission1517 );1518 });1519}15201521#[test]1522fn nobody_can_add_address_to_allow_list_of_nonexisting_collection() {1523 new_test_ext().execute_with(|| {1524 let origin1 = RuntimeOrigin::signed(1);15251526 assert_noop!(1527 Unique::add_to_allow_list(origin1, CollectionId(1), account(2)),1528 CommonError::<Test>::CollectionNotFound1529 );1530 });1531}15321533#[test]1534fn nobody_can_add_address_to_allow_list_of_deleted_collection() {1535 new_test_ext().execute_with(|| {1536 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));15371538 let origin1 = RuntimeOrigin::signed(1);1539 assert_ok!(Unique::destroy_collection(origin1.clone(), collection_id));1540 assert_noop!(1541 Unique::add_to_allow_list(origin1, collection_id, account(2)),1542 CommonError::<Test>::CollectionNotFound1543 );1544 });1545}154615471548#[test]1549fn address_is_already_added_to_allow_list() {1550 new_test_ext().execute_with(|| {1551 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1552 let origin1 = RuntimeOrigin::signed(1);15531554 assert_ok!(Unique::add_to_allow_list(1555 origin1.clone(),1556 collection_id,1557 account(2)1558 ));1559 assert_ok!(Unique::add_to_allow_list(1560 origin1,1561 collection_id,1562 account(2)1563 ));1564 assert!(<pallet_common::Allowlist<Test>>::get((1565 collection_id,1566 account(2)1567 )));1568 });1569}15701571#[test]1572fn owner_can_remove_address_from_allow_list() {1573 new_test_ext().execute_with(|| {1574 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));15751576 let origin1 = RuntimeOrigin::signed(1);1577 assert_ok!(Unique::add_to_allow_list(1578 origin1.clone(),1579 collection_id,1580 account(2)1581 ));1582 assert_ok!(Unique::remove_from_allow_list(1583 origin1,1584 collection_id,1585 account(2)1586 ));1587 assert_eq!(1588 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1589 false1590 );1591 });1592}15931594#[test]1595fn admin_can_remove_address_from_allow_list() {1596 new_test_ext().execute_with(|| {1597 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1598 let origin1 = RuntimeOrigin::signed(1);1599 let origin2 = RuntimeOrigin::signed(2);16001601 1602 assert_ok!(Unique::add_collection_admin(1603 origin1.clone(),1604 collection_id,1605 account(2)1606 ));16071608 1609 assert_ok!(Unique::add_to_allow_list(1610 origin1,1611 collection_id,1612 account(3)1613 ));16141615 1616 assert_ok!(Unique::remove_from_allow_list(1617 origin2,1618 collection_id,1619 account(3)1620 ));1621 assert_eq!(1622 <pallet_common::Allowlist<Test>>::get((collection_id, account(3))),1623 false1624 );1625 });1626}16271628#[test]1629fn nonprivileged_user_cannot_remove_address_from_allow_list() {1630 new_test_ext().execute_with(|| {1631 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1632 let origin1 = RuntimeOrigin::signed(1);1633 let origin2 = RuntimeOrigin::signed(2);16341635 assert_ok!(Unique::add_to_allow_list(1636 origin1,1637 collection_id,1638 account(2)1639 ));1640 assert_noop!(1641 Unique::remove_from_allow_list(origin2, collection_id, account(2)),1642 CommonError::<Test>::NoPermission1643 );1644 assert!(<pallet_common::Allowlist<Test>>::get((1645 collection_id,1646 account(2)1647 )));1648 });1649}16501651#[test]1652fn nobody_can_remove_address_from_allow_list_of_nonexisting_collection() {1653 new_test_ext().execute_with(|| {1654 let origin1 = RuntimeOrigin::signed(1);16551656 assert_noop!(1657 Unique::remove_from_allow_list(origin1, CollectionId(1), account(2)),1658 CommonError::<Test>::CollectionNotFound1659 );1660 });1661}16621663#[test]1664fn nobody_can_remove_address_from_allow_list_of_deleted_collection() {1665 new_test_ext().execute_with(|| {1666 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1667 let origin1 = RuntimeOrigin::signed(1);1668 let origin2 = RuntimeOrigin::signed(2);16691670 1671 assert_ok!(Unique::add_to_allow_list(1672 origin1.clone(),1673 collection_id,1674 account(2)1675 ));16761677 1678 assert!(<pallet_common::Allowlist<Test>>::get((1679 collection_id,1680 account(2)1681 )));16821683 1684 assert_ok!(Unique::destroy_collection(origin1, collection_id));16851686 1687 assert_noop!(1688 Unique::remove_from_allow_list(origin2, collection_id, account(2)),1689 CommonError::<Test>::CollectionNotFound1690 );16911692 1693 assert_eq!(1694 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1695 false1696 );1697 });1698}169917001701#[test]1702fn address_is_already_removed_from_allow_list() {1703 new_test_ext().execute_with(|| {1704 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1705 let origin1 = RuntimeOrigin::signed(1);17061707 assert_ok!(Unique::add_to_allow_list(1708 origin1.clone(),1709 collection_id,1710 account(2)1711 ));1712 assert_ok!(Unique::remove_from_allow_list(1713 origin1.clone(),1714 collection_id,1715 account(2)1716 ));1717 assert_eq!(1718 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1719 false1720 );1721 assert_ok!(Unique::remove_from_allow_list(1722 origin1,1723 collection_id,1724 account(2)1725 ));1726 assert_eq!(1727 <pallet_common::Allowlist<Test>>::get((collection_id, account(2))),1728 false1729 );1730 });1731}173217331734#[test]1735fn allow_list_test_1() {1736 new_test_ext().execute_with(|| {1737 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));17381739 let origin1 = RuntimeOrigin::signed(1);1740 assert_ok!(Unique::add_collection_admin(1741 origin1.clone(),1742 collection_id,1743 account(1)1744 ));17451746 let data = default_nft_data();1747 create_test_item(collection_id, &data.into());17481749 assert_ok!(Unique::set_collection_permissions(1750 origin1.clone(),1751 collection_id,1752 CollectionPermissions {1753 mint_mode: None,1754 access: Some(AccessMode::AllowList),1755 nesting: None,1756 }1757 ));1758 assert_ok!(Unique::add_to_allow_list(1759 origin1.clone(),1760 collection_id,1761 account(2)1762 ));17631764 assert_noop!(1765 Unique::transfer(origin1, account(3), CollectionId(1), TokenId(1), 1)1766 .map_err(|e| e.error),1767 CommonError::<Test>::AddressNotInAllowlist1768 );1769 });1770}17711772#[test]1773fn allow_list_test_2() {1774 new_test_ext().execute_with(|| {1775 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));1776 let origin1 = RuntimeOrigin::signed(1);17771778 let data = default_nft_data();1779 create_test_item(collection_id, &data.into());17801781 assert_ok!(Unique::set_collection_permissions(1782 origin1.clone(),1783 collection_id,1784 CollectionPermissions {1785 mint_mode: None,1786 access: Some(AccessMode::AllowList),1787 nesting: None,1788 }1789 ));1790 assert_ok!(Unique::add_to_allow_list(1791 origin1.clone(),1792 collection_id,1793 account(1)1794 ));1795 assert_ok!(Unique::add_to_allow_list(1796 origin1.clone(),1797 collection_id,1798 account(2)1799 ));18001801 1802 assert_ok!(Unique::approve(1803 origin1.clone(),1804 account(1),1805 collection_id,1806 TokenId(1),1807 11808 ));1809 assert_eq!(1810 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1811 account(1)1812 );18131814 assert_ok!(Unique::remove_from_allow_list(1815 origin1.clone(),1816 collection_id,1817 account(1)1818 ));18191820 assert_noop!(1821 Unique::transfer_from(1822 origin1,1823 account(1),1824 account(3),1825 CollectionId(1),1826 TokenId(1),1827 11828 )1829 .map_err(|e| e.error),1830 CommonError::<Test>::AddressNotInAllowlist1831 );1832 });1833}183418351836#[test]1837fn allow_list_test_3() {1838 new_test_ext().execute_with(|| {1839 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));18401841 let origin1 = RuntimeOrigin::signed(1);18421843 let data = default_nft_data();1844 create_test_item(collection_id, &data.into());18451846 assert_ok!(Unique::set_collection_permissions(1847 origin1.clone(),1848 collection_id,1849 CollectionPermissions {1850 mint_mode: None,1851 access: Some(AccessMode::AllowList),1852 nesting: None,1853 }1854 ));1855 assert_ok!(Unique::add_to_allow_list(1856 origin1.clone(),1857 collection_id,1858 account(1)1859 ));18601861 assert_noop!(1862 Unique::transfer(origin1, account(3), collection_id, TokenId(1), 1)1863 .map_err(|e| e.error),1864 CommonError::<Test>::AddressNotInAllowlist1865 );1866 });1867}18681869#[test]1870fn allow_list_test_4() {1871 new_test_ext().execute_with(|| {1872 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));18731874 let origin1 = RuntimeOrigin::signed(1);18751876 let data = default_nft_data();1877 create_test_item(collection_id, &data.into());18781879 assert_ok!(Unique::set_collection_permissions(1880 origin1.clone(),1881 collection_id,1882 CollectionPermissions {1883 mint_mode: None,1884 access: Some(AccessMode::AllowList),1885 nesting: None,1886 }1887 ));1888 assert_ok!(Unique::add_to_allow_list(1889 origin1.clone(),1890 collection_id,1891 account(1)1892 ));1893 assert_ok!(Unique::add_to_allow_list(1894 origin1.clone(),1895 collection_id,1896 account(2)1897 ));18981899 1900 assert_ok!(Unique::approve(1901 origin1.clone(),1902 account(1),1903 collection_id,1904 TokenId(1),1905 11906 ));1907 assert_eq!(1908 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),1909 account(1)1910 );19111912 assert_ok!(Unique::remove_from_allow_list(1913 origin1.clone(),1914 collection_id,1915 account(2)1916 ));19171918 assert_noop!(1919 Unique::transfer_from(1920 origin1,1921 account(1),1922 account(3),1923 collection_id,1924 TokenId(1),1925 11926 )1927 .map_err(|e| e.error),1928 CommonError::<Test>::AddressNotInAllowlist1929 );1930 });1931}193219331934#[test]1935fn allow_list_test_5() {1936 new_test_ext().execute_with(|| {1937 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));19381939 let origin1 = RuntimeOrigin::signed(1);19401941 let data = default_nft_data();1942 create_test_item(collection_id, &data.into());19431944 assert_ok!(Unique::set_collection_permissions(1945 origin1.clone(),1946 collection_id,1947 CollectionPermissions {1948 mint_mode: None,1949 access: Some(AccessMode::AllowList),1950 nesting: None,1951 }1952 ));1953 assert_noop!(1954 Unique::burn_item(origin1.clone(), CollectionId(1), TokenId(1), 1).map_err(|e| e.error),1955 CommonError::<Test>::AddressNotInAllowlist1956 );1957 });1958}195919601961#[test]1962fn allow_list_test_6() {1963 new_test_ext().execute_with(|| {1964 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));19651966 let origin1 = RuntimeOrigin::signed(1);19671968 let data = default_nft_data();1969 create_test_item(collection_id, &data.into());19701971 assert_ok!(Unique::set_collection_permissions(1972 origin1.clone(),1973 collection_id,1974 CollectionPermissions {1975 mint_mode: None,1976 access: Some(AccessMode::AllowList),1977 nesting: None,1978 }1979 ));19801981 1982 assert_noop!(1983 Unique::approve(origin1, account(1), CollectionId(1), TokenId(1), 1)1984 .map_err(|e| e.error),1985 CommonError::<Test>::AddressNotInAllowlist1986 );1987 });1988}1989199019911992#[test]1993fn allow_list_test_7() {1994 new_test_ext().execute_with(|| {1995 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));19961997 let data = default_nft_data();1998 create_test_item(collection_id, &data.into());19992000 let origin1 = RuntimeOrigin::signed(1);20012002 assert_ok!(Unique::set_collection_permissions(2003 origin1.clone(),2004 collection_id,2005 CollectionPermissions {2006 mint_mode: None,2007 access: Some(AccessMode::AllowList),2008 nesting: None,2009 }2010 ));2011 assert_ok!(Unique::add_to_allow_list(2012 origin1.clone(),2013 collection_id,2014 account(1)2015 ));2016 assert_ok!(Unique::add_to_allow_list(2017 origin1.clone(),2018 collection_id,2019 account(2)2020 ));20212022 assert_ok!(Unique::transfer(2023 origin1,2024 account(2),2025 CollectionId(1),2026 TokenId(1),2027 12028 ));2029 });2030}20312032#[test]2033fn allow_list_test_8() {2034 new_test_ext().execute_with(|| {2035 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));20362037 2038 let data = default_nft_data();2039 create_test_item(collection_id, &data.into());20402041 let origin1 = RuntimeOrigin::signed(1);20422043 2044 assert_ok!(Unique::set_collection_permissions(2045 origin1.clone(),2046 collection_id,2047 CollectionPermissions {2048 mint_mode: None,2049 access: Some(AccessMode::AllowList),2050 nesting: None,2051 }2052 ));2053 assert_ok!(Unique::add_to_allow_list(2054 origin1.clone(),2055 collection_id,2056 account(1)2057 ));2058 assert_ok!(Unique::add_to_allow_list(2059 origin1.clone(),2060 collection_id,2061 account(2)2062 ));20632064 2065 assert_ok!(Unique::approve(2066 origin1.clone(),2067 account(1),2068 CollectionId(1),2069 TokenId(1),2070 12071 ));2072 assert_eq!(2073 <pallet_nonfungible::Allowance<Test>>::get((CollectionId(1), TokenId(1))).unwrap(),2074 account(1)2075 );20762077 2078 assert_ok!(Unique::transfer_from(2079 origin1,2080 account(1),2081 account(2),2082 CollectionId(1),2083 TokenId(1),2084 12085 ));2086 });2087}208820892090#[test]2091fn allow_list_test_9() {2092 new_test_ext().execute_with(|| {2093 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));2094 let origin1 = RuntimeOrigin::signed(1);20952096 assert_ok!(Unique::set_collection_permissions(2097 origin1.clone(),2098 collection_id,2099 CollectionPermissions {2100 mint_mode: Some(false),2101 access: Some(AccessMode::AllowList),2102 nesting: None,2103 }2104 ));21052106 let data = default_nft_data();2107 create_test_item(collection_id, &data.into());2108 });2109}211021112112#[test]2113fn allow_list_test_10() {2114 new_test_ext().execute_with(|| {2115 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));21162117 let origin1 = RuntimeOrigin::signed(1);2118 let origin2 = RuntimeOrigin::signed(2);21192120 assert_ok!(Unique::set_collection_permissions(2121 origin1.clone(),2122 collection_id,2123 CollectionPermissions {2124 mint_mode: Some(false),2125 access: Some(AccessMode::AllowList),2126 nesting: None,2127 }2128 ));21292130 assert_ok!(Unique::add_collection_admin(2131 origin1,2132 collection_id,2133 account(2)2134 ));21352136 assert_ok!(Unique::create_item(2137 origin2,2138 collection_id,2139 account(2),2140 default_nft_data().into()2141 ));2142 });2143}214421452146#[test]2147fn allow_list_test_11() {2148 new_test_ext().execute_with(|| {2149 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));21502151 let origin1 = RuntimeOrigin::signed(1);2152 let origin2 = RuntimeOrigin::signed(2);21532154 assert_ok!(Unique::set_collection_permissions(2155 origin1.clone(),2156 collection_id,2157 CollectionPermissions {2158 mint_mode: Some(false),2159 access: Some(AccessMode::AllowList),2160 nesting: None,2161 }2162 ));2163 assert_ok!(Unique::add_to_allow_list(2164 origin1,2165 collection_id,2166 account(2)2167 ));21682169 assert_noop!(2170 Unique::create_item(2171 origin2,2172 CollectionId(1),2173 account(2),2174 default_nft_data().into()2175 )2176 .map_err(|e| e.error),2177 CommonError::<Test>::PublicMintingNotAllowed2178 );2179 });2180}218121822183#[test]2184fn allow_list_test_12() {2185 new_test_ext().execute_with(|| {2186 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));21872188 let origin1 = RuntimeOrigin::signed(1);2189 let origin2 = RuntimeOrigin::signed(2);21902191 assert_ok!(Unique::set_collection_permissions(2192 origin1.clone(),2193 collection_id,2194 CollectionPermissions {2195 mint_mode: Some(false),2196 access: Some(AccessMode::AllowList),2197 nesting: None,2198 }2199 ));22002201 assert_noop!(2202 Unique::create_item(2203 origin2,2204 CollectionId(1),2205 account(2),2206 default_nft_data().into()2207 )2208 .map_err(|e| e.error),2209 CommonError::<Test>::PublicMintingNotAllowed2210 );2211 });2212}221322142215#[test]2216fn allow_list_test_13() {2217 new_test_ext().execute_with(|| {2218 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));22192220 let origin1 = RuntimeOrigin::signed(1);22212222 assert_ok!(Unique::set_collection_permissions(2223 origin1.clone(),2224 collection_id,2225 CollectionPermissions {2226 mint_mode: Some(true),2227 access: Some(AccessMode::AllowList),2228 nesting: None,2229 }2230 ));22312232 let data = default_nft_data();2233 create_test_item(collection_id, &data.into());2234 });2235}223622372238#[test]2239fn allow_list_test_14() {2240 new_test_ext().execute_with(|| {2241 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));22422243 let origin1 = RuntimeOrigin::signed(1);2244 let origin2 = RuntimeOrigin::signed(2);22452246 assert_ok!(Unique::set_collection_permissions(2247 origin1.clone(),2248 collection_id,2249 CollectionPermissions {2250 mint_mode: Some(true),2251 access: Some(AccessMode::AllowList),2252 nesting: None,2253 }2254 ));22552256 assert_ok!(Unique::add_collection_admin(2257 origin1,2258 collection_id,2259 account(2)2260 ));22612262 assert_ok!(Unique::create_item(2263 origin2,2264 collection_id,2265 account(2),2266 default_nft_data().into()2267 ));2268 });2269}227022712272#[test]2273fn allow_list_test_15() {2274 new_test_ext().execute_with(|| {2275 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));22762277 let origin1 = RuntimeOrigin::signed(1);2278 let origin2 = RuntimeOrigin::signed(2);22792280 assert_ok!(Unique::set_collection_permissions(2281 origin1.clone(),2282 collection_id,2283 CollectionPermissions {2284 mint_mode: Some(true),2285 access: Some(AccessMode::AllowList),2286 nesting: None,2287 }2288 ));22892290 assert_noop!(2291 Unique::create_item(2292 origin2,2293 collection_id,2294 account(2),2295 default_nft_data().into()2296 )2297 .map_err(|e| e.error),2298 CommonError::<Test>::AddressNotInAllowlist2299 );2300 });2301}230223032304#[test]2305fn allow_list_test_16() {2306 new_test_ext().execute_with(|| {2307 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23082309 let origin1 = RuntimeOrigin::signed(1);2310 let origin2 = RuntimeOrigin::signed(2);23112312 assert_ok!(Unique::set_collection_permissions(2313 origin1.clone(),2314 collection_id,2315 CollectionPermissions {2316 mint_mode: Some(true),2317 access: Some(AccessMode::AllowList),2318 nesting: None,2319 }2320 ));2321 assert_ok!(Unique::add_to_allow_list(2322 origin1,2323 collection_id,2324 account(2)2325 ));23262327 assert_ok!(Unique::create_item(2328 origin2,2329 collection_id,2330 account(2),2331 default_nft_data().into()2332 ));2333 });2334}233523362337#[test]2338fn total_number_collections_bound() {2339 new_test_ext().execute_with(|| {2340 create_test_collection(&CollectionMode::NFT, CollectionId(1));2341 });2342}23432344#[test]2345fn create_max_collections() {2346 new_test_ext().execute_with(|| {2347 for i in 1..COLLECTION_NUMBER_LIMIT {2348 create_test_collection(&CollectionMode::NFT, CollectionId(i));2349 }2350 });2351}235223532354#[test]2355fn total_number_collections_bound_neg() {2356 new_test_ext().execute_with(|| {2357 let origin1 = RuntimeOrigin::signed(1);23582359 for i in 1..=COLLECTION_NUMBER_LIMIT {2360 create_test_collection(&CollectionMode::NFT, CollectionId(i));2361 }23622363 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();2364 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();2365 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();23662367 let data = CreateCollectionData {2368 name: col_name1.try_into().unwrap(),2369 description: col_desc1.try_into().unwrap(),2370 token_prefix: token_prefix1.try_into().unwrap(),2371 mode: CollectionMode::NFT,2372 ..Default::default()2373 };23742375 2376 assert_noop!(2377 Unique::create_collection_ex(origin1, data),2378 CommonError::<Test>::TotalCollectionsLimitExceeded2379 );2380 });2381}238223832384#[test]2385fn owned_tokens_bound() {2386 new_test_ext().execute_with(|| {2387 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));23882389 let data = default_nft_data();2390 create_test_item(collection_id, &data.clone().into());2391 create_test_item(collection_id, &data.into());2392 });2393}239423952396#[test]2397fn owned_tokens_bound_neg() {2398 new_test_ext().execute_with(|| {2399 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));24002401 let origin1 = RuntimeOrigin::signed(1);24022403 for _ in 1..=MAX_TOKEN_OWNERSHIP {2404 let data = default_nft_data();2405 create_test_item(collection_id, &data.clone().into());2406 }24072408 let data = default_nft_data();2409 assert_noop!(2410 Unique::create_item(origin1, CollectionId(1), account(1), data.into())2411 .map_err(|e| e.error),2412 CommonError::<Test>::AccountTokenLimitExceeded2413 );2414 });2415}241624172418#[test]2419fn collection_admins_bound() {2420 new_test_ext().execute_with(|| {2421 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));24222423 let origin1 = RuntimeOrigin::signed(1);24242425 assert_ok!(Unique::add_collection_admin(2426 origin1.clone(),2427 collection_id,2428 account(2)2429 ));2430 assert_ok!(Unique::add_collection_admin(2431 origin1,2432 collection_id,2433 account(3)2434 ));2435 });2436}243724382439#[test]2440fn collection_admins_bound_neg() {2441 new_test_ext().execute_with(|| {2442 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));24432444 let origin1 = RuntimeOrigin::signed(1);24452446 for i in 0..COLLECTION_ADMINS_LIMIT {2447 assert_ok!(Unique::add_collection_admin(2448 origin1.clone(),2449 collection_id,2450 account((2 + i).into())2451 ));2452 }2453 assert_noop!(2454 Unique::add_collection_admin(2455 origin1,2456 collection_id,2457 account((3 + COLLECTION_ADMINS_LIMIT).into())2458 ),2459 CommonError::<Test>::CollectionAdminCountExceeded2460 );2461 });2462}246324642465#[test]2466fn collection_transfer_flag_works() {2467 new_test_ext().execute_with(|| {2468 let origin1 = RuntimeOrigin::signed(1);24692470 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));2471 assert_ok!(Unique::set_transfers_enabled_flag(2472 origin1,2473 collection_id,2474 true2475 ));24762477 let data = default_nft_data();2478 create_test_item(collection_id, &data.into());2479 assert_eq!(2480 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2481 12482 );2483 assert_eq!(2484 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2485 true2486 );24872488 let origin1 = RuntimeOrigin::signed(1);24892490 2491 assert_ok!(Unique::transfer(2492 origin1,2493 account(2),2494 collection_id,2495 TokenId(1),2496 12497 ));2498 assert_eq!(2499 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2500 false2501 );2502 assert_eq!(2503 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),2504 true2505 );2506 assert_eq!(2507 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2508 02509 );2510 assert_eq!(2511 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(2))),2512 12513 );2514 });2515}25162517#[test]2518fn collection_transfer_flag_works_neg() {2519 new_test_ext().execute_with(|| {2520 let origin1 = RuntimeOrigin::signed(1);25212522 let collection_id = create_test_collection(&CollectionMode::NFT, CollectionId(1));2523 assert_ok!(Unique::set_transfers_enabled_flag(2524 origin1,2525 collection_id,2526 false2527 ));25282529 let data = default_nft_data();2530 create_test_item(collection_id, &data.into());2531 assert_eq!(2532 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2533 12534 );2535 assert_eq!(2536 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2537 true2538 );25392540 let origin1 = RuntimeOrigin::signed(1);25412542 2543 assert_noop!(2544 Unique::transfer(origin1, account(2), CollectionId(1), TokenId(1), 1)2545 .map_err(|e| e.error),2546 CommonError::<Test>::TransferNotAllowed2547 );2548 assert_eq!(2549 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(1))),2550 12551 );2552 assert_eq!(2553 <pallet_nonfungible::AccountBalance<Test>>::get((collection_id, account(2))),2554 02555 );2556 assert_eq!(2557 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(1), TokenId(1))),2558 true2559 );2560 assert_eq!(2561 <pallet_nonfungible::Owned<Test>>::get((collection_id, account(2), TokenId(1))),2562 false2563 );2564 });2565}25662567#[test]2568fn collection_sponsoring() {2569 new_test_ext().execute_with(|| {2570 2571 let user1 = 1_u64;2572 let user2 = 777_u64;2573 let origin1 = RuntimeOrigin::signed(user1);2574 let origin2 = RuntimeOrigin::signed(user2);2575 let account2 = account(user2);25762577 let collection_id =2578 create_test_collection_for_owner(&CollectionMode::NFT, user1, CollectionId(1));2579 assert_ok!(Unique::set_collection_sponsor(2580 origin1.clone(),2581 collection_id,2582 user12583 ));2584 assert_ok!(Unique::confirm_sponsorship(origin1.clone(), collection_id));25852586 2587 assert!(Unique::create_item(2588 origin2.clone(),2589 collection_id,2590 account2.clone(),2591 default_nft_data().into()2592 )2593 .is_err());25942595 assert_ok!(Unique::set_collection_permissions(2596 origin1.clone(),2597 collection_id,2598 CollectionPermissions {2599 mint_mode: Some(true),2600 access: Some(AccessMode::AllowList),2601 nesting: None,2602 }2603 ));2604 assert_ok!(Unique::add_to_allow_list(2605 origin1.clone(),2606 collection_id,2607 account2.clone()2608 ));26092610 assert_ok!(Unique::create_item(2611 origin2,2612 collection_id,2613 account2,2614 default_nft_data().into()2615 ));2616 });2617}26182619mod check_token_permissions {2620 use super::*;2621 use pallet_common::LazyValue;26222623 fn test<FTE: FnOnce() -> bool>(2624 i: usize,2625 test_case: &pallet_common::tests::TestCase,2626 check_token_existence: &mut LazyValue<bool, FTE>,2627 ) {2628 let collection_admin = test_case.collection_admin;2629 let mut is_collection_admin = LazyValue::new(|| test_case.is_collection_admin);2630 let token_owner = test_case.token_owner;2631 let mut is_token_owner = LazyValue::new(|| Ok(test_case.is_token_owner));2632 let is_no_permission = test_case.no_permission;26332634 let result = pallet_common::tests::check_token_permissions::<Test, _, _, FTE>(2635 collection_admin,2636 token_owner,2637 &mut is_collection_admin,2638 &mut is_token_owner,2639 check_token_existence,2640 );26412642 if is_no_permission {2643 assert!(2644 result.is_err(),2645 "{i}: {test_case:?}, token_exist: {}",2646 check_token_existence.value()2647 );2648 assert_err!(result, pallet_common::Error::<Test>::NoPermission,);2649 } else if check_token_existence.has_value() && !check_token_existence.value() {2650 assert!(2651 result.is_err(),2652 "{i}: {test_case:?}, token_exist: {}",2653 check_token_existence.value()2654 );2655 assert_err!(result, pallet_common::Error::<Test>::TokenNotFound,);2656 }2657 }26582659 #[test]2660 fn no_permission_only() {2661 new_test_ext().execute_with(|| {2662 let mut check_token_existence = LazyValue::new(|| true);2663 for (i, row) in pallet_common::tests::TABLE.iter().enumerate() {2664 test(i, row, &mut check_token_existence);2665 }2666 });2667 }26682669 #[test]2670 fn no_permission_and_token_not_found() {2671 new_test_ext().execute_with(|| {2672 for (i, row) in pallet_common::tests::TABLE.iter().enumerate() {2673 2674 let mut check_token_existence = LazyValue::new(|| false);2675 test(i, row, &mut check_token_existence);2676 }2677 });2678 }2679}