--- a/pallets/nft/src/lib.rs +++ b/pallets/nft/src/lib.rs @@ -264,52 +264,64 @@ decl_error! { /// Error for non-fungible-token module. pub enum Error for Module { - /// Total collections bound exceeded + /// Total collections bound exceeded. TotalCollectionsLimitExceeded, - /// Decimal_points parameter must be lower than 4 + /// Decimal_points parameter must be lower than 4. CollectionDecimalPointLimitExceeded, - /// Collection name can not be longer than 63 char + /// Collection name can not be longer than 63 char. CollectionNameLimitExceeded, - /// Collection description can not be longer than 255 char + /// Collection description can not be longer than 255 char. CollectionDescriptionLimitExceeded, - /// Token prefix can not be longer than 15 char + /// Token prefix can not be longer than 15 char. CollectionTokenPrefixLimitExceeded, - /// This collection does not exist + /// This collection does not exist. CollectionNotFound, - /// Item not exists + /// Item not exists. TokenNotFound, - /// Arithmetic calculation overflow + /// Arithmetic calculation overflow. NumOverflow, - /// Account already has admin role + /// Account already has admin role. AlreadyAdmin, - /// You do not own this collection + /// You do not own this collection. NoPermission, - /// This address is not set as sponsor, use setCollectionSponsor first + /// This address is not set as sponsor, use setCollectionSponsor first. ConfirmUnsetSponsorFail, - /// Collection is not in mint mode + /// Collection is not in mint mode. PublicMintingNotAllowed, - /// Sender parameter and item owner must be equal + /// Sender parameter and item owner must be equal. MustBeTokenOwner, - /// Item balance not enouth + /// Item balance not enough. TokenValueTooLow, - /// Size of item is too large + /// Size of item is too large. NftSizeLimitExceeded, - /// Size of item must be 0 with fungible type - FungibleUnexpectedParam, /// No approve found ApproveNotFound, - /// Requested value more than approved + /// Requested value more than approved. TokenValueNotEnough, - /// Only approved addresses can call this method + /// Only approved addresses can call this method. ApproveRequired, - /// Address is not in white list + /// Address is not in white list. AddresNotInWhiteList, - /// Number of collection admins bound exceeded + /// Number of collection admins bound exceeded. CollectionAdminsLimitExceeded, - /// Owned tokens by a single address bound exceeded + /// Owned tokens by a single address bound exceeded. AddressOwnershipLimitExceeded, - /// Length of items properties must be greater than 0 + /// Length of items properties must be greater than 0. EmptyArgument, + /// const_data exceeded data limit. + TokenConstDataLimitExceeded, + /// variable_data exceeded data limit. + TokenVariableDataLimitExceeded, + /// Not NFT item data used to mint in NFT collection. + NotNftDataUsedToMintNftCollectionToken, + /// Not Fungible item data used to mint in Fungible collection. + NotFungibleDataUsedToMintFungibleCollectionToken, + /// Not Re Fungible item data used to mint in Re Fungible collection. + NotReFungibleDataUsedToMintReFungibleCollectionToken, + /// Unexpected collection type. + UnexpectedCollectionType, + /// Can't store metadata in fungible tokens. + CantStoreMetadataInFungibleTokens } } @@ -1181,6 +1193,8 @@ let sender = ensure_signed(origin)?; Self::collection_exists(collection_id)?; + + ensure!(ChainLimit::get().custom_data_limit >= data.len() as u32, Error::::TokenVariableDataLimitExceeded); // Modify permissions check let target_collection = >::get(collection_id); @@ -1194,7 +1208,8 @@ { CollectionMode::NFT => Self::set_nft_variable_data(collection_id, item_id, data)?, CollectionMode::ReFungible(_) => Self::set_re_fungible_variable_data(collection_id, item_id, data)?, - _ => () + CollectionMode::Fungible(_) => fail!(Error::::CantStoreMetadataInFungibleTokens), + _ => fail!(Error::::UnexpectedCollectionType) }; Ok(()) @@ -1382,29 +1397,29 @@ CollectionMode::NFT => { if let CreateItemData::NFT(data) = data { // check sizes - ensure!(ChainLimit::get().custom_data_limit >= data.const_data.len() as u32, "const_data exceeded data limit."); - ensure!(ChainLimit::get().custom_data_limit >= data.variable_data.len() as u32, "variable_data exceeded data limit."); + ensure!(ChainLimit::get().custom_data_limit >= data.const_data.len() as u32, Error::::TokenConstDataLimitExceeded); + ensure!(ChainLimit::get().custom_data_limit >= data.variable_data.len() as u32, Error::::TokenVariableDataLimitExceeded); } else { - fail!("Not NFT item data used to mint in NFT collection."); + fail!(Error::::NotNftDataUsedToMintNftCollectionToken); } }, CollectionMode::Fungible(_) => { if let CreateItemData::Fungible(_) = data { } else { - fail!("Not Fungible item data used to mint in Fungible collection."); + fail!(Error::::NotFungibleDataUsedToMintFungibleCollectionToken); } }, CollectionMode::ReFungible(_) => { if let CreateItemData::ReFungible(data) = data { // check sizes - ensure!(ChainLimit::get().custom_data_limit >= data.const_data.len() as u32, "const_data exceeded data limit."); - ensure!(ChainLimit::get().custom_data_limit >= data.variable_data.len() as u32, "variable_data exceeded data limit."); + ensure!(ChainLimit::get().custom_data_limit >= data.const_data.len() as u32, Error::::TokenConstDataLimitExceeded); + ensure!(ChainLimit::get().custom_data_limit >= data.variable_data.len() as u32, Error::::TokenVariableDataLimitExceeded); } else { - fail!("Not Re Fungible item data used to mint in Re Fungible collection."); + fail!(Error::::NotReFungibleDataUsedToMintReFungibleCollectionToken); } }, - _ => { fail!("Unexpected collection type."); } + _ => { fail!(Error::::UnexpectedCollectionType); } }; Ok(()) --- a/pallets/nft/src/tests.rs +++ b/pallets/nft/src/tests.rs @@ -1725,7 +1725,7 @@ collection_id, 1, too_big_const_data - ), "const_data exceeded data limit."); + ), Error::::TokenConstDataLimitExceeded); }); } @@ -1756,7 +1756,7 @@ collection_id, 1, too_big_const_data - ), "variable_data exceeded data limit."); + ), Error::::TokenVariableDataLimitExceeded); }); } @@ -1787,7 +1787,7 @@ collection_id, 1, too_big_const_data - ), "const_data exceeded data limit."); + ), Error::::TokenConstDataLimitExceeded); }); } @@ -1818,7 +1818,7 @@ collection_id, 1, too_big_const_data - ), "variable_data exceeded data limit."); + ), Error::::TokenVariableDataLimitExceeded); }); } // #endregion @@ -1851,4 +1851,111 @@ assert_eq!(TemplateModule::collection(collection_id).const_on_chain_schema, b"".to_vec()); assert_eq!(TemplateModule::collection(collection_id).variable_on_chain_schema, b"test variable on chain schema".to_vec()); }); -} \ No newline at end of file +} + +#[test] +fn set_variable_meta_data_on_nft_token_stores_variable_meta_data() { + new_test_ext().execute_with(|| { + default_limits(); + + let collection_id = create_test_collection(&CollectionMode::NFT, 1); + + let origin1 = Origin::signed(1); + + let data = default_nft_data(); + create_test_item(1, &data.into()); + + let variable_data = b"test set_variable_meta_data method.".to_vec(); + assert_ok!(TemplateModule::set_variable_meta_data(origin1, collection_id, 1, variable_data.clone())); + + assert_eq!(TemplateModule::nft_item_id(collection_id, 1).variable_data, variable_data); + }); +} + +#[test] +fn set_variable_meta_data_on_re_fungible_token_stores_variable_meta_data() { + new_test_ext().execute_with(|| { + default_limits(); + + let collection_id = create_test_collection(&CollectionMode::ReFungible(3), 1); + + let origin1 = Origin::signed(1); + + let data = default_re_fungible_data(); + create_test_item(1, &data.into()); + + let variable_data = b"test set_variable_meta_data method.".to_vec(); + assert_ok!(TemplateModule::set_variable_meta_data(origin1, collection_id, 1, variable_data.clone())); + + assert_eq!(TemplateModule::refungible_item_id(collection_id, 1).variable_data, variable_data); + }); +} + + +#[test] +fn set_variable_meta_data_on_fungible_token_fails() { + new_test_ext().execute_with(|| { + default_limits(); + + let collection_id = create_test_collection(&CollectionMode::Fungible(3), 1); + + let origin1 = Origin::signed(1); + + let data = default_fungible_data(); + create_test_item(1, &data.into()); + + let variable_data = b"test set_variable_meta_data method.".to_vec(); + assert_noop!(TemplateModule::set_variable_meta_data(origin1, collection_id, 1, variable_data.clone()), Error::::CantStoreMetadataInFungibleTokens); + }); +} + +#[test] +fn set_variable_meta_data_on_nft_token_fails_for_big_data() { + new_test_ext().execute_with(|| { + assert_ok!(TemplateModule::set_chain_limits(RawOrigin::Root.into(), ChainLimits { + collection_numbers_limit: default_collection_numbers_limit(), + account_token_ownership_limit: 10, + collections_admins_limit: 5, + custom_data_limit: 10, + nft_sponsor_transfer_timeout: 15, + fungible_sponsor_transfer_timeout: 15, + refungible_sponsor_transfer_timeout: 15, + })); + + let collection_id = create_test_collection(&CollectionMode::NFT, 1); + + let origin1 = Origin::signed(1); + + let data = default_nft_data(); + create_test_item(1, &data.into()); + + let variable_data = b"test set_variable_meta_data method, bigger than limits.".to_vec(); + assert_noop!(TemplateModule::set_variable_meta_data(origin1, collection_id, 1, variable_data), Error::::TokenVariableDataLimitExceeded); + }); +} + +#[test] +fn set_variable_meta_data_on_re_fungible_token_fails_for_big_data() { + new_test_ext().execute_with(|| { + assert_ok!(TemplateModule::set_chain_limits(RawOrigin::Root.into(), ChainLimits { + collection_numbers_limit: default_collection_numbers_limit(), + account_token_ownership_limit: 10, + collections_admins_limit: 5, + custom_data_limit: 10, + nft_sponsor_transfer_timeout: 15, + fungible_sponsor_transfer_timeout: 15, + refungible_sponsor_transfer_timeout: 15, + })); + + + let collection_id = create_test_collection(&CollectionMode::ReFungible(3), 1); + + let origin1 = Origin::signed(1); + + let data = default_re_fungible_data(); + create_test_item(1, &data.into()); + + let variable_data = b"test set_variable_meta_data method, bigger than limits.".to_vec(); + assert_noop!(TemplateModule::set_variable_meta_data(origin1, collection_id, 1, variable_data), Error::::TokenVariableDataLimitExceeded); + }); +}