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
--- a/pallets/nft/src/lib.rs
+++ b/pallets/nft/src/lib.rs
@@ -247,6 +247,7 @@
     fn set_schema_version() -> Weight;
     fn set_chain_limits() -> Weight;
     fn set_contract_sponsoring_rate_limit() -> Weight;
+    fn set_variable_meta_data_sponsoring_rate_limit() -> Weight;
     fn toggle_contract_white_list() -> Weight;
     fn add_to_contract_white_list() -> Weight;
     fn remove_from_contract_white_list() -> Weight;
@@ -442,6 +443,10 @@
         pub FungibleTransferBasket get(fn fungible_transfer_basket): double_map hasher(identity) CollectionId, hasher(twox_64_concat) T::AccountId => T::BlockNumber;
         pub ReFungibleTransferBasket get(fn refungible_transfer_basket): double_map hasher(identity) CollectionId, hasher(identity) TokenId => T::BlockNumber;
 
+        /// Variable metadata sponsoring
+        pub VariableMetaDataBasket get(fn variable_meta_data_basket): double_map hasher(identity) CollectionId, hasher(identity) TokenId => Option<T::BlockNumber> = None;
+        pub VariableMetaDataSponsoringRateLimit get(fn variable_meta_data_sponsoring_rate_limit): map hasher(identity) CollectionId => T::BlockNumber = 0.into();
+
         // Contract Sponsorship and Ownership
         pub ContractOwner get(fn contract_owner): map hasher(twox_64_concat) T::AccountId => T::AccountId;
         pub ContractSelfSponsoring get(fn contract_self_sponsoring): map hasher(twox_64_concat) T::AccountId => bool;
@@ -640,6 +645,9 @@
             <FungibleTransferBasket<T>>::remove_prefix(collection_id);
             <ReFungibleTransferBasket<T>>::remove_prefix(collection_id);
 
+            <VariableMetaDataBasket<T>>::remove_prefix(collection_id);
+            <VariableMetaDataSponsoringRateLimit<T>>::remove(collection_id);
+
             if CollectionCount::get() > 0
             {
                 // bound couter
@@ -1424,6 +1432,19 @@
             Ok(())
         }
 
+        #[weight = <T as Config>::WeightInfo::set_variable_meta_data_sponsoring_rate_limit()]
+        pub fn set_variable_meta_data_sponsoring_rate_limit(
+            origin,
+            collection_id: CollectionId,
+            rate_limit: T::BlockNumber
+        ) -> DispatchResult {
+            let sender = ensure_signed(origin)?;
+            Self::check_collection_sponsor(collection_id.clone(), sender.clone())?;
+
+            <VariableMetaDataSponsoringRateLimit<T>>::insert(collection_id, rate_limit);
+            Ok(())
+        }
+
         /// Enable the white list for a contract. Only addresses added to the white list with addToContractWhiteList will be able to call this smart contract.
         /// 
         /// # Permissions
@@ -1866,6 +1887,21 @@
         Ok(())
     }
 
+    fn check_collection_sponsor(
+        collection_id: CollectionId,
+        subject: T::AccountId,
+    ) -> DispatchResult {
+        Self::collection_exists(collection_id)?;
+        let collection = <Collection<T>>::get(collection_id);
+        
+        ensure!(
+            collection.sponsor_confirmed &&
+            collection.sponsor == subject,
+            Error::<T>::NoPermission,
+        );
+        Ok(())
+    }
+
     fn is_item_owner(subject: T::AccountId, collection_id: CollectionId, item_id: TokenId) -> bool {
         let target_collection = <Collection<T>>::get(collection_id);
 
@@ -2467,6 +2503,39 @@
                 }
             }
 
+            Some(Call::set_variable_meta_data(collection_id, item_id, data)) => {
+                let mut sponsor_metadata_changes = false;
+
+                let collection = <Collection<T>>::get(collection_id);
+                if
+                    collection.sponsor_confirmed &&
+                    // Can't sponsor fungible collection, this tx will be rejected
+                    // as invalid
+                    !matches!(collection.mode, CollectionMode::Fungible(_)) &&
+                    data.len() <= collection.limits.sponsored_data_size as usize
+                {
+                    let rate_limit = <VariableMetaDataSponsoringRateLimit<T>>::get(collection_id);
+                    if rate_limit > 0.into() {
+                        // sponsor timeout
+                        let block_number = <system::Module<T>>::block_number() as T::BlockNumber;
+
+                        if <VariableMetaDataBasket<T>>::get(collection_id, item_id)
+                            .map(|last_block| block_number - last_block >= rate_limit)
+                            .unwrap_or(true) 
+                        {
+                            sponsor_metadata_changes = true;
+                            <VariableMetaDataBasket<T>>::insert(collection_id, item_id, block_number);
+                        }
+                    }
+                }
+
+                if !sponsor_metadata_changes {
+                    T::AccountId::default()
+                } else {
+                    <Collection<T>>::get(collection_id).sponsor
+                }
+            }
+
             _ => T::AccountId::default(),
         };
 
modifiedruntime/src/nft_weights.rsdiffbeforeafterboth
before · runtime/src/nft_weights.rs
1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//56use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight};78pub struct WeightInfo;9impl pallet_nft::WeightInfo for WeightInfo {10	fn create_collection() -> Weight {11		(70_000_000 as Weight)12			.saturating_add(DbWeight::get().reads(7 as Weight))13			.saturating_add(DbWeight::get().writes(5 as Weight))14	}15	fn destroy_collection() -> Weight {16		(90_000_000 as Weight)17			.saturating_add(DbWeight::get().reads(2 as Weight))18			.saturating_add(DbWeight::get().writes(5 as Weight))19	}20	fn add_to_white_list() -> Weight {21		(30_000_000 as Weight)22			.saturating_add(DbWeight::get().reads(3 as Weight))23			.saturating_add(DbWeight::get().writes(1 as Weight))24    }25    fn remove_from_white_list() -> Weight {26		(35_000_000 as Weight)27			.saturating_add(DbWeight::get().reads(3 as Weight))28			.saturating_add(DbWeight::get().writes(1 as Weight))29	}30	fn set_public_access_mode() -> Weight {31		(27_000_000 as Weight)32			.saturating_add(DbWeight::get().reads(1 as Weight))33			.saturating_add(DbWeight::get().writes(1 as Weight))34	}35	fn set_mint_permission() -> Weight {36		(27_000_000 as Weight)37			.saturating_add(DbWeight::get().reads(1 as Weight))38			.saturating_add(DbWeight::get().writes(1 as Weight))39	}40	fn change_collection_owner() -> Weight {41		(27_000_000 as Weight)42			.saturating_add(DbWeight::get().reads(1 as Weight))43			.saturating_add(DbWeight::get().writes(1 as Weight))44	}45	fn add_collection_admin() -> Weight {46        (32_000_000 as Weight)47            .saturating_add(DbWeight::get().reads(3 as Weight))48            .saturating_add(DbWeight::get().writes(1 as Weight))49	}50	fn remove_collection_admin() -> Weight {51		(50_000_000 as Weight)52            .saturating_add(DbWeight::get().reads(2 as Weight))53            .saturating_add(DbWeight::get().writes(1 as Weight))54    }55    fn set_collection_sponsor() -> Weight {56		(32_000_000 as Weight)57            .saturating_add(DbWeight::get().reads(2 as Weight))58            .saturating_add(DbWeight::get().writes(1 as Weight))59    }  60    fn confirm_sponsorship() -> Weight {61		(22_000_000 as Weight)62            .saturating_add(DbWeight::get().reads(1 as Weight))63            .saturating_add(DbWeight::get().writes(1 as Weight))64    }  65    fn remove_collection_sponsor() -> Weight {66		(24_000_000 as Weight)67            .saturating_add(DbWeight::get().reads(1 as Weight))68            .saturating_add(DbWeight::get().writes(1 as Weight))69    }  70    fn create_item(s: usize, ) -> Weight {71        (130_000_000 as Weight)72            .saturating_add((2135 as Weight).saturating_mul(s as Weight).saturating_mul(500 as Weight)) // 500 is temparary multiplier, fee for storage73            .saturating_add(DbWeight::get().reads(10 as Weight))74            .saturating_add(DbWeight::get().writes(8 as Weight))75    }  76    fn burn_item() -> Weight {77		(170_000_000 as Weight)78            .saturating_add(DbWeight::get().reads(9 as Weight))79            .saturating_add(DbWeight::get().writes(7 as Weight))80    }  81    fn transfer() -> Weight {82        (125_000_000 as Weight)83            .saturating_add(DbWeight::get().reads(7 as Weight))84            .saturating_add(DbWeight::get().writes(7 as Weight))85    }  86    fn approve() -> Weight {87        (45_000_000 as Weight)88            .saturating_add(DbWeight::get().reads(3 as Weight))89            .saturating_add(DbWeight::get().writes(1 as Weight))90    }91    fn transfer_from() -> Weight {92        (150_000_000 as Weight)93            .saturating_add(DbWeight::get().reads(9 as Weight))94            .saturating_add(DbWeight::get().writes(8 as Weight))95    }96    fn set_offchain_schema() -> Weight {97        (33_000_000 as Weight)98            .saturating_add(DbWeight::get().reads(2 as Weight))99            .saturating_add(DbWeight::get().writes(1 as Weight))100    }101    fn set_const_on_chain_schema() -> Weight {102        (11_100_000 as Weight)103            .saturating_add(DbWeight::get().reads(2 as Weight))104            .saturating_add(DbWeight::get().writes(1 as Weight))105    }106    fn set_variable_on_chain_schema() -> Weight {107        (11_100_000 as Weight)108            .saturating_add(DbWeight::get().reads(2 as Weight))109            .saturating_add(DbWeight::get().writes(1 as Weight))110    }111    fn set_variable_meta_data() -> Weight {112        (17_500_000 as Weight)113            .saturating_add(DbWeight::get().reads(2 as Weight))114            .saturating_add(DbWeight::get().writes(1 as Weight))115    }116    fn enable_contract_sponsoring() -> Weight {117        (13_000_000 as Weight)118            .saturating_add(DbWeight::get().reads(1 as Weight))119            .saturating_add(DbWeight::get().writes(1 as Weight))120    }121    fn set_schema_version() -> Weight {122        (8_500_000 as Weight)123            .saturating_add(DbWeight::get().reads(2 as Weight))124            .saturating_add(DbWeight::get().writes(1 as Weight))125    }126    fn set_chain_limits() -> Weight {127        (1_300_000 as Weight)128            .saturating_add(DbWeight::get().reads(0 as Weight))129            .saturating_add(DbWeight::get().writes(1 as Weight))130    }131    fn set_contract_sponsoring_rate_limit() -> Weight {132        (3_500_000 as Weight)133            .saturating_add(DbWeight::get().reads(0 as Weight))134            .saturating_add(DbWeight::get().writes(2 as Weight))135    }    136    fn toggle_contract_white_list() -> Weight {137        (3_000_000 as Weight)138            .saturating_add(DbWeight::get().reads(0 as Weight))139            .saturating_add(DbWeight::get().writes(2 as Weight))140    }    141    fn add_to_contract_white_list() -> Weight {142        (3_000_000 as Weight)143            .saturating_add(DbWeight::get().reads(0 as Weight))144            .saturating_add(DbWeight::get().writes(2 as Weight))145    }    146    fn remove_from_contract_white_list() -> Weight {147        (3_200_000 as Weight)148            .saturating_add(DbWeight::get().reads(0 as Weight))149            .saturating_add(DbWeight::get().writes(2 as Weight))150    }151    fn set_collection_limits() -> Weight {152        (8_900_000 as Weight)153            .saturating_add(DbWeight::get().reads(2 as Weight))154            .saturating_add(DbWeight::get().writes(1 as Weight))155    }156}
after · runtime/src/nft_weights.rs
1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//56use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight};78pub struct WeightInfo;9impl pallet_nft::WeightInfo for WeightInfo {10	fn create_collection() -> Weight {11		(70_000_000 as Weight)12			.saturating_add(DbWeight::get().reads(7 as Weight))13			.saturating_add(DbWeight::get().writes(5 as Weight))14	}15	fn destroy_collection() -> Weight {16		(90_000_000 as Weight)17			.saturating_add(DbWeight::get().reads(2 as Weight))18			.saturating_add(DbWeight::get().writes(5 as Weight))19	}20	fn add_to_white_list() -> Weight {21		(30_000_000 as Weight)22			.saturating_add(DbWeight::get().reads(3 as Weight))23			.saturating_add(DbWeight::get().writes(1 as Weight))24    }25    fn remove_from_white_list() -> Weight {26		(35_000_000 as Weight)27			.saturating_add(DbWeight::get().reads(3 as Weight))28			.saturating_add(DbWeight::get().writes(1 as Weight))29	}30	fn set_public_access_mode() -> Weight {31		(27_000_000 as Weight)32			.saturating_add(DbWeight::get().reads(1 as Weight))33			.saturating_add(DbWeight::get().writes(1 as Weight))34	}35	fn set_mint_permission() -> Weight {36		(27_000_000 as Weight)37			.saturating_add(DbWeight::get().reads(1 as Weight))38			.saturating_add(DbWeight::get().writes(1 as Weight))39	}40	fn change_collection_owner() -> Weight {41		(27_000_000 as Weight)42			.saturating_add(DbWeight::get().reads(1 as Weight))43			.saturating_add(DbWeight::get().writes(1 as Weight))44	}45	fn add_collection_admin() -> Weight {46        (32_000_000 as Weight)47            .saturating_add(DbWeight::get().reads(3 as Weight))48            .saturating_add(DbWeight::get().writes(1 as Weight))49	}50	fn remove_collection_admin() -> Weight {51		(50_000_000 as Weight)52            .saturating_add(DbWeight::get().reads(2 as Weight))53            .saturating_add(DbWeight::get().writes(1 as Weight))54    }55    fn set_collection_sponsor() -> Weight {56		(32_000_000 as Weight)57            .saturating_add(DbWeight::get().reads(2 as Weight))58            .saturating_add(DbWeight::get().writes(1 as Weight))59    }  60    fn confirm_sponsorship() -> Weight {61		(22_000_000 as Weight)62            .saturating_add(DbWeight::get().reads(1 as Weight))63            .saturating_add(DbWeight::get().writes(1 as Weight))64    }  65    fn remove_collection_sponsor() -> Weight {66		(24_000_000 as Weight)67            .saturating_add(DbWeight::get().reads(1 as Weight))68            .saturating_add(DbWeight::get().writes(1 as Weight))69    }  70    fn create_item(s: usize, ) -> Weight {71        (130_000_000 as Weight)72            .saturating_add((2135 as Weight).saturating_mul(s as Weight).saturating_mul(500 as Weight)) // 500 is temparary multiplier, fee for storage73            .saturating_add(DbWeight::get().reads(10 as Weight))74            .saturating_add(DbWeight::get().writes(8 as Weight))75    }  76    fn burn_item() -> Weight {77		(170_000_000 as Weight)78            .saturating_add(DbWeight::get().reads(9 as Weight))79            .saturating_add(DbWeight::get().writes(7 as Weight))80    }  81    fn transfer() -> Weight {82        (125_000_000 as Weight)83            .saturating_add(DbWeight::get().reads(7 as Weight))84            .saturating_add(DbWeight::get().writes(7 as Weight))85    }  86    fn approve() -> Weight {87        (45_000_000 as Weight)88            .saturating_add(DbWeight::get().reads(3 as Weight))89            .saturating_add(DbWeight::get().writes(1 as Weight))90    }91    fn transfer_from() -> Weight {92        (150_000_000 as Weight)93            .saturating_add(DbWeight::get().reads(9 as Weight))94            .saturating_add(DbWeight::get().writes(8 as Weight))95    }96    fn set_offchain_schema() -> Weight {97        (33_000_000 as Weight)98            .saturating_add(DbWeight::get().reads(2 as Weight))99            .saturating_add(DbWeight::get().writes(1 as Weight))100    }101    fn set_const_on_chain_schema() -> Weight {102        (11_100_000 as Weight)103            .saturating_add(DbWeight::get().reads(2 as Weight))104            .saturating_add(DbWeight::get().writes(1 as Weight))105    }106    fn set_variable_on_chain_schema() -> Weight {107        (11_100_000 as Weight)108            .saturating_add(DbWeight::get().reads(2 as Weight))109            .saturating_add(DbWeight::get().writes(1 as Weight))110    }111    fn set_variable_meta_data() -> Weight {112        (17_500_000 as Weight)113            .saturating_add(DbWeight::get().reads(2 as Weight))114            .saturating_add(DbWeight::get().writes(1 as Weight))115    }116    fn enable_contract_sponsoring() -> Weight {117        (13_000_000 as Weight)118            .saturating_add(DbWeight::get().reads(1 as Weight))119            .saturating_add(DbWeight::get().writes(1 as Weight))120    }121    fn set_schema_version() -> Weight {122        (8_500_000 as Weight)123            .saturating_add(DbWeight::get().reads(2 as Weight))124            .saturating_add(DbWeight::get().writes(1 as Weight))125    }126    fn set_chain_limits() -> Weight {127        (1_300_000 as Weight)128            .saturating_add(DbWeight::get().reads(0 as Weight))129            .saturating_add(DbWeight::get().writes(1 as Weight))130    }131    fn set_contract_sponsoring_rate_limit() -> Weight {132        (3_500_000 as Weight)133            .saturating_add(DbWeight::get().reads(0 as Weight))134            .saturating_add(DbWeight::get().writes(2 as Weight))135    }    136    fn set_variable_meta_data_sponsoring_rate_limit() -> Weight {137        (3_500_000 as Weight)138            .saturating_add(DbWeight::get().reads(1 as Weight))139            .saturating_add(DbWeight::get().writes(2 as Weight))140    }141    fn toggle_contract_white_list() -> Weight {142        (3_000_000 as Weight)143            .saturating_add(DbWeight::get().reads(0 as Weight))144            .saturating_add(DbWeight::get().writes(2 as Weight))145    }    146    fn add_to_contract_white_list() -> Weight {147        (3_000_000 as Weight)148            .saturating_add(DbWeight::get().reads(0 as Weight))149            .saturating_add(DbWeight::get().writes(2 as Weight))150    }    151    fn remove_from_contract_white_list() -> Weight {152        (3_200_000 as Weight)153            .saturating_add(DbWeight::get().reads(0 as Weight))154            .saturating_add(DbWeight::get().writes(2 as Weight))155    }156    fn set_collection_limits() -> Weight {157        (8_900_000 as Weight)158            .saturating_add(DbWeight::get().reads(2 as Weight))159            .saturating_add(DbWeight::get().writes(1 as Weight))160    }161}