git.delta.rocks / unique-network / refs/commits / 03c27a6027ca

difftreelog

Merge pull request #22 from usetech-llc/feature/NFTPAR-142

usetech-llc2020-12-08parents: #7be352d #d855cf8.patch.diff
in: master
NFTPAR-142 Off-chain and on-chain data schema.

2 files changed

modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
264decl_error! {264decl_error! {
265 /// Error for non-fungible-token module.265 /// Error for non-fungible-token module.
266 pub enum Error for Module<T: Trait> {266 pub enum Error for Module<T: Trait> {
267 /// Total collections bound exceeded267 /// Total collections bound exceeded.
268 TotalCollectionsLimitExceeded,268 TotalCollectionsLimitExceeded,
269 /// Decimal_points parameter must be lower than 4269 /// Decimal_points parameter must be lower than 4.
270 CollectionDecimalPointLimitExceeded, 270 CollectionDecimalPointLimitExceeded,
271 /// Collection name can not be longer than 63 char271 /// Collection name can not be longer than 63 char.
272 CollectionNameLimitExceeded, 272 CollectionNameLimitExceeded,
273 /// Collection description can not be longer than 255 char273 /// Collection description can not be longer than 255 char.
274 CollectionDescriptionLimitExceeded, 274 CollectionDescriptionLimitExceeded,
275 /// Token prefix can not be longer than 15 char275 /// Token prefix can not be longer than 15 char.
276 CollectionTokenPrefixLimitExceeded,276 CollectionTokenPrefixLimitExceeded,
277 /// This collection does not exist277 /// This collection does not exist.
278 CollectionNotFound,278 CollectionNotFound,
279 /// Item not exists279 /// Item not exists.
280 TokenNotFound,280 TokenNotFound,
281 /// Arithmetic calculation overflow281 /// Arithmetic calculation overflow.
282 NumOverflow, 282 NumOverflow,
283 /// Account already has admin role283 /// Account already has admin role.
284 AlreadyAdmin, 284 AlreadyAdmin,
285 /// You do not own this collection285 /// You do not own this collection.
286 NoPermission,286 NoPermission,
287 /// This address is not set as sponsor, use setCollectionSponsor first287 /// This address is not set as sponsor, use setCollectionSponsor first.
288 ConfirmUnsetSponsorFail,288 ConfirmUnsetSponsorFail,
289 /// Collection is not in mint mode289 /// Collection is not in mint mode.
290 PublicMintingNotAllowed,290 PublicMintingNotAllowed,
291 /// Sender parameter and item owner must be equal291 /// Sender parameter and item owner must be equal.
292 MustBeTokenOwner,292 MustBeTokenOwner,
293 /// Item balance not enouth293 /// Item balance not enough.
294 TokenValueTooLow,294 TokenValueTooLow,
295 /// Size of item is too large295 /// Size of item is too large.
296 NftSizeLimitExceeded,296 NftSizeLimitExceeded,
297 /// Size of item must be 0 with fungible type
298 FungibleUnexpectedParam,
299 /// No approve found297 /// No approve found
300 ApproveNotFound,298 ApproveNotFound,
301 /// Requested value more than approved299 /// Requested value more than approved.
302 TokenValueNotEnough,300 TokenValueNotEnough,
303 /// Only approved addresses can call this method301 /// Only approved addresses can call this method.
304 ApproveRequired,302 ApproveRequired,
305 /// Address is not in white list303 /// Address is not in white list.
306 AddresNotInWhiteList,304 AddresNotInWhiteList,
307 /// Number of collection admins bound exceeded305 /// Number of collection admins bound exceeded.
308 CollectionAdminsLimitExceeded,306 CollectionAdminsLimitExceeded,
309 /// Owned tokens by a single address bound exceeded307 /// Owned tokens by a single address bound exceeded.
310 AddressOwnershipLimitExceeded,308 AddressOwnershipLimitExceeded,
311 /// Length of items properties must be greater than 0309 /// Length of items properties must be greater than 0.
312 EmptyArgument,310 EmptyArgument,
311 /// const_data exceeded data limit.
312 TokenConstDataLimitExceeded,
313 /// variable_data exceeded data limit.
314 TokenVariableDataLimitExceeded,
315 /// Not NFT item data used to mint in NFT collection.
316 NotNftDataUsedToMintNftCollectionToken,
317 /// Not Fungible item data used to mint in Fungible collection.
318 NotFungibleDataUsedToMintFungibleCollectionToken,
319 /// Not Re Fungible item data used to mint in Re Fungible collection.
320 NotReFungibleDataUsedToMintReFungibleCollectionToken,
321 /// Unexpected collection type.
322 UnexpectedCollectionType,
323 /// Can't store metadata in fungible tokens.
324 CantStoreMetadataInFungibleTokens
313 }325 }
314}326}
315327
1182 1194
1183 Self::collection_exists(collection_id)?;1195 Self::collection_exists(collection_id)?;
1196
1197 ensure!(ChainLimit::get().custom_data_limit >= data.len() as u32, Error::<T>::TokenVariableDataLimitExceeded);
11841198
1185 // Modify permissions check1199 // Modify permissions check
1186 let target_collection = <Collection<T>>::get(collection_id);1200 let target_collection = <Collection<T>>::get(collection_id);
1194 {1208 {
1195 CollectionMode::NFT => Self::set_nft_variable_data(collection_id, item_id, data)?,1209 CollectionMode::NFT => Self::set_nft_variable_data(collection_id, item_id, data)?,
1196 CollectionMode::ReFungible(_) => Self::set_re_fungible_variable_data(collection_id, item_id, data)?,1210 CollectionMode::ReFungible(_) => Self::set_re_fungible_variable_data(collection_id, item_id, data)?,
1211 CollectionMode::Fungible(_) => fail!(Error::<T>::CantStoreMetadataInFungibleTokens),
1197 _ => ()1212 _ => fail!(Error::<T>::UnexpectedCollectionType)
1198 };1213 };
11991214
1200 Ok(())1215 Ok(())
1382 CollectionMode::NFT => {1397 CollectionMode::NFT => {
1383 if let CreateItemData::NFT(data) = data {1398 if let CreateItemData::NFT(data) = data {
1384 // check sizes1399 // check sizes
1385 ensure!(ChainLimit::get().custom_data_limit >= data.const_data.len() as u32, "const_data exceeded data limit.");1400 ensure!(ChainLimit::get().custom_data_limit >= data.const_data.len() as u32, Error::<T>::TokenConstDataLimitExceeded);
1386 ensure!(ChainLimit::get().custom_data_limit >= data.variable_data.len() as u32, "variable_data exceeded data limit.");1401 ensure!(ChainLimit::get().custom_data_limit >= data.variable_data.len() as u32, Error::<T>::TokenVariableDataLimitExceeded);
1387 } else {1402 } else {
1388 fail!("Not NFT item data used to mint in NFT collection.");1403 fail!(Error::<T>::NotNftDataUsedToMintNftCollectionToken);
1389 }1404 }
1390 },1405 },
1391 CollectionMode::Fungible(_) => {1406 CollectionMode::Fungible(_) => {
1392 if let CreateItemData::Fungible(_) = data {1407 if let CreateItemData::Fungible(_) = data {
1393 } else {1408 } else {
1394 fail!("Not Fungible item data used to mint in Fungible collection.");1409 fail!(Error::<T>::NotFungibleDataUsedToMintFungibleCollectionToken);
1395 }1410 }
1396 },1411 },
1397 CollectionMode::ReFungible(_) => {1412 CollectionMode::ReFungible(_) => {
1398 if let CreateItemData::ReFungible(data) = data {1413 if let CreateItemData::ReFungible(data) = data {
13991414
1400 // check sizes1415 // check sizes
1401 ensure!(ChainLimit::get().custom_data_limit >= data.const_data.len() as u32, "const_data exceeded data limit.");1416 ensure!(ChainLimit::get().custom_data_limit >= data.const_data.len() as u32, Error::<T>::TokenConstDataLimitExceeded);
1402 ensure!(ChainLimit::get().custom_data_limit >= data.variable_data.len() as u32, "variable_data exceeded data limit.");1417 ensure!(ChainLimit::get().custom_data_limit >= data.variable_data.len() as u32, Error::<T>::TokenVariableDataLimitExceeded);
1403 } else {1418 } else {
1404 fail!("Not Re Fungible item data used to mint in Re Fungible collection.");1419 fail!(Error::<T>::NotReFungibleDataUsedToMintReFungibleCollectionToken);
1405 }1420 }
1406 },1421 },
1407 _ => { fail!("Unexpected collection type."); }1422 _ => { fail!(Error::<T>::UnexpectedCollectionType); }
1408 };1423 };
14091424
1410 Ok(())1425 Ok(())
modifiedpallets/nft/src/tests.rsdiffbeforeafterboth
--- 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::<Test>::TokenConstDataLimitExceeded);
     });
 }
 
@@ -1756,7 +1756,7 @@
             collection_id,
             1,
             too_big_const_data
-        ), "variable_data exceeded data limit.");
+        ), Error::<Test>::TokenVariableDataLimitExceeded);
     });
 }
 
@@ -1787,7 +1787,7 @@
             collection_id,
             1,
             too_big_const_data
-        ), "const_data exceeded data limit.");
+        ), Error::<Test>::TokenConstDataLimitExceeded);
     });
 }
 
@@ -1818,7 +1818,7 @@
             collection_id,
             1,
             too_big_const_data
-        ), "variable_data exceeded data limit.");
+        ), Error::<Test>::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::<Test>::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::<Test>::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::<Test>::TokenVariableDataLimitExceeded);
+    });
+}