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

difftreelog

feat delegate erc call to other struct

Yaroslav Bolyukin2022-05-12parent: #187efe5.patch.diff
in: master

9 files changed

modifiedcrates/evm-coder-macros/src/solidity_interface.rsdiffbeforeafterboth
--- a/crates/evm-coder-macros/src/solidity_interface.rs
+++ b/crates/evm-coder-macros/src/solidity_interface.rs
@@ -23,6 +23,7 @@
 use syn::{
 	Expr, FnArg, GenericArgument, Generics, Ident, ImplItem, ImplItemMethod, ItemImpl, Lit, Meta,
 	MetaNameValue, NestedMeta, PatType, Path, PathArguments, ReturnType, Type, spanned::Spanned,
+	parse_str,
 };
 
 use crate::{
@@ -35,16 +36,21 @@
 	name: Ident,
 	pascal_call_name: Ident,
 	snake_call_name: Ident,
+	via: Option<(Type, Ident)>,
 }
 impl Is {
-	fn try_from(path: &Path) -> syn::Result<Self> {
+	fn new_via(path: &Path, via: Option<(Type, Ident)>) -> syn::Result<Self> {
 		let name = parse_ident_from_path(path, false)?.clone();
 		Ok(Self {
 			pascal_call_name: pascal_ident_to_call(&name),
 			snake_call_name: pascal_ident_to_snake_call(&name),
 			name,
+			via,
 		})
 	}
+	fn new(path: &Path) -> syn::Result<Self> {
+		Self::new_via(path, None)
+	}
 
 	fn expand_call_def(&self, gen_ref: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {
 		let name = &self.name;
@@ -85,8 +91,18 @@
 	) -> proc_macro2::TokenStream {
 		let name = &self.name;
 		let pascal_call_name = &self.pascal_call_name;
+		let via_typ = self
+			.via
+			.as_ref()
+			.map(|(t, _)| quote! {#t})
+			.unwrap_or_else(|| quote! {Self});
+		let via_map = self
+			.via
+			.as_ref()
+			.map(|(_, i)| quote! {.#i()})
+			.unwrap_or_default();
 		quote! {
-			#call_name::#name(call) => return <Self as ::evm_coder::Callable<#pascal_call_name #generics>>::call(self, Msg {
+			#call_name::#name(call) => return <#via_typ as ::evm_coder::Callable<#pascal_call_name #generics>>::call(self #via_map, Msg {
 				call,
 				caller: c.caller,
 				value: c.value,
@@ -126,8 +142,46 @@
 		let mut out = Vec::new();
 		for item in items {
 			match item {
-				NestedMeta::Meta(Meta::Path(path)) => out.push(Is::try_from(path)?),
-				_ => return Err(syn::Error::new(item.span(), "expected path").into()),
+				NestedMeta::Meta(Meta::Path(path)) => out.push(Is::new(path)?),
+				// TODO: replace meta parsing with manual
+				NestedMeta::Meta(Meta::List(list))
+					if list.path.is_ident("via") && list.nested.len() == 3 =>
+				{
+					let mut data = list.nested.iter();
+					let typ = match data.next().expect("len == 3") {
+						NestedMeta::Lit(Lit::Str(s)) => {
+							let v = s.value();
+							let typ: Type = parse_str(&v)?;
+							typ
+						}
+						_ => {
+							return Err(syn::Error::new(
+								item.span(),
+								"via typ should be type in string",
+							)
+							.into())
+						}
+					};
+					let via = match data.next().expect("len == 3") {
+						NestedMeta::Meta(Meta::Path(path)) => path
+							.get_ident()
+							.ok_or_else(|| syn::Error::new(item.span(), "via should be ident"))?,
+						_ => return Err(syn::Error::new(item.span(), "via should be ident").into()),
+					};
+					let path = match data.next().expect("len == 3") {
+						NestedMeta::Meta(Meta::Path(path)) => path,
+						_ => return Err(syn::Error::new(item.span(), "path should be path").into()),
+					};
+
+					out.push(Is::new_via(path, Some((typ, via.clone())))?)
+				}
+				_ => {
+					return Err(syn::Error::new(
+						item.span(),
+						"expected either Name or via(\"Type\", getter, Name)",
+					)
+					.into())
+				}
 			}
 		}
 		Ok(Self(out))
modifiedpallets/common/src/erc.rsdiffbeforeafterboth
--- a/pallets/common/src/erc.rs
+++ b/pallets/common/src/erc.rs
@@ -14,9 +14,14 @@
 // You should have received a copy of the GNU General Public License
 // along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
 
-pub use pallet_evm::PrecompileOutput;
-pub use pallet_evm::PrecompileResult;
+use evm_coder::{solidity_interface, types::*, execution::Result};
+pub use pallet_evm::{PrecompileOutput, PrecompileResult, account::CrossAccountId};
+use pallet_evm_coder_substrate::dispatch_to_evm;
 use sp_core::{H160, U256};
+use sp_std::vec::Vec;
+use up_data_structs::Property;
+
+use crate::{Pallet, CollectionHandle, Config};
 
 /// Does not always represent a full collection, for RFT it is either
 /// collection (Implementing ERC721), or specific collection token (Implementing ERC20)
@@ -25,3 +30,27 @@
 
 	fn call(self, source: &H160, input: &[u8], value: U256) -> Option<PrecompileResult>;
 }
+
+#[solidity_interface(name = "CollectionProperties")]
+impl<T: Config> CollectionHandle<T> {
+	fn set_property(&mut self, caller: caller, key: string, value: string) -> Result<()> {
+		<Pallet<T>>::set_collection_property(
+			self,
+			&T::CrossAccountId::from_eth(caller),
+			Property {
+				key: <Vec<u8>>::from(key)
+					.try_into()
+					.map_err(|_| "key too large")?,
+				value: <Vec<u8>>::from(value)
+					.try_into()
+					.map_err(|_| "value too large")?,
+			},
+		)
+		.map_err(dispatch_to_evm::<T>)?;
+		Ok(())
+	}
+
+	fn delete_property(&mut self, caller: caller, key: string) -> Result<()> {
+		self.set_property(caller, key, string::new())
+	}
+}
modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -595,7 +595,7 @@
 			.iter()
 			.map(|(key, value)| Property {
 				key: key.clone(),
-				value: value.clone()
+				value: value.clone(),
 			})
 			.collect();
 
@@ -680,19 +680,20 @@
 		};
 
 		let mut collection_properties = up_data_structs::CollectionProperties::get();
-		collection_properties.try_set_from_iter(
-			data.properties.into_iter()
-				.map(|p| (p.key, p.value))
-		).map_err(|e| -> Error<T> { e.into() })?;
+		collection_properties
+			.try_set_from_iter(data.properties.into_iter().map(|p| (p.key, p.value)))
+			.map_err(|e| -> Error<T> { e.into() })?;
 
 		CollectionProperties::<T>::insert(id, collection_properties);
 
 		let mut token_props_permissions = PropertiesPermissionMap::new();
-		token_props_permissions.try_set_from_iter(
-			data.token_property_permissions
-			.into_iter()
-			.map(|property| (property.key, property.permission))
-		).map_err(|e| -> Error<T> { e.into() })?;
+		token_props_permissions
+			.try_set_from_iter(
+				data.token_property_permissions
+					.into_iter()
+					.map(|property| (property.key, property.permission)),
+			)
+			.map_err(|e| -> Error<T> { e.into() })?;
 
 		CollectionPropertyPermissions::<T>::insert(id, token_props_permissions);
 
@@ -806,7 +807,8 @@
 
 		CollectionProperties::<T>::try_mutate(collection.id, |properties| {
 			properties.remove(&property_key)
-		}).map_err(|e| -> Error<T> { e.into() })?;
+		})
+		.map_err(|e| -> Error<T> { e.into() })?;
 
 		Self::deposit_event(Event::CollectionPropertyDeleted(
 			collection.id,
@@ -903,11 +905,10 @@
 		let properties = keys
 			.into_iter()
 			.filter_map(|key| {
-				properties.get(&key)
-					.map(|value| Property {
-						key,
-						value: value.clone(),
-					})
+				properties.get(&key).map(|value| Property {
+					key,
+					value: value.clone(),
+				})
 			})
 			.collect();
 
modifiedpallets/fungible/src/erc.rsdiffbeforeafterboth
--- a/pallets/fungible/src/erc.rs
+++ b/pallets/fungible/src/erc.rs
@@ -24,6 +24,7 @@
 use pallet_evm::account::CrossAccountId;
 use pallet_evm_coder_substrate::{call, dispatch_to_evm};
 use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};
+use pallet_common::{CollectionHandle, erc::CollectionPropertiesCall};
 
 use crate::{
 	Allowance, Balance, Config, FungibleHandle, Pallet, SelfWeightOf, TotalSupply,
@@ -144,7 +145,14 @@
 	}
 }
 
-#[solidity_interface(name = "UniqueFungible", is(ERC20))]
+#[solidity_interface(
+	name = "UniqueFungible",
+	is(
+		ERC20,
+		ERC20UniqueExtensions,
+		via("CollectionHandle<T>", common_mut, CollectionProperties)
+	)
+)]
 impl<T: Config> FungibleHandle<T> {}
 
 generate_stubgen!(gen_impl, UniqueFungibleCall<()>, true);
modifiedpallets/fungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/fungible/src/lib.rs
+++ b/pallets/fungible/src/lib.rs
@@ -110,6 +110,9 @@
 	pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {
 		self.0
 	}
+	pub fn common_mut(&mut self) -> &mut pallet_common::CollectionHandle<T> {
+		&mut self.0
+	}
 }
 impl<T: Config> WithRecorder<T> for FungibleHandle<T> {
 	fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {
modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
before · pallets/nonfungible/src/common.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use core::marker::PhantomData;1819use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight, BoundedVec};20use up_data_structs::{21	TokenId, CustomDataLimit, CreateItemExData, CollectionId, budget::Budget, Property,22	PropertyKey, PropertyKeyPermission,23};24use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};25use sp_runtime::DispatchError;26use sp_std::vec::Vec;2728use crate::{29	AccountBalance, Allowance, Config, CreateItemData, Error, NonfungibleHandle, Owned, Pallet,30	SelfWeightOf, TokenData, weights::WeightInfo, TokensMinted,31};3233pub struct CommonWeights<T: Config>(PhantomData<T>);34impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {35	fn create_item() -> Weight {36		<SelfWeightOf<T>>::create_item()37	}3839	fn create_multiple_items_ex(data: &CreateItemExData<T::CrossAccountId>) -> Weight {40		match data {41			CreateItemExData::NFT(t) => <SelfWeightOf<T>>::create_multiple_items_ex(t.len() as u32),42			_ => 0,43		}44	}4546	fn create_multiple_items(amount: u32) -> Weight {47		<SelfWeightOf<T>>::create_multiple_items(amount)48	}4950	fn burn_item() -> Weight {51		<SelfWeightOf<T>>::burn_item()52	}5354	fn set_collection_properties(amount: u32) -> Weight {55		<SelfWeightOf<T>>::set_collection_properties(amount)56	}5758	fn delete_collection_properties(amount: u32) -> Weight {59		<SelfWeightOf<T>>::delete_collection_properties(amount)60	}6162	fn set_token_properties(amount: u32) -> Weight {63		<SelfWeightOf<T>>::set_token_properties(amount)64	}6566	fn delete_token_properties(amount: u32) -> Weight {67		<SelfWeightOf<T>>::delete_token_properties(amount)68	}6970	fn set_property_permissions(amount: u32) -> Weight {71		<SelfWeightOf<T>>::set_property_permissions(amount)72	}7374	fn transfer() -> Weight {75		<SelfWeightOf<T>>::transfer()76	}7778	fn approve() -> Weight {79		<SelfWeightOf<T>>::approve()80	}8182	fn transfer_from() -> Weight {83		<SelfWeightOf<T>>::transfer_from()84	}8586	fn burn_from() -> Weight {87		<SelfWeightOf<T>>::burn_from()88	}8990	fn set_variable_metadata(bytes: u32) -> Weight {91		<SelfWeightOf<T>>::set_variable_metadata(bytes)92	}93}9495fn map_create_data<T: Config>(96	data: up_data_structs::CreateItemData,97	to: &T::CrossAccountId,98) -> Result<CreateItemData<T>, DispatchError> {99	match data {100		up_data_structs::CreateItemData::NFT(data) => Ok(CreateItemData::<T> {101			const_data: data.const_data,102			variable_data: data.variable_data,103			properties: data.properties,104			owner: to.clone(),105		}),106		_ => fail!(<Error<T>>::NotNonfungibleDataUsedToMintFungibleCollectionToken),107	}108}109110impl<T: Config> CommonCollectionOperations<T> for NonfungibleHandle<T> {111	fn create_item(112		&self,113		sender: T::CrossAccountId,114		to: T::CrossAccountId,115		data: up_data_structs::CreateItemData,116		nesting_budget: &dyn Budget,117	) -> DispatchResultWithPostInfo {118		with_weight(119			<Pallet<T>>::create_item(120				self,121				&sender,122				map_create_data::<T>(data, &to)?,123				nesting_budget,124			),125			<CommonWeights<T>>::create_item(),126		)127	}128129	fn create_multiple_items(130		&self,131		sender: T::CrossAccountId,132		to: T::CrossAccountId,133		data: Vec<up_data_structs::CreateItemData>,134		nesting_budget: &dyn Budget,135	) -> DispatchResultWithPostInfo {136		let data = data137			.into_iter()138			.map(|d| map_create_data::<T>(d, &to))139			.collect::<Result<Vec<_>, DispatchError>>()?;140141		let amount = data.len();142		with_weight(143			<Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),144			<CommonWeights<T>>::create_multiple_items(amount as u32),145		)146	}147148	fn create_multiple_items_ex(149		&self,150		sender: <T>::CrossAccountId,151		data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,152		nesting_budget: &dyn Budget,153	) -> DispatchResultWithPostInfo {154		let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);155		let data = match data {156			up_data_structs::CreateItemExData::NFT(nft) => nft,157			_ => fail!(Error::<T>::NotNonfungibleDataUsedToMintFungibleCollectionToken),158		};159160		with_weight(161			<Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),162			weight,163		)164	}165166	fn set_collection_properties(167		&self,168		sender: T::CrossAccountId,169		properties: Vec<Property>,170	) -> DispatchResultWithPostInfo {171		let weight = <CommonWeights<T>>::set_collection_properties(properties.len() as u32);172173		with_weight(174			<Pallet<T>>::set_collection_properties(self, &sender, properties),175			weight,176		)177	}178179	fn delete_collection_properties(180		&self,181		sender: &T::CrossAccountId,182		property_keys: Vec<PropertyKey>,183	) -> DispatchResultWithPostInfo {184		let weight = <CommonWeights<T>>::delete_collection_properties(property_keys.len() as u32);185186		with_weight(187			<Pallet<T>>::delete_collection_properties(self, &sender, property_keys),188			weight,189		)190	}191192	fn set_token_properties(193		&self,194		sender: T::CrossAccountId,195		token_id: TokenId,196		properties: Vec<Property>,197	) -> DispatchResultWithPostInfo {198		let weight = <CommonWeights<T>>::set_token_properties(properties.len() as u32);199200		with_weight(201			<Pallet<T>>::set_token_properties(self, &sender, token_id, properties),202			weight,203		)204	}205206	fn delete_token_properties(207		&self,208		sender: T::CrossAccountId,209		token_id: TokenId,210		property_keys: Vec<PropertyKey>,211	) -> DispatchResultWithPostInfo {212		let weight = <CommonWeights<T>>::delete_token_properties(property_keys.len() as u32);213214		with_weight(215			<Pallet<T>>::delete_token_properties(self, &sender, token_id, property_keys),216			weight,217		)218	}219220	fn set_property_permissions(221		&self,222		sender: &T::CrossAccountId,223		property_permissions: Vec<PropertyKeyPermission>,224	) -> DispatchResultWithPostInfo {225		let weight =226			<CommonWeights<T>>::set_property_permissions(property_permissions.len() as u32);227228		with_weight(229			<Pallet<T>>::set_property_permissions(self, sender, property_permissions),230			weight,231		)232	}233234	fn burn_item(235		&self,236		sender: T::CrossAccountId,237		token: TokenId,238		amount: u128,239	) -> DispatchResultWithPostInfo {240		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);241		if amount == 1 {242			with_weight(243				<Pallet<T>>::burn(self, &sender, token),244				<CommonWeights<T>>::burn_item(),245			)246		} else {247			Ok(().into())248		}249	}250251	fn transfer(252		&self,253		from: T::CrossAccountId,254		to: T::CrossAccountId,255		token: TokenId,256		amount: u128,257		nesting_budget: &dyn Budget,258	) -> DispatchResultWithPostInfo {259		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);260		if amount == 1 {261			with_weight(262				<Pallet<T>>::transfer(self, &from, &to, token, nesting_budget),263				<CommonWeights<T>>::transfer(),264			)265		} else {266			Ok(().into())267		}268	}269270	fn approve(271		&self,272		sender: T::CrossAccountId,273		spender: T::CrossAccountId,274		token: TokenId,275		amount: u128,276	) -> DispatchResultWithPostInfo {277		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);278279		with_weight(280			if amount == 1 {281				<Pallet<T>>::set_allowance(self, &sender, token, Some(&spender))282			} else {283				<Pallet<T>>::set_allowance(self, &sender, token, None)284			},285			<CommonWeights<T>>::approve(),286		)287	}288289	fn transfer_from(290		&self,291		sender: T::CrossAccountId,292		from: T::CrossAccountId,293		to: T::CrossAccountId,294		token: TokenId,295		amount: u128,296		nesting_budget: &dyn Budget,297	) -> DispatchResultWithPostInfo {298		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);299300		if amount == 1 {301			with_weight(302				<Pallet<T>>::transfer_from(self, &sender, &from, &to, token, nesting_budget),303				<CommonWeights<T>>::transfer_from(),304			)305		} else {306			Ok(().into())307		}308	}309310	fn burn_from(311		&self,312		sender: T::CrossAccountId,313		from: T::CrossAccountId,314		token: TokenId,315		amount: u128,316		nesting_budget: &dyn Budget,317	) -> DispatchResultWithPostInfo {318		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);319320		if amount == 1 {321			with_weight(322				<Pallet<T>>::burn_from(self, &sender, &from, token, nesting_budget),323				<CommonWeights<T>>::burn_from(),324			)325		} else {326			Ok(().into())327		}328	}329330	fn set_variable_metadata(331		&self,332		sender: T::CrossAccountId,333		token: TokenId,334		data: BoundedVec<u8, CustomDataLimit>,335	) -> DispatchResultWithPostInfo {336		let len = data.len();337		with_weight(338			<Pallet<T>>::set_variable_metadata(self, &sender, token, data),339			<CommonWeights<T>>::set_variable_metadata(len as u32),340		)341	}342343	fn check_nesting(344		&self,345		sender: T::CrossAccountId,346		from: (CollectionId, TokenId),347		under: TokenId,348		budget: &dyn Budget,349	) -> sp_runtime::DispatchResult {350		<Pallet<T>>::check_nesting(self, sender, from, under, budget)351	}352353	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {354		<Owned<T>>::iter_prefix((self.id, account))355			.map(|(id, _)| id)356			.collect()357	}358359	fn collection_tokens(&self) -> Vec<TokenId> {360		<TokenData<T>>::iter_prefix((self.id,))361			.map(|(id, _)| id)362			.collect()363	}364365	fn token_exists(&self, token: TokenId) -> bool {366		<Pallet<T>>::token_exists(self, token)367	}368369	fn last_token_id(&self) -> TokenId {370		TokenId(<TokensMinted<T>>::get(self.id))371	}372373	fn token_owner(&self, token: TokenId) -> Option<T::CrossAccountId> {374		<TokenData<T>>::get((self.id, token)).map(|t| t.owner)375	}376	fn const_metadata(&self, token: TokenId) -> Vec<u8> {377		<TokenData<T>>::get((self.id, token))378			.map(|t| t.const_data)379			.unwrap_or_default()380			.into_inner()381	}382	fn variable_metadata(&self, token: TokenId) -> Vec<u8> {383		<TokenData<T>>::get((self.id, token))384			.map(|t| t.variable_data)385			.unwrap_or_default()386			.into_inner()387	}388389	fn token_properties(&self, token_id: TokenId, keys: Vec<PropertyKey>) -> Vec<Property> {390		let properties = <Pallet<T>>::token_properties((self.id, token_id));391392		keys.into_iter()393			.filter_map(|key| {394				properties.get(&key)395					.map(|value| Property {396						key,397						value: value.clone(),398					})399			})400			.collect()401	}402403	fn total_supply(&self) -> u32 {404		<Pallet<T>>::total_supply(self)405	}406407	fn account_balance(&self, account: T::CrossAccountId) -> u32 {408		<AccountBalance<T>>::get((self.id, account))409	}410411	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {412		if <TokenData<T>>::get((self.id, token))413			.map(|a| a.owner == account)414			.unwrap_or(false)415		{416			1417		} else {418			0419		}420	}421422	fn allowance(423		&self,424		sender: T::CrossAccountId,425		spender: T::CrossAccountId,426		token: TokenId,427	) -> u128 {428		if <TokenData<T>>::get((self.id, token))429			.map(|a| a.owner != sender)430			.unwrap_or(true)431		{432			0433		} else if <Allowance<T>>::get((self.id, token)) == Some(spender) {434			1435		} else {436			0437		}438	}439}
after · pallets/nonfungible/src/common.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use core::marker::PhantomData;1819use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight, BoundedVec};20use up_data_structs::{21	TokenId, CustomDataLimit, CreateItemExData, CollectionId, budget::Budget, Property,22	PropertyKey, PropertyKeyPermission,23};24use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};25use sp_runtime::DispatchError;26use sp_std::vec::Vec;2728use crate::{29	AccountBalance, Allowance, Config, CreateItemData, Error, NonfungibleHandle, Owned, Pallet,30	SelfWeightOf, TokenData, weights::WeightInfo, TokensMinted,31};3233pub struct CommonWeights<T: Config>(PhantomData<T>);34impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {35	fn create_item() -> Weight {36		<SelfWeightOf<T>>::create_item()37	}3839	fn create_multiple_items_ex(data: &CreateItemExData<T::CrossAccountId>) -> Weight {40		match data {41			CreateItemExData::NFT(t) => <SelfWeightOf<T>>::create_multiple_items_ex(t.len() as u32),42			_ => 0,43		}44	}4546	fn create_multiple_items(amount: u32) -> Weight {47		<SelfWeightOf<T>>::create_multiple_items(amount)48	}4950	fn burn_item() -> Weight {51		<SelfWeightOf<T>>::burn_item()52	}5354	fn set_collection_properties(amount: u32) -> Weight {55		<SelfWeightOf<T>>::set_collection_properties(amount)56	}5758	fn delete_collection_properties(amount: u32) -> Weight {59		<SelfWeightOf<T>>::delete_collection_properties(amount)60	}6162	fn set_token_properties(amount: u32) -> Weight {63		<SelfWeightOf<T>>::set_token_properties(amount)64	}6566	fn delete_token_properties(amount: u32) -> Weight {67		<SelfWeightOf<T>>::delete_token_properties(amount)68	}6970	fn set_property_permissions(amount: u32) -> Weight {71		<SelfWeightOf<T>>::set_property_permissions(amount)72	}7374	fn transfer() -> Weight {75		<SelfWeightOf<T>>::transfer()76	}7778	fn approve() -> Weight {79		<SelfWeightOf<T>>::approve()80	}8182	fn transfer_from() -> Weight {83		<SelfWeightOf<T>>::transfer_from()84	}8586	fn burn_from() -> Weight {87		<SelfWeightOf<T>>::burn_from()88	}8990	fn set_variable_metadata(bytes: u32) -> Weight {91		<SelfWeightOf<T>>::set_variable_metadata(bytes)92	}93}9495fn map_create_data<T: Config>(96	data: up_data_structs::CreateItemData,97	to: &T::CrossAccountId,98) -> Result<CreateItemData<T>, DispatchError> {99	match data {100		up_data_structs::CreateItemData::NFT(data) => Ok(CreateItemData::<T> {101			const_data: data.const_data,102			variable_data: data.variable_data,103			properties: data.properties,104			owner: to.clone(),105		}),106		_ => fail!(<Error<T>>::NotNonfungibleDataUsedToMintFungibleCollectionToken),107	}108}109110impl<T: Config> CommonCollectionOperations<T> for NonfungibleHandle<T> {111	fn create_item(112		&self,113		sender: T::CrossAccountId,114		to: T::CrossAccountId,115		data: up_data_structs::CreateItemData,116		nesting_budget: &dyn Budget,117	) -> DispatchResultWithPostInfo {118		with_weight(119			<Pallet<T>>::create_item(120				self,121				&sender,122				map_create_data::<T>(data, &to)?,123				nesting_budget,124			),125			<CommonWeights<T>>::create_item(),126		)127	}128129	fn create_multiple_items(130		&self,131		sender: T::CrossAccountId,132		to: T::CrossAccountId,133		data: Vec<up_data_structs::CreateItemData>,134		nesting_budget: &dyn Budget,135	) -> DispatchResultWithPostInfo {136		let data = data137			.into_iter()138			.map(|d| map_create_data::<T>(d, &to))139			.collect::<Result<Vec<_>, DispatchError>>()?;140141		let amount = data.len();142		with_weight(143			<Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),144			<CommonWeights<T>>::create_multiple_items(amount as u32),145		)146	}147148	fn create_multiple_items_ex(149		&self,150		sender: <T>::CrossAccountId,151		data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,152		nesting_budget: &dyn Budget,153	) -> DispatchResultWithPostInfo {154		let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);155		let data = match data {156			up_data_structs::CreateItemExData::NFT(nft) => nft,157			_ => fail!(Error::<T>::NotNonfungibleDataUsedToMintFungibleCollectionToken),158		};159160		with_weight(161			<Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),162			weight,163		)164	}165166	fn set_collection_properties(167		&self,168		sender: T::CrossAccountId,169		properties: Vec<Property>,170	) -> DispatchResultWithPostInfo {171		let weight = <CommonWeights<T>>::set_collection_properties(properties.len() as u32);172173		with_weight(174			<Pallet<T>>::set_collection_properties(self, &sender, properties),175			weight,176		)177	}178179	fn delete_collection_properties(180		&self,181		sender: &T::CrossAccountId,182		property_keys: Vec<PropertyKey>,183	) -> DispatchResultWithPostInfo {184		let weight = <CommonWeights<T>>::delete_collection_properties(property_keys.len() as u32);185186		with_weight(187			<Pallet<T>>::delete_collection_properties(self, &sender, property_keys),188			weight,189		)190	}191192	fn set_token_properties(193		&self,194		sender: T::CrossAccountId,195		token_id: TokenId,196		properties: Vec<Property>,197	) -> DispatchResultWithPostInfo {198		let weight = <CommonWeights<T>>::set_token_properties(properties.len() as u32);199200		with_weight(201			<Pallet<T>>::set_token_properties(self, &sender, token_id, properties),202			weight,203		)204	}205206	fn delete_token_properties(207		&self,208		sender: T::CrossAccountId,209		token_id: TokenId,210		property_keys: Vec<PropertyKey>,211	) -> DispatchResultWithPostInfo {212		let weight = <CommonWeights<T>>::delete_token_properties(property_keys.len() as u32);213214		with_weight(215			<Pallet<T>>::delete_token_properties(self, &sender, token_id, property_keys),216			weight,217		)218	}219220	fn set_property_permissions(221		&self,222		sender: &T::CrossAccountId,223		property_permissions: Vec<PropertyKeyPermission>,224	) -> DispatchResultWithPostInfo {225		let weight =226			<CommonWeights<T>>::set_property_permissions(property_permissions.len() as u32);227228		with_weight(229			<Pallet<T>>::set_property_permissions(self, sender, property_permissions),230			weight,231		)232	}233234	fn burn_item(235		&self,236		sender: T::CrossAccountId,237		token: TokenId,238		amount: u128,239	) -> DispatchResultWithPostInfo {240		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);241		if amount == 1 {242			with_weight(243				<Pallet<T>>::burn(self, &sender, token),244				<CommonWeights<T>>::burn_item(),245			)246		} else {247			Ok(().into())248		}249	}250251	fn transfer(252		&self,253		from: T::CrossAccountId,254		to: T::CrossAccountId,255		token: TokenId,256		amount: u128,257		nesting_budget: &dyn Budget,258	) -> DispatchResultWithPostInfo {259		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);260		if amount == 1 {261			with_weight(262				<Pallet<T>>::transfer(self, &from, &to, token, nesting_budget),263				<CommonWeights<T>>::transfer(),264			)265		} else {266			Ok(().into())267		}268	}269270	fn approve(271		&self,272		sender: T::CrossAccountId,273		spender: T::CrossAccountId,274		token: TokenId,275		amount: u128,276	) -> DispatchResultWithPostInfo {277		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);278279		with_weight(280			if amount == 1 {281				<Pallet<T>>::set_allowance(self, &sender, token, Some(&spender))282			} else {283				<Pallet<T>>::set_allowance(self, &sender, token, None)284			},285			<CommonWeights<T>>::approve(),286		)287	}288289	fn transfer_from(290		&self,291		sender: T::CrossAccountId,292		from: T::CrossAccountId,293		to: T::CrossAccountId,294		token: TokenId,295		amount: u128,296		nesting_budget: &dyn Budget,297	) -> DispatchResultWithPostInfo {298		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);299300		if amount == 1 {301			with_weight(302				<Pallet<T>>::transfer_from(self, &sender, &from, &to, token, nesting_budget),303				<CommonWeights<T>>::transfer_from(),304			)305		} else {306			Ok(().into())307		}308	}309310	fn burn_from(311		&self,312		sender: T::CrossAccountId,313		from: T::CrossAccountId,314		token: TokenId,315		amount: u128,316		nesting_budget: &dyn Budget,317	) -> DispatchResultWithPostInfo {318		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);319320		if amount == 1 {321			with_weight(322				<Pallet<T>>::burn_from(self, &sender, &from, token, nesting_budget),323				<CommonWeights<T>>::burn_from(),324			)325		} else {326			Ok(().into())327		}328	}329330	fn set_variable_metadata(331		&self,332		sender: T::CrossAccountId,333		token: TokenId,334		data: BoundedVec<u8, CustomDataLimit>,335	) -> DispatchResultWithPostInfo {336		let len = data.len();337		with_weight(338			<Pallet<T>>::set_variable_metadata(self, &sender, token, data),339			<CommonWeights<T>>::set_variable_metadata(len as u32),340		)341	}342343	fn check_nesting(344		&self,345		sender: T::CrossAccountId,346		from: (CollectionId, TokenId),347		under: TokenId,348		budget: &dyn Budget,349	) -> sp_runtime::DispatchResult {350		<Pallet<T>>::check_nesting(self, sender, from, under, budget)351	}352353	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {354		<Owned<T>>::iter_prefix((self.id, account))355			.map(|(id, _)| id)356			.collect()357	}358359	fn collection_tokens(&self) -> Vec<TokenId> {360		<TokenData<T>>::iter_prefix((self.id,))361			.map(|(id, _)| id)362			.collect()363	}364365	fn token_exists(&self, token: TokenId) -> bool {366		<Pallet<T>>::token_exists(self, token)367	}368369	fn last_token_id(&self) -> TokenId {370		TokenId(<TokensMinted<T>>::get(self.id))371	}372373	fn token_owner(&self, token: TokenId) -> Option<T::CrossAccountId> {374		<TokenData<T>>::get((self.id, token)).map(|t| t.owner)375	}376	fn const_metadata(&self, token: TokenId) -> Vec<u8> {377		<TokenData<T>>::get((self.id, token))378			.map(|t| t.const_data)379			.unwrap_or_default()380			.into_inner()381	}382	fn variable_metadata(&self, token: TokenId) -> Vec<u8> {383		<TokenData<T>>::get((self.id, token))384			.map(|t| t.variable_data)385			.unwrap_or_default()386			.into_inner()387	}388389	fn token_properties(&self, token_id: TokenId, keys: Vec<PropertyKey>) -> Vec<Property> {390		let properties = <Pallet<T>>::token_properties((self.id, token_id));391392		keys.into_iter()393			.filter_map(|key| {394				properties.get(&key).map(|value| Property {395					key,396					value: value.clone(),397				})398			})399			.collect()400	}401402	fn total_supply(&self) -> u32 {403		<Pallet<T>>::total_supply(self)404	}405406	fn account_balance(&self, account: T::CrossAccountId) -> u32 {407		<AccountBalance<T>>::get((self.id, account))408	}409410	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {411		if <TokenData<T>>::get((self.id, token))412			.map(|a| a.owner == account)413			.unwrap_or(false)414		{415			1416		} else {417			0418		}419	}420421	fn allowance(422		&self,423		sender: T::CrossAccountId,424		spender: T::CrossAccountId,425		token: TokenId,426	) -> u128 {427		if <TokenData<T>>::get((self.id, token))428			.map(|a| a.owner != sender)429			.unwrap_or(true)430		{431			0432		} else if <Allowance<T>>::get((self.id, token)) == Some(spender) {433			1434		} else {435			0436		}437	}438}
modifiedpallets/nonfungible/src/erc.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/erc.rs
+++ b/pallets/nonfungible/src/erc.rs
@@ -26,7 +26,8 @@
 use sp_core::{H160, U256};
 use sp_std::{vec::Vec, vec};
 use pallet_common::{
-	erc::{CommonEvmHandler, PrecompileResult},
+	erc::{CommonEvmHandler, PrecompileResult, CollectionPropertiesCall},
+	CollectionHandle,
 };
 use pallet_evm::account::CrossAccountId;
 use pallet_evm_coder_substrate::call;
@@ -504,6 +505,7 @@
 		ERC721UniqueExtensions,
 		ERC721Mintable,
 		ERC721Burnable,
+		via("CollectionHandle<T>", common_mut, CollectionProperties)
 	)
 )]
 impl<T: Config> NonfungibleHandle<T> {}
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -142,6 +142,9 @@
 	pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {
 		self.0
 	}
+	pub fn common_mut(&mut self) -> &mut pallet_common::CollectionHandle<T> {
+		&mut self.0
+	}
 }
 impl<T: Config> WithRecorder<T> for NonfungibleHandle<T> {
 	fn recorder(&self) -> &SubstrateRecorder<T> {
@@ -302,7 +305,8 @@
 
 		<TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {
 			properties.remove(&property_key)
-		}).map_err(|e| -> CommonError<T> { e.into() })?;
+		})
+		.map_err(|e| -> CommonError<T> { e.into() })?;
 
 		<PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertyDeleted(
 			collection.id,
modifiedprimitives/data-structs/src/lib.rsdiffbeforeafterboth
--- a/primitives/data-structs/src/lib.rs
+++ b/primitives/data-structs/src/lib.rs
@@ -699,7 +699,7 @@
 
 	fn try_set_from_iter<I>(&mut self, iter: I) -> Result<(), PropertiesError>
 	where
-		I: Iterator<Item=(PropertyKey, Self::Value)>
+		I: Iterator<Item = (PropertyKey, Self::Value)>,
 	{
 		for (key, value) in iter {
 			self.try_set(key, value)?;
@@ -711,7 +711,9 @@
 
 #[derive(Encode, Decode, TypeInfo, Derivative, Clone, PartialEq, MaxEncodedLen)]
 #[derivative(Default(bound = ""))]
-pub struct PropertiesMap<Value>(BoundedBTreeMap<PropertyKey, Value, ConstU32<MAX_PROPERTIES_PER_ITEM>>);
+pub struct PropertiesMap<Value>(
+	BoundedBTreeMap<PropertyKey, Value, ConstU32<MAX_PROPERTIES_PER_ITEM>>,
+);
 
 impl<Value> PropertiesMap<Value> {
 	pub fn new() -> Self {