git.delta.rocks / unique-network / refs/commits / e9c5c5eb8270

difftreelog

feat implement setVariableMetadata sponsoring

Yaroslav Bolyukin2021-02-25parent: #65e4349.patch.diff
in: master

3 files changed

modifiedpallets/nft/src/default_weights.rsdiffbeforeafterboth
--- a/pallets/nft/src/default_weights.rs
+++ b/pallets/nft/src/default_weights.rs
@@ -127,6 +127,11 @@
             .saturating_add(DbWeight::get().reads(0 as Weight))
             .saturating_add(DbWeight::get().writes(2 as Weight))
     }    
+    fn set_variable_meta_data_sponsoring_rate_limit() -> Weight {
+        (3_500_000 as Weight)
+            .saturating_add(DbWeight::get().reads(2 as Weight))
+            .saturating_add(DbWeight::get().writes(1 as Weight))
+    }
     fn toggle_contract_white_list() -> Weight {
         (3_000_000 as Weight)
             .saturating_add(DbWeight::get().reads(0 as Weight))
modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
247 fn set_schema_version() -> Weight;247 fn set_schema_version() -> Weight;
248 fn set_chain_limits() -> Weight;248 fn set_chain_limits() -> Weight;
249 fn set_contract_sponsoring_rate_limit() -> Weight;249 fn set_contract_sponsoring_rate_limit() -> Weight;
250 fn set_variable_meta_data_sponsoring_rate_limit() -> Weight;
250 fn toggle_contract_white_list() -> Weight;251 fn toggle_contract_white_list() -> Weight;
251 fn add_to_contract_white_list() -> Weight;252 fn add_to_contract_white_list() -> Weight;
252 fn remove_from_contract_white_list() -> Weight;253 fn remove_from_contract_white_list() -> Weight;
442 pub FungibleTransferBasket get(fn fungible_transfer_basket): double_map hasher(identity) CollectionId, hasher(twox_64_concat) T::AccountId => T::BlockNumber;443 pub FungibleTransferBasket get(fn fungible_transfer_basket): double_map hasher(identity) CollectionId, hasher(twox_64_concat) T::AccountId => T::BlockNumber;
443 pub ReFungibleTransferBasket get(fn refungible_transfer_basket): double_map hasher(identity) CollectionId, hasher(identity) TokenId => T::BlockNumber;444 pub ReFungibleTransferBasket get(fn refungible_transfer_basket): double_map hasher(identity) CollectionId, hasher(identity) TokenId => T::BlockNumber;
445
446 /// Variable metadata sponsoring
447 pub VariableMetaDataBasket get(fn variable_meta_data_basket): double_map hasher(identity) CollectionId, hasher(identity) TokenId => Option<T::BlockNumber> = None;
448 pub VariableMetaDataSponsoringRateLimit get(fn variable_meta_data_sponsoring_rate_limit): map hasher(identity) CollectionId => T::BlockNumber = 0.into();
444449
445 // Contract Sponsorship and Ownership450 // Contract Sponsorship and Ownership
446 pub ContractOwner get(fn contract_owner): map hasher(twox_64_concat) T::AccountId => T::AccountId;451 pub ContractOwner get(fn contract_owner): map hasher(twox_64_concat) T::AccountId => T::AccountId;
640 <FungibleTransferBasket<T>>::remove_prefix(collection_id);645 <FungibleTransferBasket<T>>::remove_prefix(collection_id);
641 <ReFungibleTransferBasket<T>>::remove_prefix(collection_id);646 <ReFungibleTransferBasket<T>>::remove_prefix(collection_id);
647
648 <VariableMetaDataBasket<T>>::remove_prefix(collection_id);
649 <VariableMetaDataSponsoringRateLimit<T>>::remove(collection_id);
642650
643 if CollectionCount::get() > 0651 if CollectionCount::get() > 0
644 {652 {
1424 Ok(())1432 Ok(())
1425 }1433 }
1434
1435 #[weight = <T as Config>::WeightInfo::set_variable_meta_data_sponsoring_rate_limit()]
1436 pub fn set_variable_meta_data_sponsoring_rate_limit(
1437 origin,
1438 collection_id: CollectionId,
1439 rate_limit: T::BlockNumber
1440 ) -> DispatchResult {
1441 let sender = ensure_signed(origin)?;
1442 Self::check_collection_sponsor(collection_id.clone(), sender.clone())?;
1443
1444 <VariableMetaDataSponsoringRateLimit<T>>::insert(collection_id, rate_limit);
1445 Ok(())
1446 }
14261447
1427 /// Enable the white list for a contract. Only addresses added to the white list with addToContractWhiteList will be able to call this smart contract.1448 /// Enable the white list for a contract. Only addresses added to the white list with addToContractWhiteList will be able to call this smart contract.
1428 /// 1449 ///
1866 Ok(())1887 Ok(())
1867 }1888 }
1889
1890 fn check_collection_sponsor(
1891 collection_id: CollectionId,
1892 subject: T::AccountId,
1893 ) -> DispatchResult {
1894 Self::collection_exists(collection_id)?;
1895 let collection = <Collection<T>>::get(collection_id);
1896
1897 ensure!(
1898 collection.sponsor_confirmed &&
1899 collection.sponsor == subject,
1900 Error::<T>::NoPermission,
1901 );
1902 Ok(())
1903 }
18681904
1869 fn is_item_owner(subject: T::AccountId, collection_id: CollectionId, item_id: TokenId) -> bool {1905 fn is_item_owner(subject: T::AccountId, collection_id: CollectionId, item_id: TokenId) -> bool {
1870 let target_collection = <Collection<T>>::get(collection_id);1906 let target_collection = <Collection<T>>::get(collection_id);
2467 }2503 }
2468 }2504 }
2505
2506 Some(Call::set_variable_meta_data(collection_id, item_id, data)) => {
2507 let mut sponsor_metadata_changes = false;
2508
2509 let collection = <Collection<T>>::get(collection_id);
2510 if
2511 collection.sponsor_confirmed &&
2512 // Can't sponsor fungible collection, this tx will be rejected
2513 // as invalid
2514 !matches!(collection.mode, CollectionMode::Fungible(_)) &&
2515 data.len() <= collection.limits.sponsored_data_size as usize
2516 {
2517 let rate_limit = <VariableMetaDataSponsoringRateLimit<T>>::get(collection_id);
2518 if rate_limit > 0.into() {
2519 // sponsor timeout
2520 let block_number = <system::Module<T>>::block_number() as T::BlockNumber;
2521
2522 if <VariableMetaDataBasket<T>>::get(collection_id, item_id)
2523 .map(|last_block| block_number - last_block >= rate_limit)
2524 .unwrap_or(true)
2525 {
2526 sponsor_metadata_changes = true;
2527 <VariableMetaDataBasket<T>>::insert(collection_id, item_id, block_number);
2528 }
2529 }
2530 }
2531
2532 if !sponsor_metadata_changes {
2533 T::AccountId::default()
2534 } else {
2535 <Collection<T>>::get(collection_id).sponsor
2536 }
2537 }
24692538
2470 _ => T::AccountId::default(),2539 _ => T::AccountId::default(),
2471 };2540 };
modifiedruntime/src/nft_weights.rsdiffbeforeafterboth
--- a/runtime/src/nft_weights.rs
+++ b/runtime/src/nft_weights.rs
@@ -133,6 +133,11 @@
             .saturating_add(DbWeight::get().reads(0 as Weight))
             .saturating_add(DbWeight::get().writes(2 as Weight))
     }    
+    fn set_variable_meta_data_sponsoring_rate_limit() -> Weight {
+        (3_500_000 as Weight)
+            .saturating_add(DbWeight::get().reads(1 as Weight))
+            .saturating_add(DbWeight::get().writes(2 as Weight))
+    }
     fn toggle_contract_white_list() -> Weight {
         (3_000_000 as Weight)
             .saturating_add(DbWeight::get().reads(0 as Weight))