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

difftreelog

Merge commit 'b3fda41c65ef3d5198749d60622f0184af244734' into feature/NFTPAR-366_upstream_updates

Yaroslav Bolyukin2021-05-06parents: #cf66146 #b3fda41.patch.diff
in: master

3 files changed

modifiedpallets/nft/src/eth/mod.rsdiffbeforeafterboth
--- a/pallets/nft/src/eth/mod.rs
+++ b/pallets/nft/src/eth/mod.rs
@@ -117,8 +117,8 @@
 			let recipient = T::CrossAccountId::from_eth(recipient);
 
 			<Module<T>>::transfer_internal(
-				sender,
-				recipient,
+				&sender,
+				&recipient,
 				&collection,
 				1,
 				amount,
@@ -143,8 +143,8 @@
 			let spender = T::CrossAccountId::from_eth(spender);
 
 			<Module<T>>::approve_internal(
-				sender,
-				spender,
+				&sender,
+				&spender,
 				&collection,
 				1,
 				amount,
@@ -160,8 +160,8 @@
 			let token_id = token_id.try_into().map_err(|_| "bad token id")?;
 
 			<Module<T>>::approve_internal(
-				sender,
-				approved,
+				&sender,
+				&approved,
 				&collection,
 				token_id,
 				1,
@@ -176,9 +176,9 @@
 			let recipient = T::CrossAccountId::from_eth(recipient);
 
 			<Module<T>>::transfer_from_internal(
-				sender,
-				from,
-				recipient,
+				&sender,
+				&from,
+				&recipient,
 				&collection,
 				1,
 				amount,
@@ -195,9 +195,9 @@
 			let token_id = token_id.try_into().map_err(|_| "bad token id")?;
 
 			<Module<T>>::transfer_from_internal(
-				sender,
-				from,
-				recipient,
+				&sender,
+				&from,
+				&recipient,
 				&collection,
 				token_id,
 				1,
modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
--- a/pallets/nft/src/lib.rs
+++ b/pallets/nft/src/lib.rs
@@ -870,10 +870,14 @@
 
             let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
             let collection = Self::get_collection(collection_id)?;
-            Self::check_owner_or_admin_permissions(&collection, &sender)?;
 
-            <WhiteList<T>>::insert(collection_id, address.as_sub(), true);
-            
+            Self::toggle_white_list_internal(
+                &sender,
+                &collection,
+                &address,
+                true,
+            )?;
+
             Ok(())
         }
 
@@ -895,9 +899,13 @@
 
             let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
             let collection = Self::get_collection(collection_id)?;
-            Self::check_owner_or_admin_permissions(&collection, &sender)?;
 
-            <WhiteList<T>>::remove(collection_id, address.as_sub());
+            Self::toggle_white_list_internal(
+                &sender,
+                &collection,
+                &address,
+                false,
+            )?;
 
             Ok(())
         }
@@ -1137,16 +1145,12 @@
         #[weight = <T as Config>::WeightInfo::create_item(data.len())]
         #[transactional]
         pub fn create_item(origin, collection_id: CollectionId, owner: T::CrossAccountId, data: CreateItemData) -> DispatchResult {
-
             let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
+            let collection = Self::get_collection(collection_id)?;
 
-            let target_collection = Self::get_collection(collection_id)?;
+            Self::create_item_internal(&sender, &collection, &owner, data);
 
-            Self::can_create_items_in_collection(&target_collection, &sender, &owner, 1)?;
-            Self::validate_create_item_args(&target_collection, &data)?;
-            Self::create_item_no_validation(&target_collection, owner, data)?;
-
-            Self::submit_logs(target_collection)?;
+            Self::submit_logs(collection)?;
             Ok(())
         }
 
@@ -1178,7 +1182,7 @@
             let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
             let collection = Self::get_collection(collection_id)?;
 
-            Self::create_multiple_items_internal(sender, &collection, owner, items_data)?;
+            Self::create_multiple_items_internal(&sender, &collection, &owner, items_data)?;
 
             Self::submit_logs(collection)?;
             Ok(())
@@ -1239,7 +1243,7 @@
             let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
             let collection = Self::get_collection(collection_id)?;
 
-            Self::transfer_internal(sender, recipient, &collection, item_id, value)?;
+            Self::transfer_internal(&sender, &recipient, &collection, item_id, value)?;
 
             Self::submit_logs(collection)?;
             Ok(())
@@ -1263,11 +1267,10 @@
         #[weight = <T as Config>::WeightInfo::approve()]
         #[transactional]
         pub fn approve(origin, spender: T::CrossAccountId, collection_id: CollectionId, item_id: TokenId, amount: u128) -> DispatchResult {
-
             let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
             let collection = Self::get_collection(collection_id)?;
 
-            Self::approve_internal(sender, spender, &collection, item_id, amount)?;
+            Self::approve_internal(&sender, &spender, &collection, item_id, amount)?;
 
             Self::submit_logs(collection)?;
             Ok(())
@@ -1295,19 +1298,15 @@
         #[weight = <T as Config>::WeightInfo::transfer_from()]
         #[transactional]
         pub fn transfer_from(origin, from: T::CrossAccountId, recipient: T::CrossAccountId, collection_id: CollectionId, item_id: TokenId, value: u128 ) -> DispatchResult {
-
             let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
             let collection = Self::get_collection(collection_id)?;
 
-            Self::transfer_from_internal(sender, from, recipient, &collection, item_id, value)?;
+            Self::transfer_from_internal(&sender, &from, &recipient, &collection, item_id, value)?;
 
             Self::submit_logs(collection)?;
             Ok(())
         }
-
         // #[weight = 0]
-        // pub fn safe_transfer_from(origin, collection_id: CollectionId, item_id: TokenId, new_owner: T::AccountId) -> DispatchResult {
-
         //     // let no_perm_mes = "You do not have permissions to modify this collection";
         //     // ensure!(<ApprovedList<T>>::contains_key((collection_id, item_id)), no_perm_mes);
         //     // let list_itm = <ApprovedList<T>>::get((collection_id, item_id));
@@ -1342,8 +1341,9 @@
         ) -> DispatchResult {
             let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
             
-            let target_collection = Self::get_collection(collection_id)?;
-            Self::set_variable_meta_data_internal(sender, &target_collection, item_id, data)?;
+            let collection = Self::get_collection(collection_id)?;
+
+            Self::set_variable_meta_data_internal(&sender, &collection, item_id, data)?;
 
             Ok(())
         }
@@ -1679,8 +1679,15 @@
 }
 
 impl<T: Config> Module<T> {
+    pub fn create_item_internal(sender: &T::CrossAccountId, collection: &CollectionHandle<T>, owner: &T::CrossAccountId, data: CreateItemData) -> DispatchResult {
+        Self::can_create_items_in_collection(&collection, &sender, &owner, 1)?;
+        Self::validate_create_item_args(&collection, &data)?;
+        Self::create_item_no_validation(&collection, owner, data)?;
+
+        Ok(())
+    }
 
-    pub fn transfer_internal(sender: T::CrossAccountId, recipient: T::CrossAccountId, target_collection: &CollectionHandle<T>, item_id: TokenId, value: u128) -> DispatchResult {
+    pub fn transfer_internal(sender: &T::CrossAccountId, recipient: &T::CrossAccountId, target_collection: &CollectionHandle<T>, item_id: TokenId, value: u128) -> DispatchResult {
         // Limits check
         Self::is_correct_transfer(target_collection, &recipient)?;
 
@@ -1702,14 +1709,14 @@
             _ => ()
         };
 
-        Self::deposit_event(RawEvent::Transfer(target_collection.id, item_id, sender, recipient, value));
+        Self::deposit_event(RawEvent::Transfer(target_collection.id, item_id, sender.clone(), recipient.clone(), value));
 
         Ok(())
     }
 
 	pub fn approve_internal(
-		sender: T::CrossAccountId,
-		spender: T::CrossAccountId,
+		sender: &T::CrossAccountId,
+		spender: &T::CrossAccountId,
 		collection: &CollectionHandle<T>,
 		item_id: TokenId,
 		amount: u128
@@ -1772,14 +1779,14 @@
 			);
 		}
 
-		Self::deposit_event(RawEvent::Approved(collection.id, item_id, sender, spender, allowance));
+		Self::deposit_event(RawEvent::Approved(collection.id, item_id, sender.clone(), spender.clone(), allowance));
 		Ok(())
 	}
 
 	pub fn transfer_from_internal(
-		sender: T::CrossAccountId,
-		from: T::CrossAccountId,
-		recipient: T::CrossAccountId,
+		sender: &T::CrossAccountId,
+		from: &T::CrossAccountId,
+		recipient: &T::CrossAccountId,
 		collection: &CollectionHandle<T>,
 		item_id: TokenId,
 		amount: u128,
@@ -1841,7 +1848,7 @@
 	}
 
     pub fn set_variable_meta_data_internal(
-        sender: T::CrossAccountId,
+        sender: &T::CrossAccountId,
         collection: &CollectionHandle<T>, 
         item_id: TokenId,
         data: Vec<u8>,
@@ -1867,9 +1874,9 @@
     }
 
     pub fn create_multiple_items_internal(
-        sender: T::CrossAccountId,
+        sender: &T::CrossAccountId,
         collection: &CollectionHandle<T>,
-        owner: T::CrossAccountId,
+        owner: &T::CrossAccountId,
         items_data: Vec<CreateItemData>,
     ) -> DispatchResult {
         Self::can_create_items_in_collection(&collection, &sender, &owner, items_data.len() as u32)?;
@@ -1878,7 +1885,7 @@
             Self::validate_create_item_args(&collection, data)?;
         }
         for data in &items_data {
-            Self::create_item_no_validation(&collection, owner.clone(), data.clone())?;
+            Self::create_item_no_validation(&collection, owner, data.clone())?;
         }
 
         Ok(())
@@ -1914,6 +1921,23 @@
         Ok(())
     }
 
+    pub fn toggle_white_list_internal(
+        sender: &T::CrossAccountId,
+        collection: &CollectionHandle<T>,
+        address: &T::CrossAccountId,
+        whitelisted: bool,
+    ) -> DispatchResult {
+        Self::check_owner_or_admin_permissions(&collection, &sender)?;
+
+        if whitelisted {
+            <WhiteList<T>>::insert(collection.id, address.as_sub(), true);
+        } else {
+            <WhiteList<T>>::remove(collection.id, address.as_sub());
+        }
+
+        Ok(())
+    }
+
     fn is_correct_transfer(collection: &CollectionHandle<T>, recipient: &T::CrossAccountId) -> DispatchResult {
         let collection_id = collection.id;
 
@@ -1984,7 +2008,7 @@
         Ok(())
     }
 
-    fn create_item_no_validation(collection: &CollectionHandle<T>, owner: T::CrossAccountId, data: CreateItemData) -> DispatchResult {
+    fn create_item_no_validation(collection: &CollectionHandle<T>, owner: &T::CrossAccountId, data: CreateItemData) -> DispatchResult {
         match data
         {
             CreateItemData::NFT(data) => {
modifiedruntime/src/chain_extension.rsdiffbeforeafterboth
before · runtime/src/chain_extension.rs
1//! NFT Chain Extension23//4// This file is subject to the terms and conditions defined in5// file 'LICENSE', which is part of this source code package.6//78use codec::{Decode, Encode};910pub use pallet_contracts::chain_extension::RetVal;11use pallet_contracts::chain_extension::{12    ChainExtension, Environment, Ext, InitState, SysConfig, UncheckedFrom,13};1415pub use frame_support::debug;16use frame_support::dispatch::DispatchError;1718extern crate pallet_nft;19pub use pallet_nft::*;20use crate::Runtime;21use sp_runtime::AccountId32;22use crate::Vec;2324/// Transfer parameters25#[derive(Debug, PartialEq, Encode, Decode)]26pub struct NFTExtTransfer<E: Ext> {27    pub recipient: <E::T as SysConfig>::AccountId,28    pub collection_id: u32,29    pub token_id: u32,30    pub amount: u128,31}3233/// The chain Extension of NFT pallet34pub struct NFTExtension;3536impl<C: Config> ChainExtension<C> for NFTExtension {37    fn call<E: Ext>(func_id: u32, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>38    where39        <E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,40    {41        // The memory of the vm stores buf in scale-codec42        match func_id {43            0 => {44                let mut env = env.buf_in_buf_out();45                let input: NFTExtTransfer<E> = env.read_as()?;4647                // Sender to AccountId3248                let mut bytes_sender: [u8; 32] = [0; 32];49                let addr_vec_sender: Vec<u8> = env.ext().caller().encode();50                for i in 0..32 {51                    bytes_sender[i] = addr_vec_sender[i];52                }53                let sender = AccountId32::from(bytes_sender);5455                // Recipient to AccountId3256                let mut bytes_rec: [u8; 32] = [0; 32];57                let addr_vec_rec: Vec<u8> = input.recipient.encode();58                for i in 0..32 {59                    bytes_rec[i] = addr_vec_rec[i];60                }61                let recipient = AccountId32::from(bytes_rec);6263                let collection = pallet_nft::Module::<Runtime>::get_collection(input.collection_id)?;6465                match pallet_nft::Module::<Runtime>::transfer_internal(66                    <Runtime as Config>::CrossAccountId::from_sub(sender),67                    <Runtime as Config>::CrossAccountId::from_sub(recipient),68                    &collection,69                    input.token_id,70                    input.amount,71                ) {72                    Ok(_) => Ok(RetVal::Converging(func_id)),73                    _ => Err(DispatchError::Other("Transfer error"))74                }75            },76			_ => {77				panic!("Passed unknown func_id to test chain extension: {}", func_id);78            }79        }80    }81}
after · runtime/src/chain_extension.rs
1//! NFT Chain Extension23//4// This file is subject to the terms and conditions defined in5// file 'LICENSE', which is part of this source code package.6//78use codec::{Decode, Encode};910pub use pallet_contracts::chain_extension::RetVal;11use pallet_contracts::chain_extension::{12    ChainExtension, Environment, Ext, InitState, SysConfig, UncheckedFrom,13};1415pub use frame_support::debug;16use frame_support::dispatch::DispatchError;1718extern crate pallet_nft;19pub use pallet_nft::*;20use pallet_nft::CrossAccountId;21use crate::Vec;2223/// Create item parameters24#[derive(Debug, PartialEq, Encode, Decode)]25pub struct NFTExtCreateItem<E: Ext> {26    pub owner: <E::T as SysConfig>::AccountId,27    pub collection_id: u32,28    pub data: CreateItemData,29}3031/// Transfer parameters32#[derive(Debug, PartialEq, Encode, Decode)]33pub struct NFTExtTransfer<E: Ext> {34    pub recipient: <E::T as SysConfig>::AccountId,35    pub collection_id: u32,36    pub token_id: u32,37    pub amount: u128,38}3940#[derive(Debug, PartialEq, Encode, Decode)]41pub struct NFTExtCreateMultipleItems<E: Ext> {42    pub owner: <E::T as SysConfig>::AccountId,43    pub collection_id: u32,44    pub data: Vec<CreateItemData>,45}4647#[derive(Debug, PartialEq, Encode, Decode)]48pub struct NFTExtApprove<E: Ext> {49    pub spender: <E::T as SysConfig>::AccountId,50    pub collection_id: u32,51    pub item_id: u32,52    pub amount: u128,53}5455#[derive(Debug, PartialEq, Encode, Decode)]56pub struct NFTExtTransferFrom<E: Ext> {57    pub owner: <E::T as SysConfig>::AccountId,58    pub recipient: <E::T as SysConfig>::AccountId,59    pub collection_id: u32,60    pub item_id: u32,61    pub amount: u128,62}6364#[derive(Debug, PartialEq, Encode, Decode)]65pub struct NFTExtSetVariableMetaData {66    pub collection_id: u32,67    pub item_id: u32,68    pub data: Vec<u8>,   69}7071#[derive(Debug, PartialEq, Encode, Decode)]72pub struct NFTExtToggleWhiteList<E: Ext> {73    pub collection_id: u32,74    pub address: <E::T as SysConfig>::AccountId,75    pub whitelisted: bool,76}7778/// The chain Extension of NFT pallet79pub struct NFTExtension;8081impl<C: Config> ChainExtension<C> for NFTExtension {82    fn call<E: Ext>(func_id: u32, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>83    where84        E: Ext<T = C>,85        <E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,86    {87        // The memory of the vm stores buf in scale-codec88        match func_id {89            0 => {90                let mut env = env.buf_in_buf_out();91                let input: NFTExtTransfer<E> = env.read_as()?;9293                let collection = pallet_nft::Module::<C>::get_collection(input.collection_id)?;9495                match pallet_nft::Module::<C>::transfer_internal(96                    &C::CrossAccountId::from_sub(env.ext().caller().clone()),97                    &C::CrossAccountId::from_sub(input.recipient),98                    &collection,99                    input.token_id,100                    input.amount,101                ) {102                    Ok(_) => Ok(RetVal::Converging(func_id)),103                    _ => Err(DispatchError::Other("Transfer error"))104                }105            },106            1 => {107                // Create Item108                let mut env = env.buf_in_buf_out();109                let input: NFTExtCreateItem<E> = env.read_as()?;110111                let collection = pallet_nft::Module::<C>::get_collection(input.collection_id)?;112113                match pallet_nft::Module::<C>::create_item_internal(114                    &C::CrossAccountId::from_sub(env.ext().address().clone()),115                    &collection,116                    &C::CrossAccountId::from_sub(input.owner),117                    input.data,118                ) {119                    Ok(_) => Ok(RetVal::Converging(func_id)),120                    _ => Err(DispatchError::Other("CreateItem error"))121                }122            },123            2 => {124                // Create multiple items125                let mut env = env.buf_in_buf_out();126                let input: NFTExtCreateMultipleItems<E> = env.read_as()?;127128                let collection = pallet_nft::Module::<C>::get_collection(input.collection_id)?;129130                match pallet_nft::Module::<C>::create_multiple_items_internal(131                    &C::CrossAccountId::from_sub(env.ext().address().clone()),132                    &collection,133                    &C::CrossAccountId::from_sub(input.owner),134                    input.data,135                ) {136                    Ok(_) => Ok(RetVal::Converging(func_id)),137                    _ => Err(DispatchError::Other("CreateMultipleItems error"))138                }139            },140            3 => {141                // Approve142                let mut env = env.buf_in_buf_out();143                let input: NFTExtApprove<E> = env.read_as()?;144145                let collection = pallet_nft::Module::<C>::get_collection(input.collection_id)?;146147                pallet_nft::Module::<C>::approve_internal(148                    &C::CrossAccountId::from_sub(env.ext().address().clone()),149                    &C::CrossAccountId::from_sub(input.spender),150                    &collection,151                    input.item_id,152                    input.amount,153                )?;154                Ok(RetVal::Converging(func_id))155            },156            4 => {157                // Transfer from158                let mut env = env.buf_in_buf_out();159                let input: NFTExtTransferFrom<E> = env.read_as()?;160161                let collection = pallet_nft::Module::<C>::get_collection(input.collection_id)?;162163                pallet_nft::Module::<C>::transfer_from_internal(164                    &C::CrossAccountId::from_sub(env.ext().address().clone()),165                    &C::CrossAccountId::from_sub(input.owner),166                    &C::CrossAccountId::from_sub(input.recipient),167                    &collection,168                    input.item_id,169                    input.amount170                )?;171                Ok(RetVal::Converging(func_id))172            },173            5 => {174                // Set variable metadata175                let mut env = env.buf_in_buf_out();176                let input: NFTExtSetVariableMetaData = env.read_as()?;177178                let collection = pallet_nft::Module::<C>::get_collection(input.collection_id)?;179180                pallet_nft::Module::<C>::set_variable_meta_data_internal(181                    &C::CrossAccountId::from_sub(env.ext().address().clone()),182                    &collection,183                    input.item_id,184                    input.data,185                )?;186                Ok(RetVal::Converging(func_id))187            },188            6 => {189                // Toggle whitelist190                let mut env = env.buf_in_buf_out();191                let input: NFTExtToggleWhiteList<E> = env.read_as()?;192193                let collection = pallet_nft::Module::<C>::get_collection(input.collection_id)?;194195                pallet_nft::Module::<C>::toggle_white_list_internal(196                    &C::CrossAccountId::from_sub(env.ext().address().clone()),197                    &collection,198                    &C::CrossAccountId::from_sub(input.address),199                    input.whitelisted,200                )?;201                Ok(RetVal::Converging(func_id))202            }203            _ => {204                panic!("Passed unknown func_id to test chain extension: {}", func_id);205            }206        }207    }208}