git.delta.rocks / unique-network / refs/commits / 2f30e2f1b171

difftreelog

misk: remove simple type aliases

Trubnikov Sergey2023-01-11parent: #916ae42.patch.diff
in: master

9 files changed

modifiedcrates/evm-coder/src/events.rsdiffbeforeafterboth
--- a/crates/evm-coder/src/events.rs
+++ b/crates/evm-coder/src/events.rs
@@ -61,7 +61,7 @@
 	}
 }
 
-impl ToTopic for uint32 {
+impl ToTopic for u32 {
 	fn to_topic(&self) -> H256 {
 		let mut out = [0u8; 32];
 		out[28..32].copy_from_slice(&self.to_be_bytes());
modifiedcrates/evm-coder/src/lib.rsdiffbeforeafterboth
--- a/crates/evm-coder/src/lib.rs
+++ b/crates/evm-coder/src/lib.rs
@@ -132,11 +132,6 @@
 	use primitive_types::{U256, H160, H256};
 
 	pub type address = H160;
-	pub type uint8 = u8;
-	pub type uint16 = u16;
-	pub type uint32 = u32;
-	pub type uint64 = u64;
-	pub type uint128 = u128;
 	pub type uint256 = U256;
 	pub type bytes4 = [u8; 4];
 	pub type topic = H256;
@@ -148,10 +143,6 @@
 
 	#[derive(Default, Debug, PartialEq, Eq, Clone)]
 	pub struct bytes(pub Vec<u8>);
-
-	/// Solidity doesn't have `void` type, however we have special implementation
-	/// for empty tuple return type
-	pub type void = ();
 
 	//#region Special types
 	/// Makes function payable
modifiedpallets/common/src/erc.rsdiffbeforeafterboth
--- a/pallets/common/src/erc.rs
+++ b/pallets/common/src/erc.rs
@@ -93,12 +93,7 @@
 	/// @param value Propery value.
 	#[solidity(hide)]
 	#[weight(<SelfWeightOf<T>>::set_collection_properties(1))]
-	fn set_collection_property(
-		&mut self,
-		caller: caller,
-		key: string,
-		value: bytes,
-	) -> Result<void> {
+	fn set_collection_property(&mut self, caller: caller, key: string, value: bytes) -> Result<()> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let key = <Vec<u8>>::from(key)
 			.try_into()
@@ -117,7 +112,7 @@
 		&mut self,
 		caller: caller,
 		properties: Vec<eth::Property>,
-	) -> Result<void> {
+	) -> Result<()> {
 		let caller = T::CrossAccountId::from_eth(caller);
 
 		let properties = properties
@@ -212,7 +207,7 @@
 	///
 	/// @param sponsor Address of the sponsor from whose account funds will be debited for operations with the contract.
 	#[solidity(hide)]
-	fn set_collection_sponsor(&mut self, caller: caller, sponsor: address) -> Result<void> {
+	fn set_collection_sponsor(&mut self, caller: caller, sponsor: address) -> Result<()> {
 		self.consume_store_reads_and_writes(1, 1)?;
 
 		let caller = T::CrossAccountId::from_eth(caller);
@@ -231,7 +226,7 @@
 		&mut self,
 		caller: caller,
 		sponsor: eth::CrossAddress,
-	) -> Result<void> {
+	) -> Result<()> {
 		self.consume_store_reads_and_writes(1, 1)?;
 
 		let caller = T::CrossAccountId::from_eth(caller);
@@ -252,7 +247,7 @@
 	/// Collection sponsorship confirmation.
 	///
 	/// @dev After setting the sponsor for the collection, it must be confirmed with this function.
-	fn confirm_collection_sponsorship(&mut self, caller: caller) -> Result<void> {
+	fn confirm_collection_sponsorship(&mut self, caller: caller) -> Result<()> {
 		self.consume_store_writes(1)?;
 
 		let caller = T::CrossAccountId::from_eth(caller);
@@ -261,7 +256,7 @@
 	}
 
 	/// Remove collection sponsor.
-	fn remove_collection_sponsor(&mut self, caller: caller) -> Result<void> {
+	fn remove_collection_sponsor(&mut self, caller: caller) -> Result<()> {
 		self.consume_store_reads_and_writes(1, 1)?;
 		let caller = T::CrossAccountId::from_eth(caller);
 		self.remove_sponsor(&caller).map_err(dispatch_to_evm::<T>)
@@ -343,11 +338,7 @@
 	/// @dev Throws error if limit not found.
 	/// @param limit Some limit.
 	#[solidity(rename_selector = "setCollectionLimit")]
-	fn set_collection_limit(
-		&mut self,
-		caller: caller,
-		limit: eth::CollectionLimit,
-	) -> Result<void> {
+	fn set_collection_limit(&mut self, caller: caller, limit: eth::CollectionLimit) -> Result<()> {
 		self.consume_store_reads_and_writes(1, 1)?;
 
 		if !limit.has_value() {
@@ -369,7 +360,7 @@
 		&mut self,
 		caller: caller,
 		new_admin: eth::CrossAddress,
-	) -> Result<void> {
+	) -> Result<()> {
 		self.consume_store_reads_and_writes(2, 2)?;
 
 		let caller = T::CrossAccountId::from_eth(caller);
@@ -384,7 +375,7 @@
 		&mut self,
 		caller: caller,
 		admin: eth::CrossAddress,
-	) -> Result<void> {
+	) -> Result<()> {
 		self.consume_store_reads_and_writes(2, 2)?;
 
 		let caller = T::CrossAccountId::from_eth(caller);
@@ -396,7 +387,7 @@
 	/// Add collection admin.
 	/// @param newAdmin Address of the added administrator.
 	#[solidity(hide)]
-	fn add_collection_admin(&mut self, caller: caller, new_admin: address) -> Result<void> {
+	fn add_collection_admin(&mut self, caller: caller, new_admin: address) -> Result<()> {
 		self.consume_store_reads_and_writes(2, 2)?;
 
 		let caller = T::CrossAccountId::from_eth(caller);
@@ -409,7 +400,7 @@
 	///
 	/// @param admin Address of the removed administrator.
 	#[solidity(hide)]
-	fn remove_collection_admin(&mut self, caller: caller, admin: address) -> Result<void> {
+	fn remove_collection_admin(&mut self, caller: caller, admin: address) -> Result<()> {
 		self.consume_store_reads_and_writes(2, 2)?;
 
 		let caller = T::CrossAccountId::from_eth(caller);
@@ -422,7 +413,7 @@
 	///
 	/// @param enable If "true" degenerates to nesting: 'Owner' else to nesting: 'Disabled'
 	#[solidity(rename_selector = "setCollectionNesting")]
-	fn set_nesting_bool(&mut self, caller: caller, enable: bool) -> Result<void> {
+	fn set_nesting_bool(&mut self, caller: caller, enable: bool) -> Result<()> {
 		self.consume_store_reads_and_writes(1, 1)?;
 
 		let caller = T::CrossAccountId::from_eth(caller);
@@ -446,7 +437,7 @@
 		caller: caller,
 		enable: bool,
 		collections: Vec<address>,
-	) -> Result<void> {
+	) -> Result<()> {
 		self.consume_store_reads_and_writes(1, 1)?;
 
 		if collections.is_empty() {
@@ -513,7 +504,7 @@
 	/// @param mode Access mode
 	/// 	0 for Normal
 	/// 	1 for AllowList
-	fn set_collection_access(&mut self, caller: caller, mode: uint8) -> Result<void> {
+	fn set_collection_access(&mut self, caller: caller, mode: u8) -> Result<()> {
 		self.consume_store_reads_and_writes(1, 1)?;
 
 		let caller = T::CrossAccountId::from_eth(caller);
@@ -540,7 +531,7 @@
 	///
 	/// @param user Address of a trusted user.
 	#[solidity(hide)]
-	fn add_to_collection_allow_list(&mut self, caller: caller, user: address) -> Result<void> {
+	fn add_to_collection_allow_list(&mut self, caller: caller, user: address) -> Result<()> {
 		self.consume_store_writes(1)?;
 
 		let caller = T::CrossAccountId::from_eth(caller);
@@ -556,7 +547,7 @@
 		&mut self,
 		caller: caller,
 		user: eth::CrossAddress,
-	) -> Result<void> {
+	) -> Result<()> {
 		self.consume_store_writes(1)?;
 
 		let caller = T::CrossAccountId::from_eth(caller);
@@ -569,7 +560,7 @@
 	///
 	/// @param user Address of a removed user.
 	#[solidity(hide)]
-	fn remove_from_collection_allow_list(&mut self, caller: caller, user: address) -> Result<void> {
+	fn remove_from_collection_allow_list(&mut self, caller: caller, user: address) -> Result<()> {
 		self.consume_store_writes(1)?;
 
 		let caller = T::CrossAccountId::from_eth(caller);
@@ -585,7 +576,7 @@
 		&mut self,
 		caller: caller,
 		user: eth::CrossAddress,
-	) -> Result<void> {
+	) -> Result<()> {
 		self.consume_store_writes(1)?;
 
 		let caller = T::CrossAccountId::from_eth(caller);
@@ -597,7 +588,7 @@
 	/// Switch permission for minting.
 	///
 	/// @param mode Enable if "true".
-	fn set_collection_mint_mode(&mut self, caller: caller, mode: bool) -> Result<void> {
+	fn set_collection_mint_mode(&mut self, caller: caller, mode: bool) -> Result<()> {
 		self.consume_store_reads_and_writes(1, 1)?;
 
 		let caller = T::CrossAccountId::from_eth(caller);
@@ -654,7 +645,7 @@
 	/// @dev Owner can be changed only by current owner
 	/// @param newOwner new owner account
 	#[solidity(hide, rename_selector = "changeCollectionOwner")]
-	fn set_owner(&mut self, caller: caller, new_owner: address) -> Result<void> {
+	fn set_owner(&mut self, caller: caller, new_owner: address) -> Result<()> {
 		self.consume_store_writes(1)?;
 
 		let caller = T::CrossAccountId::from_eth(caller);
@@ -682,7 +673,7 @@
 		&mut self,
 		caller: caller,
 		new_owner: eth::CrossAddress,
-	) -> Result<void> {
+	) -> Result<()> {
 		self.consume_store_writes(1)?;
 
 		let caller = T::CrossAccountId::from_eth(caller);
modifiedpallets/evm-contract-helpers/src/eth.rsdiffbeforeafterboth
--- a/pallets/evm-contract-helpers/src/eth.rs
+++ b/pallets/evm-contract-helpers/src/eth.rs
@@ -108,7 +108,7 @@
 		caller: caller,
 		contract_address: address,
 		sponsor: address,
-	) -> Result<void> {
+	) -> Result<()> {
 		self.recorder().consume_sload()?;
 		self.recorder().consume_sstore()?;
 
@@ -125,7 +125,7 @@
 	/// Set contract as self sponsored.
 	///
 	/// @param contractAddress Contract for which a self sponsoring is being enabled.
-	fn self_sponsored_enable(&mut self, caller: caller, contract_address: address) -> Result<void> {
+	fn self_sponsored_enable(&mut self, caller: caller, contract_address: address) -> Result<()> {
 		self.recorder().consume_sload()?;
 		self.recorder().consume_sstore()?;
 
@@ -146,7 +146,7 @@
 	/// Remove sponsor.
 	///
 	/// @param contractAddress Contract for which a sponsorship is being removed.
-	fn remove_sponsor(&mut self, caller: caller, contract_address: address) -> Result<void> {
+	fn remove_sponsor(&mut self, caller: caller, contract_address: address) -> Result<()> {
 		self.recorder().consume_sload()?;
 		self.recorder().consume_sstore()?;
 
@@ -161,7 +161,7 @@
 	/// @dev Caller must be same that set via [`setSponsor`].
 	///
 	/// @param contractAddress Сontract for which need to confirm sponsorship.
-	fn confirm_sponsorship(&mut self, caller: caller, contract_address: address) -> Result<void> {
+	fn confirm_sponsorship(&mut self, caller: caller, contract_address: address) -> Result<()> {
 		self.recorder().consume_sload()?;
 		self.recorder().consume_sstore()?;
 
@@ -210,7 +210,7 @@
 		caller: caller,
 		contract_address: address,
 		mode: SponsoringModeT,
-	) -> Result<void> {
+	) -> Result<()> {
 		self.recorder().consume_sload()?;
 		self.recorder().consume_sstore()?;
 
@@ -223,7 +223,7 @@
 	/// Get current contract sponsoring rate limit
 	/// @param contractAddress Contract to get sponsoring rate limit of
 	/// @return uint32 Amount of blocks between two sponsored transactions
-	fn sponsoring_rate_limit(&self, contract_address: address) -> Result<uint32> {
+	fn sponsoring_rate_limit(&self, contract_address: address) -> Result<u32> {
 		self.recorder().consume_sload()?;
 
 		Ok(<SponsoringRateLimit<T>>::get(contract_address)
@@ -241,8 +241,8 @@
 		&mut self,
 		caller: caller,
 		contract_address: address,
-		rate_limit: uint32,
-	) -> Result<void> {
+		rate_limit: u32,
+	) -> Result<()> {
 		self.recorder().consume_sload()?;
 		self.recorder().consume_sstore()?;
 
@@ -262,7 +262,7 @@
 		caller: caller,
 		contract_address: address,
 		fee_limit: uint256,
-	) -> Result<void> {
+	) -> Result<()> {
 		self.recorder().consume_sload()?;
 		self.recorder().consume_sstore()?;
 
@@ -304,7 +304,7 @@
 		contract_address: address,
 		user: address,
 		is_allowed: bool,
-	) -> Result<void> {
+	) -> Result<()> {
 		self.recorder().consume_sload()?;
 		self.recorder().consume_sstore()?;
 
@@ -332,7 +332,7 @@
 		caller: caller,
 		contract_address: address,
 		enabled: bool,
-	) -> Result<void> {
+	) -> Result<()> {
 		self.recorder().consume_sload()?;
 		self.recorder().consume_sstore()?;
 
modifiedpallets/fungible/src/erc.rsdiffbeforeafterboth
--- a/pallets/fungible/src/erc.rs
+++ b/pallets/fungible/src/erc.rs
@@ -72,7 +72,7 @@
 		Ok(<TotalSupply<T>>::get(self.id).into())
 	}
 
-	fn decimals(&self) -> Result<uint8> {
+	fn decimals(&self) -> Result<u8> {
 		Ok(if let CollectionMode::Fungible(decimals) = &self.mode {
 			*decimals
 		} else {
modifiedpallets/nonfungible/src/erc.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/erc.rs
+++ b/pallets/nonfungible/src/erc.rs
@@ -423,7 +423,7 @@
 		_to: address,
 		_token_id: uint256,
 		_data: bytes,
-	) -> Result<void> {
+	) -> Result<()> {
 		// TODO: Not implemetable
 		Err("not implemented".into())
 	}
@@ -433,7 +433,7 @@
 		_from: address,
 		_to: address,
 		_token_id: uint256,
-	) -> Result<void> {
+	) -> Result<()> {
 		// TODO: Not implemetable
 		Err("not implemented".into())
 	}
@@ -454,7 +454,7 @@
 		from: address,
 		to: address,
 		token_id: uint256,
-	) -> Result<void> {
+	) -> Result<()> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let from = T::CrossAccountId::from_eth(from);
 		let to = T::CrossAccountId::from_eth(to);
@@ -475,7 +475,7 @@
 	/// @param approved The new approved NFT controller
 	/// @param tokenId The NFT to approve
 	#[weight(<SelfWeightOf<T>>::approve())]
-	fn approve(&mut self, caller: caller, approved: address, token_id: uint256) -> Result<void> {
+	fn approve(&mut self, caller: caller, approved: address, token_id: uint256) -> Result<()> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let approved = T::CrossAccountId::from_eth(approved);
 		let token = token_id.try_into()?;
@@ -495,7 +495,7 @@
 		caller: caller,
 		operator: address,
 		approved: bool,
-	) -> Result<void> {
+	) -> Result<()> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let operator = T::CrossAccountId::from_eth(operator);
 
@@ -528,7 +528,7 @@
 	///  operator of the current owner.
 	/// @param tokenId The NFT to approve
 	#[weight(<SelfWeightOf<T>>::burn_item())]
-	fn burn(&mut self, caller: caller, token_id: uint256) -> Result<void> {
+	fn burn(&mut self, caller: caller, token_id: uint256) -> Result<()> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let token = token_id.try_into()?;
 
@@ -773,7 +773,7 @@
 		caller: caller,
 		approved: eth::CrossAddress,
 		token_id: uint256,
-	) -> Result<void> {
+	) -> Result<()> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let approved = approved.into_sub_cross_account::<T>()?;
 		let token = token_id.try_into()?;
@@ -789,7 +789,7 @@
 	/// @param to The new owner
 	/// @param tokenId The NFT to transfer
 	#[weight(<SelfWeightOf<T>>::transfer())]
-	fn transfer(&mut self, caller: caller, to: address, token_id: uint256) -> Result<void> {
+	fn transfer(&mut self, caller: caller, to: address, token_id: uint256) -> Result<()> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let to = T::CrossAccountId::from_eth(to);
 		let token = token_id.try_into()?;
@@ -812,7 +812,7 @@
 		caller: caller,
 		to: eth::CrossAddress,
 		token_id: uint256,
-	) -> Result<void> {
+	) -> Result<()> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let to = to.into_sub_cross_account::<T>()?;
 		let token = token_id.try_into()?;
@@ -837,7 +837,7 @@
 		from: eth::CrossAddress,
 		to: eth::CrossAddress,
 		token_id: uint256,
-	) -> Result<void> {
+	) -> Result<()> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let from = from.into_sub_cross_account::<T>()?;
 		let to = to.into_sub_cross_account::<T>()?;
@@ -858,7 +858,7 @@
 	/// @param tokenId The NFT to transfer
 	#[solidity(hide)]
 	#[weight(<SelfWeightOf<T>>::burn_from())]
-	fn burn_from(&mut self, caller: caller, from: address, token_id: uint256) -> Result<void> {
+	fn burn_from(&mut self, caller: caller, from: address, token_id: uint256) -> Result<()> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let from = T::CrossAccountId::from_eth(from);
 		let token = token_id.try_into()?;
@@ -883,7 +883,7 @@
 		caller: caller,
 		from: eth::CrossAddress,
 		token_id: uint256,
-	) -> Result<void> {
+	) -> Result<()> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let from = from.into_sub_cross_account::<T>()?;
 		let token = token_id.try_into()?;
modifiedpallets/refungible/src/erc.rsdiffbeforeafterboth
--- a/pallets/refungible/src/erc.rs
+++ b/pallets/refungible/src/erc.rs
@@ -424,7 +424,7 @@
 		_to: address,
 		_token_id: uint256,
 		_data: bytes,
-	) -> Result<void> {
+	) -> Result<()> {
 		// TODO: Not implemetable
 		Err("not implemented".into())
 	}
@@ -436,7 +436,7 @@
 		_from: address,
 		_to: address,
 		_token_id: uint256,
-	) -> Result<void> {
+	) -> Result<()> {
 		// TODO: Not implemetable
 		Err("not implemented".into())
 	}
@@ -458,7 +458,7 @@
 		from: address,
 		to: address,
 		token_id: uint256,
-	) -> Result<void> {
+	) -> Result<()> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let from = T::CrossAccountId::from_eth(from);
 		let to = T::CrossAccountId::from_eth(to);
@@ -477,7 +477,7 @@
 	}
 
 	/// @dev Not implemented
-	fn approve(&mut self, _caller: caller, _approved: address, _token_id: uint256) -> Result<void> {
+	fn approve(&mut self, _caller: caller, _approved: address, _token_id: uint256) -> Result<()> {
 		Err("not implemented".into())
 	}
 
@@ -491,7 +491,7 @@
 		caller: caller,
 		operator: address,
 		approved: bool,
-	) -> Result<void> {
+	) -> Result<()> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let operator = T::CrossAccountId::from_eth(operator);
 
@@ -556,7 +556,7 @@
 	///  operator of the current owner.
 	/// @param tokenId The RFT to approve
 	#[weight(<SelfWeightOf<T>>::burn_item_fully())]
-	fn burn(&mut self, caller: caller, token_id: uint256) -> Result<void> {
+	fn burn(&mut self, caller: caller, token_id: uint256) -> Result<()> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let token = token_id.try_into()?;
 
@@ -805,7 +805,7 @@
 	/// @param to The new owner
 	/// @param tokenId The RFT to transfer
 	#[weight(<SelfWeightOf<T>>::transfer_creating_removing())]
-	fn transfer(&mut self, caller: caller, to: address, token_id: uint256) -> Result<void> {
+	fn transfer(&mut self, caller: caller, to: address, token_id: uint256) -> Result<()> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let to = T::CrossAccountId::from_eth(to);
 		let token = token_id.try_into()?;
@@ -833,7 +833,7 @@
 		caller: caller,
 		to: eth::CrossAddress,
 		token_id: uint256,
-	) -> Result<void> {
+	) -> Result<()> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let to = to.into_sub_cross_account::<T>()?;
 		let token = token_id.try_into()?;
@@ -862,7 +862,7 @@
 		from: eth::CrossAddress,
 		to: eth::CrossAddress,
 		token_id: uint256,
-	) -> Result<void> {
+	) -> Result<()> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let from = from.into_sub_cross_account::<T>()?;
 		let to = to.into_sub_cross_account::<T>()?;
@@ -888,7 +888,7 @@
 	/// @param tokenId The RFT to transfer
 	#[solidity(hide)]
 	#[weight(<SelfWeightOf<T>>::burn_from())]
-	fn burn_from(&mut self, caller: caller, from: address, token_id: uint256) -> Result<void> {
+	fn burn_from(&mut self, caller: caller, from: address, token_id: uint256) -> Result<()> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let from = T::CrossAccountId::from_eth(from);
 		let token = token_id.try_into()?;
@@ -917,7 +917,7 @@
 		caller: caller,
 		from: eth::CrossAddress,
 		token_id: uint256,
-	) -> Result<void> {
+	) -> Result<()> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let from = from.into_sub_cross_account::<T>()?;
 		let token = token_id.try_into()?;
modifiedpallets/refungible/src/erc_token.rsdiffbeforeafterboth
--- a/pallets/refungible/src/erc_token.rs
+++ b/pallets/refungible/src/erc_token.rs
@@ -109,7 +109,7 @@
 	}
 
 	/// @dev Not supported
-	fn decimals(&self) -> Result<uint8> {
+	fn decimals(&self) -> Result<u8> {
 		// Decimals aren't supported for refungible tokens
 		Ok(0)
 	}
modifiedpallets/unique/src/eth/mod.rsdiffbeforeafterboth
before · pallets/unique/src/eth/mod.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/>.1617//! Implementation of CollectionHelpers contract.1819use core::marker::PhantomData;20use ethereum as _;21use evm_coder::{22	abi::AbiType, execution::*, generate_stubgen, solidity, solidity_interface, types::*, weight,23};24use frame_support::traits::Get;25use crate::Pallet;2627use pallet_common::{28	CollectionById,29	dispatch::CollectionDispatch,30	erc::{CollectionHelpersEvents, static_property::key},31	eth::{map_eth_to_id, collection_id_to_address},32	Pallet as PalletCommon,33};34use pallet_evm::{account::CrossAccountId, OnMethodCall, PrecompileHandle, PrecompileResult};35use pallet_evm_coder_substrate::{dispatch_to_evm, SubstrateRecorder, WithRecorder};36use up_data_structs::{37	CollectionDescription, CollectionMode, CollectionName, CollectionTokenPrefix,38	CreateCollectionData,39};4041use crate::{weights::WeightInfo, Config, SelfWeightOf};4243use alloc::format;44use sp_std::vec::Vec;4546/// See [`CollectionHelpersCall`]47pub struct EvmCollectionHelpers<T: Config>(SubstrateRecorder<T>);48impl<T: Config> WithRecorder<T> for EvmCollectionHelpers<T> {49	fn recorder(&self) -> &SubstrateRecorder<T> {50		&self.051	}5253	fn into_recorder(self) -> SubstrateRecorder<T> {54		self.055	}56}5758fn convert_data<T: Config>(59	caller: caller,60	name: string,61	description: string,62	token_prefix: string,63) -> Result<(64	T::CrossAccountId,65	CollectionName,66	CollectionDescription,67	CollectionTokenPrefix,68)> {69	let caller = T::CrossAccountId::from_eth(caller);70	let name = name71		.encode_utf16()72		.collect::<Vec<u16>>()73		.try_into()74		.map_err(|_| error_field_too_long(stringify!(name), CollectionName::bound()))?;75	let description = description76		.encode_utf16()77		.collect::<Vec<u16>>()78		.try_into()79		.map_err(|_| {80			error_field_too_long(stringify!(description), CollectionDescription::bound())81		})?;82	let token_prefix = token_prefix.into_bytes().try_into().map_err(|_| {83		error_field_too_long(stringify!(token_prefix), CollectionTokenPrefix::bound())84	})?;85	Ok((caller, name, description, token_prefix))86}8788#[inline(always)]89fn create_collection_internal<T: Config>(90	caller: caller,91	value: value,92	name: string,93	collection_mode: CollectionMode,94	description: string,95	token_prefix: string,96) -> Result<address> {97	let (caller, name, description, token_prefix) =98		convert_data::<T>(caller, name, description, token_prefix)?;99	let data = CreateCollectionData {100		name,101		mode: collection_mode,102		description,103		token_prefix,104		..Default::default()105	};106	check_sent_amount_equals_collection_creation_price::<T>(value)?;107	let collection_helpers_address =108		T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());109110	let collection_id = T::CollectionDispatch::create(111		caller.clone(),112		collection_helpers_address,113		data,114		Default::default(),115	)116	.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;117	let address = pallet_common::eth::collection_id_to_address(collection_id);118	Ok(address)119}120121fn check_sent_amount_equals_collection_creation_price<T: Config>(value: value) -> Result<()> {122	let value = value.as_u128();123	let creation_price: u128 = T::CollectionCreationPrice::get()124		.try_into()125		.map_err(|_| ()) // workaround for `expect` requiring `Debug` trait126		.expect("Collection creation price should be convertible to u128");127	if value != creation_price {128		return Err(format!(129			"Sent amount not equals to collection creation price ({0})",130			creation_price131		)132		.into());133	}134	Ok(())135}136137/// @title Contract, which allows users to operate with collections138#[solidity_interface(name = CollectionHelpers, events(CollectionHelpersEvents))]139impl<T> EvmCollectionHelpers<T>140where141	T: Config + pallet_common::Config + pallet_nonfungible::Config + pallet_refungible::Config,142{143	/// Create an NFT collection144	/// @param name Name of the collection145	/// @param description Informative description of the collection146	/// @param tokenPrefix Token prefix to represent the collection tokens in UI and user applications147	/// @return address Address of the newly created collection148	#[weight(<SelfWeightOf<T>>::create_collection())]149	#[solidity(rename_selector = "createNFTCollection")]150	fn create_nft_collection(151		&mut self,152		caller: caller,153		value: value,154		name: string,155		description: string,156		token_prefix: string,157	) -> Result<address> {158		let (caller, name, description, token_prefix) =159			convert_data::<T>(caller, name, description, token_prefix)?;160		let data = CreateCollectionData {161			name,162			mode: CollectionMode::NFT,163			description,164			token_prefix,165			..Default::default()166		};167		check_sent_amount_equals_collection_creation_price::<T>(value)?;168		let collection_helpers_address =169			T::CrossAccountId::from_eth(<T as pallet_common::Config>::ContractAddress::get());170		let collection_id = T::CollectionDispatch::create(171			caller,172			collection_helpers_address,173			data,174			Default::default(),175		)176		.map_err(dispatch_to_evm::<T>)?;177178		let address = pallet_common::eth::collection_id_to_address(collection_id);179		Ok(address)180	}181	/// Create an NFT collection182	/// @param name Name of the collection183	/// @param description Informative description of the collection184	/// @param tokenPrefix Token prefix to represent the collection tokens in UI and user applications185	/// @return address Address of the newly created collection186	#[weight(<SelfWeightOf<T>>::create_collection())]187	#[deprecated(note = "mathod was renamed to `create_nft_collection`, prefer it instead")]188	#[solidity(hide)]189	fn create_nonfungible_collection(190		&mut self,191		caller: caller,192		value: value,193		name: string,194		description: string,195		token_prefix: string,196	) -> Result<address> {197		create_collection_internal::<T>(198			caller,199			value,200			name,201			CollectionMode::NFT,202			description,203			token_prefix,204		)205	}206207	#[weight(<SelfWeightOf<T>>::create_collection())]208	#[solidity(rename_selector = "createRFTCollection")]209	fn create_rft_collection(210		&mut self,211		caller: caller,212		value: value,213		name: string,214		description: string,215		token_prefix: string,216	) -> Result<address> {217		create_collection_internal::<T>(218			caller,219			value,220			name,221			CollectionMode::ReFungible,222			description,223			token_prefix,224		)225	}226227	#[weight(<SelfWeightOf<T>>::create_collection())]228	#[solidity(rename_selector = "createFTCollection")]229	fn create_fungible_collection(230		&mut self,231		caller: caller,232		value: value,233		name: string,234		decimals: uint8,235		description: string,236		token_prefix: string,237	) -> Result<address> {238		create_collection_internal::<T>(239			caller,240			value,241			name,242			CollectionMode::Fungible(decimals),243			description,244			token_prefix,245		)246	}247248	#[solidity(rename_selector = "makeCollectionERC721MetadataCompatible")]249	fn make_collection_metadata_compatible(250		&mut self,251		caller: caller,252		collection: address,253		base_uri: string,254	) -> Result<()> {255		let caller = T::CrossAccountId::from_eth(caller);256		let collection =257			pallet_common::eth::map_eth_to_id(&collection).ok_or("not a collection address")?;258		let mut collection =259			<crate::CollectionHandle<T>>::new(collection).ok_or("collection not found")?;260261		if !matches!(262			collection.mode,263			CollectionMode::NFT | CollectionMode::ReFungible264		) {265			return Err("target collection should be either NFT or Refungible".into());266		}267268		self.recorder().consume_sstore()?;269		collection270			.check_is_owner_or_admin(&caller)271			.map_err(dispatch_to_evm::<T>)?;272273		if collection.flags.erc721metadata {274			return Err("target collection is already Erc721Metadata compatible".into());275		}276		collection.flags.erc721metadata = true;277278		let all_permissions = <pallet_common::CollectionPropertyPermissions<T>>::get(collection.id);279		if all_permissions.get(&key::url()).is_none() {280			self.recorder().consume_sstore()?;281			<PalletCommon<T>>::set_property_permission(282				&collection,283				&caller,284				up_data_structs::PropertyKeyPermission {285					key: key::url(),286					permission: up_data_structs::PropertyPermission {287						mutable: true,288						collection_admin: true,289						token_owner: false,290					},291				},292			)293			.map_err(dispatch_to_evm::<T>)?;294		}295		if all_permissions.get(&key::suffix()).is_none() {296			self.recorder().consume_sstore()?;297			<PalletCommon<T>>::set_property_permission(298				&collection,299				&caller,300				up_data_structs::PropertyKeyPermission {301					key: key::suffix(),302					permission: up_data_structs::PropertyPermission {303						mutable: true,304						collection_admin: true,305						token_owner: false,306					},307				},308			)309			.map_err(dispatch_to_evm::<T>)?;310		}311312		let all_properties = <pallet_common::CollectionProperties<T>>::get(collection.id);313		if all_properties.get(&key::base_uri()).is_none() && !base_uri.is_empty() {314			self.recorder().consume_sstore()?;315			<PalletCommon<T>>::set_collection_properties(316				&collection,317				&caller,318				[up_data_structs::Property {319					key: key::base_uri(),320					value: base_uri321						.into_bytes()322						.try_into()323						.map_err(|_| "base uri is too large")?,324				}]325				.into_iter(),326			)327			.map_err(dispatch_to_evm::<T>)?;328		}329330		self.recorder().consume_sstore()?;331		collection.save().map_err(dispatch_to_evm::<T>)?;332333		Ok(())334	}335336	#[weight(<SelfWeightOf<T>>::destroy_collection())]337	fn destroy_collection(&mut self, caller: caller, collection_address: address) -> Result<void> {338		let caller = T::CrossAccountId::from_eth(caller);339340		let collection_id = pallet_common::eth::map_eth_to_id(&collection_address)341			.ok_or("Invalid collection address format")?;342		<Pallet<T>>::destroy_collection_internal(caller, collection_id)343			.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)344	}345346	/// Check if a collection exists347	/// @param collectionAddress Address of the collection in question348	/// @return bool Does the collection exist?349	fn is_collection_exist(&self, _caller: caller, collection_address: address) -> Result<bool> {350		if let Some(id) = pallet_common::eth::map_eth_to_id(&collection_address) {351			let collection_id = id;352			return Ok(<CollectionById<T>>::contains_key(collection_id));353		}354355		Ok(false)356	}357358	fn collection_creation_fee(&self) -> Result<value> {359		let price: u128 = T::CollectionCreationPrice::get()360			.try_into()361			.map_err(|_| ()) // workaround for `expect` requiring `Debug` trait362			.expect("Collection creation price should be convertible to u128");363		Ok(price.into())364	}365366	/// Returns address of a collection.367	/// @param collectionId  - CollectionId  of the collection368	/// @return eth mirror address of the collection369	fn collection_address(&self, collection_id: uint32) -> Result<address> {370		Ok(collection_id_to_address(collection_id.into()))371	}372373	/// Returns collectionId of a collection.374	/// @param collectionAddress  - Eth address of the collection375	/// @return collectionId of the collection376	fn collection_id(&self, collection_address: address) -> Result<uint32> {377		map_eth_to_id(&collection_address)378			.map(|id| id.0)379			.ok_or(Error::Revert(format!(380				"failed to convert address {} into collectionId.",381				collection_address382			)))383	}384}385386/// Implements [`OnMethodCall`], which delegates call to [`EvmCollectionHelpers`]387pub struct CollectionHelpersOnMethodCall<T: Config>(PhantomData<*const T>);388impl<T: Config + pallet_nonfungible::Config + pallet_refungible::Config> OnMethodCall<T>389	for CollectionHelpersOnMethodCall<T>390{391	fn is_reserved(contract: &sp_core::H160) -> bool {392		contract == &T::ContractAddress::get()393	}394395	fn is_used(contract: &sp_core::H160) -> bool {396		contract == &T::ContractAddress::get()397	}398399	fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {400		if handle.code_address() != T::ContractAddress::get() {401			return None;402		}403404		let helpers =405			EvmCollectionHelpers::<T>(SubstrateRecorder::<T>::new(handle.remaining_gas()));406		pallet_evm_coder_substrate::call(handle, helpers)407	}408409	fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {410		(contract == &T::ContractAddress::get())411			.then(|| include_bytes!("./stubs/CollectionHelpers.raw").to_vec())412	}413}414415generate_stubgen!(collection_helper_impl, CollectionHelpersCall<()>, true);416generate_stubgen!(collection_helper_iface, CollectionHelpersCall<()>, false);417418fn error_field_too_long(feild: &str, bound: usize) -> Error {419	Error::Revert(format!("{} is too long. Max length is {}.", feild, bound))420}