git.delta.rocks / unique-network / refs/commits / 73e422cd19aa

difftreelog

refactor remove id from collection info

Yaroslav Bolyukin2021-03-15parent: #3f9b318.patch.diff
in: master

3 files changed

modifiednode/src/chain_spec.rsdiffbeforeafterboth
--- a/node/src/chain_spec.rs
+++ b/node/src/chain_spec.rs
@@ -183,7 +183,6 @@
             collection_id: vec![(
                 1,
                 Collection {
-					id: 1,
                     owner: get_account_id_from_seed::<sr25519::Public>("Alice"),
                     mode: CollectionMode::NFT,
                     access: AccessMode::Normal,
modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
--- a/pallets/nft/src/lib.rs
+++ b/pallets/nft/src/lib.rs
@@ -13,6 +13,7 @@
 #[cfg(feature = "std")]
 pub use serde::*;
 
+use core::ops::{Deref, DerefMut};
 use codec::{Decode, Encode};
 pub use frame_support::{
     construct_runtime, decl_event, decl_module, decl_storage, decl_error,
@@ -126,7 +127,6 @@
 #[derive(Encode, Decode, Clone, PartialEq)]
 #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
 pub struct Collection<T: Config> {
-    pub id: CollectionId,
     pub owner: T::AccountId,
     pub mode: CollectionMode,
     pub access: AccessMode,
@@ -144,6 +144,25 @@
     pub const_on_chain_schema: Vec<u8>, //
 }
 
+pub struct CollectionHandle<T: Config> {
+    pub id: CollectionId,
+    collection: Collection<T>,
+}
+
+impl<T: Config> Deref for CollectionHandle<T> {
+    type Target = Collection<T>;
+
+    fn deref(&self) -> &Self::Target {
+        &self.collection
+    }
+}
+
+impl<T: Config> DerefMut for CollectionHandle<T> {
+    fn deref_mut(&mut self) -> &mut Self::Target {
+        &mut self.collection
+    }
+}
+
 #[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]
 #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
 pub struct NftItemType<AccountId> {
@@ -617,7 +636,6 @@
 
             // Create new collection
             let new_collection = Collection {
-                id: next_id,
                 owner: who.clone(),
                 name: collection_name,
                 mode: mode.clone(),
@@ -1579,7 +1597,7 @@
             let sender = ensure_signed(origin)?;
             let mut target_collection = Self::get_collection(collection_id)?;
             Self::check_owner_permissions(&target_collection, sender.clone())?;
-            let old_limits = target_collection.limits;
+            let old_limits = &target_collection.limits;
             let chain_limits = ChainLimit::get();
 
             // collection bounds
@@ -1608,7 +1626,7 @@
 
 impl<T: Config> Module<T> {
 
-    pub fn transfer_internal(sender: T::AccountId, recipient: T::AccountId, target_collection: &Collection<T>, item_id: TokenId, value: u128) -> DispatchResult {
+    pub fn transfer_internal(sender: T::AccountId, recipient: T::AccountId, target_collection: &CollectionHandle<T>, item_id: TokenId, value: u128) -> DispatchResult {
         // Limits check
         Self::is_correct_transfer(target_collection, &recipient)?;
 
@@ -1636,7 +1654,7 @@
     }
 
 
-    fn is_correct_transfer(collection: &Collection<T>, recipient: &T::AccountId) -> DispatchResult {
+    fn is_correct_transfer(collection: &CollectionHandle<T>, recipient: &T::AccountId) -> DispatchResult {
         let collection_id = collection.id;
 
         // check token limit and account token limit
@@ -1646,7 +1664,7 @@
         Ok(())
     }
 
-    fn can_create_items_in_collection(collection: &Collection<T>, sender: &T::AccountId, owner: &T::AccountId) -> DispatchResult {
+    fn can_create_items_in_collection(collection: &CollectionHandle<T>, sender: &T::AccountId, owner: &T::AccountId) -> DispatchResult {
         let collection_id = collection.id;
 
         // check token limit and account token limit
@@ -1664,7 +1682,7 @@
         Ok(())
     }
 
-    fn validate_create_item_args(target_collection: &Collection<T>, data: &CreateItemData) -> DispatchResult {
+    fn validate_create_item_args(target_collection: &CollectionHandle<T>, data: &CreateItemData) -> DispatchResult {
         match target_collection.mode
         {
             CollectionMode::NFT => {
@@ -1702,7 +1720,7 @@
         Ok(())
     }
 
-    fn create_item_no_validation(collection: &Collection<T>, owner: T::AccountId, data: CreateItemData) -> DispatchResult {
+    fn create_item_no_validation(collection: &CollectionHandle<T>, owner: T::AccountId, data: CreateItemData) -> DispatchResult {
         let collection_id = collection.id;
 
         match data
@@ -1739,7 +1757,7 @@
         Ok(())
     }
 
-    fn add_fungible_item(collection: &Collection<T>, owner: &T::AccountId, value: u128) -> DispatchResult {
+    fn add_fungible_item(collection: &CollectionHandle<T>, owner: &T::AccountId, value: u128) -> DispatchResult {
         let collection_id = collection.id;
 
         // Does new owner already have an account?
@@ -1763,7 +1781,7 @@
         Ok(())
     }
 
-    fn add_refungible_item(collection: &Collection<T>, item: ReFungibleItemType<T::AccountId>) -> DispatchResult {
+    fn add_refungible_item(collection: &CollectionHandle<T>, item: ReFungibleItemType<T::AccountId>) -> DispatchResult {
         let collection_id = collection.id;
 
         let current_index = <ItemListIndex>::get(collection_id)
@@ -1788,7 +1806,7 @@
         Ok(())
     }
 
-    fn add_nft_item(collection: &Collection<T>, item: NftItemType<T::AccountId>) -> DispatchResult {
+    fn add_nft_item(collection: &CollectionHandle<T>, item: NftItemType<T::AccountId>) -> DispatchResult {
         let collection_id = collection.id;
 
         let current_index = <ItemListIndex>::get(collection_id)
@@ -1811,7 +1829,7 @@
     }
 
     fn burn_refungible_item(
-        collection: &Collection<T>,
+        collection: &CollectionHandle<T>,
         item_id: TokenId,
         owner: &T::AccountId,
     ) -> DispatchResult {
@@ -1857,7 +1875,7 @@
         Ok(())
     }
 
-    fn burn_nft_item(collection: &Collection<T>, item_id: TokenId) -> DispatchResult {
+    fn burn_nft_item(collection: &CollectionHandle<T>, item_id: TokenId) -> DispatchResult {
         let collection_id = collection.id;
 
         ensure!(
@@ -1878,7 +1896,7 @@
         Ok(())
     }
 
-    fn burn_fungible_item(owner: &T::AccountId, collection: &Collection<T>, value: u128) -> DispatchResult {
+    fn burn_fungible_item(owner: &T::AccountId, collection: &CollectionHandle<T>, value: u128) -> DispatchResult {
         let collection_id = collection.id;
 
         ensure!(
@@ -1905,16 +1923,20 @@
         Ok(())
     }
 
-    pub fn get_collection(collection_id: CollectionId) -> Result<Collection<T>, sp_runtime::DispatchError> {
+    pub fn get_collection(collection_id: CollectionId) -> Result<CollectionHandle<T>, sp_runtime::DispatchError> {
         Ok(<CollectionById<T>>::get(collection_id)
+            .map(|collection| CollectionHandle {
+                id: collection_id,
+                collection
+            })
             .ok_or(Error::<T>::CollectionNotFound)?)
     }
 
-    fn save_collection(collection: Collection<T>) {
-        <CollectionById<T>>::insert(collection.id, collection);
+    fn save_collection(collection: CollectionHandle<T>) {
+        <CollectionById<T>>::insert(collection.id, collection.collection);
     }
 
-    fn check_owner_permissions(target_collection: &Collection<T>, subject: T::AccountId) -> DispatchResult {
+    fn check_owner_permissions(target_collection: &CollectionHandle<T>, subject: T::AccountId) -> DispatchResult {
         ensure!(
             subject == target_collection.owner,
             Error::<T>::NoPermission
@@ -1923,7 +1945,7 @@
         Ok(())
     }
 
-    fn is_owner_or_admin_permissions(collection: &Collection<T>, subject: T::AccountId) -> bool {
+    fn is_owner_or_admin_permissions(collection: &CollectionHandle<T>, subject: T::AccountId) -> bool {
         let mut result: bool = subject == collection.owner;
         let exists = <AdminList<T>>::contains_key(collection.id);
 
@@ -1937,7 +1959,7 @@
     }
 
     fn check_owner_or_admin_permissions(
-        collection: &Collection<T>,
+        collection: &CollectionHandle<T>,
         subject: T::AccountId,
     ) -> DispatchResult {
         let result = Self::is_owner_or_admin_permissions(collection, subject.clone());
@@ -1951,7 +1973,7 @@
 
     fn owned_amount(
         subject: T::AccountId,
-        target_collection: &Collection<T>,
+        target_collection: &CollectionHandle<T>,
         item_id: TokenId,
     ) -> Option<u128> {
         let collection_id = target_collection.id;
@@ -1979,7 +2001,7 @@
         }
     }
 
-    fn is_item_owner(subject: T::AccountId, target_collection: &Collection<T>, item_id: TokenId) -> bool {
+    fn is_item_owner(subject: T::AccountId, target_collection: &CollectionHandle<T>, item_id: TokenId) -> bool {
         let collection_id = target_collection.id;
 
         match target_collection.mode {
@@ -1999,7 +2021,7 @@
         }
     }
 
-    fn check_white_list(collection: &Collection<T>, address: &T::AccountId) -> DispatchResult {
+    fn check_white_list(collection: &CollectionHandle<T>, address: &T::AccountId) -> DispatchResult {
         let collection_id = collection.id;
 
         let mes = Error::<T>::AddresNotInWhiteList;
@@ -2011,7 +2033,7 @@
     /// Check if token exists. In case of Fungible, check if there is an entry for 
     /// the owner in fungible balances double map
     fn token_exists(
-        target_collection: &Collection<T>,
+        target_collection: &CollectionHandle<T>,
         item_id: TokenId,
         owner: &T::AccountId
     ) -> DispatchResult {
@@ -2029,7 +2051,7 @@
     }
 
     fn transfer_fungible(
-        collection: &Collection<T>,
+        collection: &CollectionHandle<T>,
         value: u128,
         owner: &T::AccountId,
         recipient: &T::AccountId,
@@ -2059,7 +2081,7 @@
     }
 
     fn transfer_refungible(
-        collection: &Collection<T>,
+        collection: &CollectionHandle<T>,
         item_id: TokenId,
         value: u128,
         owner: T::AccountId,
@@ -2142,7 +2164,7 @@
     }
 
     fn transfer_nft(
-        collection: &Collection<T>,
+        collection: &CollectionHandle<T>,
         item_id: TokenId,
         sender: T::AccountId,
         new_owner: T::AccountId,
@@ -2180,7 +2202,7 @@
     }
     
     fn set_re_fungible_variable_data(
-        collection: &Collection<T>,
+        collection: &CollectionHandle<T>,
         item_id: TokenId,
         data: Vec<u8>
     ) -> DispatchResult {
@@ -2195,7 +2217,7 @@
     }
 
     fn set_nft_variable_data(
-        collection: &Collection<T>,
+        collection: &CollectionHandle<T>,
         item_id: TokenId,
         data: Vec<u8>
     ) -> DispatchResult {
modifiedruntime_types.jsondiffbeforeafterboth
before · runtime_types.json
1{2    "AccessMode": {3      "_enum": [4        "Normal",5        "WhiteList"6      ]7    },8    "DecimalPoints": "u8",9    "CollectionMode": {10      "_enum": {11        "Invalid": null,12        "NFT": null,13        "Fungible": "DecimalPoints",14        "ReFungible": null15      }16    },17    "Ownership": {18      "Owner": "AccountId",19      "Fraction": "u128"20    },21    "FungibleItemType": {22      "Value": "u128"23    },24    "NftItemType": {25      "Owner": "AccountId",26      "ConstData": "Vec<u8>",27      "VariableData": "Vec<u8>"28    },29    "ReFungibleItemType": {30      "Owner": "Vec<Ownership<AccountId>>",31      "ConstData": "Vec<u8>",32      "VariableData": "Vec<u8>"33    },34    "Collection": {35      "Id": "CollectionId",36      "Owner": "AccountId",37      "Mode": "CollectionMode",38      "Access": "AccessMode",39      "DecimalPoints": "DecimalPoints",40      "Name": "Vec<u16>",41      "Description": "Vec<u16>",42      "TokenPrefix": "Vec<u8>",43      "MintMode": "bool",44      "OffchainSchema": "Vec<u8>",45      "SchemaVersion": "SchemaVersion",46      "Sponsor": "AccountId",47      "SponsorConfirmed": "bool",48      "Limits": "CollectionLimits",49      "VariableOnChainSchema": "Vec<u8>",50      "ConstOnChainSchema": "Vec<u8>"51    },52    "RawData": "Vec<u8>",53    "Address": "AccountId",54    "LookupSource": "AccountId",55    "Weight": "u64",56    "CreateNftData": {57      "const_data": "Vec<u8>",58      "variable_data": "Vec<u8>" 59    },60    "CreateFungibleData": {61      "value": "u128"62    },63    "CreateReFungibleData": {64      "const_data": "Vec<u8>",65      "variable_data": "Vec<u8>",66      "pieces": "u128"67    },68    "CreateItemData": {69      "_enum": {70        "NFT": "CreateNftData",71        "Fungible": "CreateFungibleData",72        "ReFungible": "CreateReFungibleData"73      }74    },75    "SchemaVersion": {76      "_enum": [77        "ImageURL",78        "Unique"79      ]80    },81    "CollectionId": "u32",82    "TokenId": "u32",83    "ChainLimits": {84      "CollectionNumbersLimit": "u32",85      "AccountTokenOwnershipLimit": "u32",86      "CollectionAdminsLimit": "u64",87      "CustomDataLimit": "u32",88      "NftSponsorTimeout": "u32",89      "FungibleSponsorTimeout": "u32",90      "RefungibleSponsorTimeout": "u32",91      "OffchainSchemaLimit": "u32",92      "VariableOnChainSchemaLimit": "u32",93      "ConstOnChainSchemaLimit": "u32"94    },95    "CollectionLimits": {96      "AccountTokenOwnershipLimit": "u32",97      "SponsoredDataSize": "u32",98      "SponsoredDataRateLimit": "Option<BlockNumber>",99      "TokenLimit": "u32",100      "SponsorTimeout": "u32",101      "OwnerCanTransfer": "bool",102      "OwnerCanDestroy": "bool"103    }104}
after · runtime_types.json
1{2    "AccessMode": {3      "_enum": [4        "Normal",5        "WhiteList"6      ]7    },8    "DecimalPoints": "u8",9    "CollectionMode": {10      "_enum": {11        "Invalid": null,12        "NFT": null,13        "Fungible": "DecimalPoints",14        "ReFungible": null15      }16    },17    "Ownership": {18      "Owner": "AccountId",19      "Fraction": "u128"20    },21    "FungibleItemType": {22      "Value": "u128"23    },24    "NftItemType": {25      "Owner": "AccountId",26      "ConstData": "Vec<u8>",27      "VariableData": "Vec<u8>"28    },29    "ReFungibleItemType": {30      "Owner": "Vec<Ownership<AccountId>>",31      "ConstData": "Vec<u8>",32      "VariableData": "Vec<u8>"33    },34    "Collection": {35      "Owner": "AccountId",36      "Mode": "CollectionMode",37      "Access": "AccessMode",38      "DecimalPoints": "DecimalPoints",39      "Name": "Vec<u16>",40      "Description": "Vec<u16>",41      "TokenPrefix": "Vec<u8>",42      "MintMode": "bool",43      "OffchainSchema": "Vec<u8>",44      "SchemaVersion": "SchemaVersion",45      "Sponsor": "AccountId",46      "SponsorConfirmed": "bool",47      "Limits": "CollectionLimits",48      "VariableOnChainSchema": "Vec<u8>",49      "ConstOnChainSchema": "Vec<u8>"50    },51    "RawData": "Vec<u8>",52    "Address": "AccountId",53    "LookupSource": "AccountId",54    "Weight": "u64",55    "CreateNftData": {56      "const_data": "Vec<u8>",57      "variable_data": "Vec<u8>" 58    },59    "CreateFungibleData": {60      "value": "u128"61    },62    "CreateReFungibleData": {63      "const_data": "Vec<u8>",64      "variable_data": "Vec<u8>",65      "pieces": "u128"66    },67    "CreateItemData": {68      "_enum": {69        "NFT": "CreateNftData",70        "Fungible": "CreateFungibleData",71        "ReFungible": "CreateReFungibleData"72      }73    },74    "SchemaVersion": {75      "_enum": [76        "ImageURL",77        "Unique"78      ]79    },80    "CollectionId": "u32",81    "TokenId": "u32",82    "ChainLimits": {83      "CollectionNumbersLimit": "u32",84      "AccountTokenOwnershipLimit": "u32",85      "CollectionAdminsLimit": "u64",86      "CustomDataLimit": "u32",87      "NftSponsorTimeout": "u32",88      "FungibleSponsorTimeout": "u32",89      "RefungibleSponsorTimeout": "u32",90      "OffchainSchemaLimit": "u32",91      "VariableOnChainSchemaLimit": "u32",92      "ConstOnChainSchemaLimit": "u32"93    },94    "CollectionLimits": {95      "AccountTokenOwnershipLimit": "u32",96      "SponsoredDataSize": "u32",97      "SponsoredDataRateLimit": "Option<BlockNumber>",98      "TokenLimit": "u32",99      "SponsorTimeout": "u32",100      "OwnerCanTransfer": "bool",101      "OwnerCanDestroy": "bool"102    }103}