git.delta.rocks / unique-network / refs/commits / 4458a1dde4ff

difftreelog

minor: Fix tokenURI logic.

Trubnikov Sergey2022-07-19parent: #2dd986e.patch.diff
in: master

2 files changed

modifiedpallets/nonfungible/src/erc.rsdiffbeforeafterboth
before · pallets/nonfungible/src/erc.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//! # Nonfungible Pallet EVM API18//!19//! Provides ERC-721 standart support implementation and EVM API for unique extensions for Nonfungible Pallet.20//! Method implementations are mostly doing parameter conversion and calling Nonfungible Pallet methods.2122extern crate alloc;23use core::{24	char::{REPLACEMENT_CHARACTER, decode_utf16},25	convert::TryInto,26};27use evm_coder::{ToLog, execution::*, generate_stubgen, solidity, solidity_interface, types::*, weight};28use frame_support::BoundedVec;29use up_data_structs::{30	TokenId, PropertyPermission, PropertyKeyPermission, Property, CollectionId, PropertyKey,31	CollectionPropertiesVec,32};33use pallet_evm_coder_substrate::dispatch_to_evm;34use sp_std::vec::Vec;35use pallet_common::{36	erc::{CommonEvmHandler, PrecompileResult, CollectionCall, static_property_key_value::*},37	CollectionHandle, CollectionPropertyPermissions,38};39use pallet_evm::{account::CrossAccountId, PrecompileHandle};40use pallet_evm_coder_substrate::call;41use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};42use alloc::string::ToString;4344use crate::{45	AccountBalance, Config, CreateItemData, NonfungibleHandle, Pallet, TokenData, TokensMinted,46	SelfWeightOf, weights::WeightInfo, TokenProperties,47};4849/// @title A contract that allows to set and delete token properties and change token property permissions.50#[solidity_interface(name = "TokenProperties")]51impl<T: Config> NonfungibleHandle<T> {52	/// @notice Set permissions for token property.53	/// @dev Throws error if `msg.sender` is not admin or owner of the collection.54	/// @param key Property key.55	/// @param is_mutable Permission to mutate property.56	/// @param collection_admin Permission to mutate property by collection admin if property is mutable.57	/// @param token_owner Permission to mutate property by token owner if property is mutable.58	fn set_token_property_permission(59		&mut self,60		caller: caller,61		key: string,62		is_mutable: bool,63		collection_admin: bool,64		token_owner: bool,65	) -> Result<()> {66		let caller = T::CrossAccountId::from_eth(caller);67		<Pallet<T>>::set_property_permission(68			self,69			&caller,70			PropertyKeyPermission {71				key: <Vec<u8>>::from(key)72					.try_into()73					.map_err(|_| "too long key")?,74				permission: PropertyPermission {75					mutable: is_mutable,76					collection_admin,77					token_owner,78				},79			},80		)81		.map_err(dispatch_to_evm::<T>)82	}8384	/// @notice Set token property value.85	/// @dev Throws error if `msg.sender` has no permission to edit the property.86	/// @param tokenId ID of the token.87	/// @param key Property key.88	/// @param value Property value.89	fn set_property(90		&mut self,91		caller: caller,92		token_id: uint256,93		key: string,94		value: bytes,95	) -> Result<()> {96		let caller = T::CrossAccountId::from_eth(caller);97		let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;98		let key = <Vec<u8>>::from(key)99			.try_into()100			.map_err(|_| "key too long")?;101		let value = value.try_into().map_err(|_| "value too long")?;102103		let nesting_budget = self104			.recorder105			.weight_calls_budget(<StructureWeight<T>>::find_parent());106107		<Pallet<T>>::set_token_property(108			self,109			&caller,110			TokenId(token_id),111			Property { key, value },112			&nesting_budget,113		)114		.map_err(dispatch_to_evm::<T>)115	}116117	/// @notice Delete token property value.118	/// @dev Throws error if `msg.sender` has no permission to edit the property.119	/// @param tokenId ID of the token.120	/// @param key Property key.121	fn delete_property(&mut self, token_id: uint256, caller: caller, key: string) -> Result<()> {122		let caller = T::CrossAccountId::from_eth(caller);123		let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;124		let key = <Vec<u8>>::from(key)125			.try_into()126			.map_err(|_| "key too long")?;127128		let nesting_budget = self129			.recorder130			.weight_calls_budget(<StructureWeight<T>>::find_parent());131132		<Pallet<T>>::delete_token_property(self, &caller, TokenId(token_id), key, &nesting_budget)133			.map_err(dispatch_to_evm::<T>)134	}135136	/// @notice Get token property value.137	/// @dev Throws error if key not found138	/// @param tokenId ID of the token.139	/// @param key Property key.140	/// @return Property value bytes141	fn property(&self, token_id: uint256, key: string) -> Result<bytes> {142		let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;143		let key = <Vec<u8>>::from(key)144			.try_into()145			.map_err(|_| "key too long")?;146147		let props = <TokenProperties<T>>::get((self.id, token_id));148		let prop = props.get(&key).ok_or("key not found")?;149150		Ok(prop.to_vec())151	}152}153154#[derive(ToLog)]155pub enum ERC721Events {156	/// @dev This emits when ownership of any NFT changes by any mechanism.157	///  This event emits when NFTs are created (`from` == 0) and destroyed158	///  (`to` == 0). Exception: during contract creation, any number of NFTs159	///  may be created and assigned without emitting Transfer. At the time of160	///  any transfer, the approved address for that NFT (if any) is reset to none.161	Transfer {162		#[indexed]163		from: address,164		#[indexed]165		to: address,166		#[indexed]167		token_id: uint256,168	},169	/// @dev This emits when the approved address for an NFT is changed or170	///  reaffirmed. The zero address indicates there is no approved address.171	///  When a Transfer event emits, this also indicates that the approved172	///  address for that NFT (if any) is reset to none.173	Approval {174		#[indexed]175		owner: address,176		#[indexed]177		approved: address,178		#[indexed]179		token_id: uint256,180	},181	/// @dev This emits when an operator is enabled or disabled for an owner.182	///  The operator can manage all NFTs of the owner.183	#[allow(dead_code)]184	ApprovalForAll {185		#[indexed]186		owner: address,187		#[indexed]188		operator: address,189		approved: bool,190	},191}192193#[derive(ToLog)]194pub enum ERC721MintableEvents {195	#[allow(dead_code)]196	MintingFinished {},197}198199/// @title ERC-721 Non-Fungible Token Standard, optional metadata extension200/// @dev See https://eips.ethereum.org/EIPS/eip-721201#[solidity_interface(name = "ERC721Metadata")]202impl<T: Config> NonfungibleHandle<T> {203	/// @notice A descriptive name for a collection of NFTs in this contract204	fn name(&self) -> Result<string> {205		Ok(decode_utf16(self.name.iter().copied())206			.map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))207			.collect::<string>())208	}209210	/// @notice An abbreviated name for NFTs in this contract211	fn symbol(&self) -> Result<string> {212		Ok(string::from_utf8_lossy(&self.token_prefix).into())213	}214215	/// @notice A distinct Uniform Resource Identifier (URI) for a given asset.216	/// @dev Throws if `tokenId` is not a valid NFT. URIs are defined in RFC217	///  3986. The URI may point to a JSON file that conforms to the "ERC721218	///  Metadata JSON Schema".219	/// @return token's const_metadata220	#[solidity(rename_selector = "tokenURI")]221	fn token_uri(&self, token_id: uint256) -> Result<string> {222		let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;	223224		if let Ok(shema_name) = get_token_property(self, token_id, &schema_name_key()) {225			if shema_name != "ERC721" {226				return Ok("".into());227			}228		} else {229			return Ok("".into());230		}231232		if let Ok(url) = get_token_property(self, token_id, &u_key()) {233			if !url.is_empty() {234				return Ok(url);235			}236		}237238		if let Ok(base_uri) = get_token_property(self, token_id, &base_uri_key()) {239			if !base_uri.is_empty() {240				if let Ok(suffix) = get_token_property(self, token_id, &s_key()) {241					if !suffix.is_empty() {242						return Ok(base_uri + suffix.as_str());243					}244				}245246				return Ok(base_uri + token_id.to_string().as_str());247			}248		}249250		Ok("".into())251	}252}253254/// @title ERC-721 Non-Fungible Token Standard, optional enumeration extension255/// @dev See https://eips.ethereum.org/EIPS/eip-721256#[solidity_interface(name = "ERC721Enumerable")]257impl<T: Config> NonfungibleHandle<T> {258	/// @notice Enumerate valid NFTs259	/// @param index A counter less than `totalSupply()`260	/// @return The token identifier for the `index`th NFT,261	///  (sort order not specified)262	fn token_by_index(&self, index: uint256) -> Result<uint256> {263		Ok(index)264	}265266	/// @dev Not implemented267	fn token_of_owner_by_index(&self, _owner: address, _index: uint256) -> Result<uint256> {268		// TODO: Not implemetable269		Err("not implemented".into())270	}271272	/// @notice Count NFTs tracked by this contract273	/// @return A count of valid NFTs tracked by this contract, where each one of274	///  them has an assigned and queryable owner not equal to the zero address275	fn total_supply(&self) -> Result<uint256> {276		self.consume_store_reads(1)?;277		Ok(<Pallet<T>>::total_supply(self).into())278	}279}280281/// @title ERC-721 Non-Fungible Token Standard282/// @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md283#[solidity_interface(name = "ERC721", events(ERC721Events))]284impl<T: Config> NonfungibleHandle<T> {285	/// @notice Count all NFTs assigned to an owner286	/// @dev NFTs assigned to the zero address are considered invalid, and this287	///  function throws for queries about the zero address.288	/// @param owner An address for whom to query the balance289	/// @return The number of NFTs owned by `owner`, possibly zero290	fn balance_of(&self, owner: address) -> Result<uint256> {291		self.consume_store_reads(1)?;292		let owner = T::CrossAccountId::from_eth(owner);293		let balance = <AccountBalance<T>>::get((self.id, owner));294		Ok(balance.into())295	}296	/// @notice Find the owner of an NFT297	/// @dev NFTs assigned to zero address are considered invalid, and queries298	///  about them do throw.299	/// @param tokenId The identifier for an NFT300	/// @return The address of the owner of the NFT301	fn owner_of(&self, token_id: uint256) -> Result<address> {302		self.consume_store_reads(1)?;303		let token: TokenId = token_id.try_into()?;304		Ok(*<TokenData<T>>::get((self.id, token))305			.ok_or("token not found")?306			.owner307			.as_eth())308	}309	/// @dev Not implemented310	fn safe_transfer_from_with_data(311		&mut self,312		_from: address,313		_to: address,314		_token_id: uint256,315		_data: bytes,316		_value: value,317	) -> Result<void> {318		// TODO: Not implemetable319		Err("not implemented".into())320	}321	/// @dev Not implemented322	fn safe_transfer_from(323		&mut self,324		_from: address,325		_to: address,326		_token_id: uint256,327		_value: value,328	) -> Result<void> {329		// TODO: Not implemetable330		Err("not implemented".into())331	}332333	/// @notice Transfer ownership of an NFT -- THE CALLER IS RESPONSIBLE334	///  TO CONFIRM THAT `to` IS CAPABLE OF RECEIVING NFTS OR ELSE335	///  THEY MAY BE PERMANENTLY LOST336	/// @dev Throws unless `msg.sender` is the current owner or an authorized337	///  operator for this NFT. Throws if `from` is not the current owner. Throws338	///  if `to` is the zero address. Throws if `tokenId` is not a valid NFT.339	/// @param from The current owner of the NFT340	/// @param to The new owner341	/// @param tokenId The NFT to transfer342	/// @param _value Not used for an NFT343	#[weight(<SelfWeightOf<T>>::transfer_from())]344	fn transfer_from(345		&mut self,346		caller: caller,347		from: address,348		to: address,349		token_id: uint256,350		_value: value,351	) -> Result<void> {352		let caller = T::CrossAccountId::from_eth(caller);353		let from = T::CrossAccountId::from_eth(from);354		let to = T::CrossAccountId::from_eth(to);355		let token = token_id.try_into()?;356		let budget = self357			.recorder358			.weight_calls_budget(<StructureWeight<T>>::find_parent());359360		<Pallet<T>>::transfer_from(self, &caller, &from, &to, token, &budget)361			.map_err(dispatch_to_evm::<T>)?;362		Ok(())363	}364365	/// @notice Set or reaffirm the approved address for an NFT366	/// @dev The zero address indicates there is no approved address.367	/// @dev Throws unless `msg.sender` is the current NFT owner, or an authorized368	///  operator of the current owner.369	/// @param approved The new approved NFT controller370	/// @param tokenId The NFT to approve371	#[weight(<SelfWeightOf<T>>::approve())]372	fn approve(373		&mut self,374		caller: caller,375		approved: address,376		token_id: uint256,377		_value: value,378	) -> Result<void> {379		let caller = T::CrossAccountId::from_eth(caller);380		let approved = T::CrossAccountId::from_eth(approved);381		let token = token_id.try_into()?;382383		<Pallet<T>>::set_allowance(self, &caller, token, Some(&approved))384			.map_err(dispatch_to_evm::<T>)?;385		Ok(())386	}387388	/// @dev Not implemented389	fn set_approval_for_all(390		&mut self,391		_caller: caller,392		_operator: address,393		_approved: bool,394	) -> Result<void> {395		// TODO: Not implemetable396		Err("not implemented".into())397	}398399	/// @dev Not implemented400	fn get_approved(&self, _token_id: uint256) -> Result<address> {401		// TODO: Not implemetable402		Err("not implemented".into())403	}404405	/// @dev Not implemented406	fn is_approved_for_all(&self, _owner: address, _operator: address) -> Result<address> {407		// TODO: Not implemetable408		Err("not implemented".into())409	}410}411412/// @title ERC721 Token that can be irreversibly burned (destroyed).413#[solidity_interface(name = "ERC721Burnable")]414impl<T: Config> NonfungibleHandle<T> {415	/// @notice Burns a specific ERC721 token.416	/// @dev Throws unless `msg.sender` is the current NFT owner, or an authorized417	///  operator of the current owner.418	/// @param tokenId The NFT to approve419	#[weight(<SelfWeightOf<T>>::burn_item())]420	fn burn(&mut self, caller: caller, token_id: uint256) -> Result<void> {421		let caller = T::CrossAccountId::from_eth(caller);422		let token = token_id.try_into()?;423424		<Pallet<T>>::burn(self, &caller, token).map_err(dispatch_to_evm::<T>)?;425		Ok(())426	}427}428429/// @title ERC721 minting logic.430#[solidity_interface(name = "ERC721Mintable", events(ERC721MintableEvents))]431impl<T: Config> NonfungibleHandle<T> {432	fn minting_finished(&self) -> Result<bool> {433		Ok(false)434	}435436	/// @notice Function to mint token.437	/// @dev `tokenId` should be obtained with `nextTokenId` method,438	///  unlike standard, you can't specify it manually439	/// @param to The new owner440	/// @param tokenId ID of the minted NFT441	#[weight(<SelfWeightOf<T>>::create_item())]442	fn mint(&mut self, caller: caller, to: address, token_id: uint256) -> Result<bool> {443		let caller = T::CrossAccountId::from_eth(caller);444		let to = T::CrossAccountId::from_eth(to);445		let token_id: u32 = token_id.try_into()?;446		let budget = self447			.recorder448			.weight_calls_budget(<StructureWeight<T>>::find_parent());449450		if <TokensMinted<T>>::get(self.id)451			.checked_add(1)452			.ok_or("item id overflow")?453			!= token_id454		{455			return Err("item id should be next".into());456		}457458		<Pallet<T>>::create_item(459			self,460			&caller,461			CreateItemData::<T> {462				properties: BoundedVec::default(),463				owner: to,464			},465			&budget,466		)467		.map_err(dispatch_to_evm::<T>)?;468469		Ok(true)470	}471472	/// @notice Function to mint token with the given tokenUri.473	/// @dev `tokenId` should be obtained with `nextTokenId` method,474	///  unlike standard, you can't specify it manually475	/// @param to The new owner476	/// @param tokenId ID of the minted NFT477	/// @param tokenUri Token URI that would be stored in the NFT properties478	#[solidity(rename_selector = "mintWithTokenURI")]479	#[weight(<SelfWeightOf<T>>::create_item())]480	fn mint_with_token_uri(481		&mut self,482		caller: caller,483		to: address,484		token_id: uint256,485		token_uri: string,486	) -> Result<bool> {487		let key = token_uri_key();488		let permission = get_token_permission::<T>(self.id, &key)?;489		if !permission.collection_admin {490			return Err("Operation is not allowed".into());491		}492493		let caller = T::CrossAccountId::from_eth(caller);494		let to = T::CrossAccountId::from_eth(to);495		let token_id: u32 = token_id.try_into().map_err(|_| "amount overflow")?;496		let budget = self497			.recorder498			.weight_calls_budget(<StructureWeight<T>>::find_parent());499500		if <TokensMinted<T>>::get(self.id)501			.checked_add(1)502			.ok_or("item id overflow")?503			!= token_id504		{505			return Err("item id should be next".into());506		}507508		let mut properties = CollectionPropertiesVec::default();509		properties510			.try_push(Property {511				key,512				value: token_uri513					.into_bytes()514					.try_into()515					.map_err(|_| "token uri is too long")?,516			})517			.map_err(|e| Error::Revert(alloc::format!("Can't add property: {:?}", e)))?;518519		<Pallet<T>>::create_item(520			self,521			&caller,522			CreateItemData::<T> {523				properties,524				owner: to,525			},526			&budget,527		)528		.map_err(dispatch_to_evm::<T>)?;529		Ok(true)530	}531532	/// @dev Not implemented533	fn finish_minting(&mut self, _caller: caller) -> Result<bool> {534		Err("not implementable".into())535	}536}537538fn get_token_property<T: Config>(collection: &CollectionHandle<T>, token_id: u32, key: &up_data_structs::PropertyKey) -> Result<string> {539	collection.consume_store_reads(1)?;540	let properties = <TokenProperties<T>>::try_get((collection.id, token_id))541		.map_err(|_| Error::Revert("Token properties not found".into()))?;542	if let Some(property) = properties.get(key) {543		return Ok(string::from_utf8_lossy(property).into());544	}545546	Err("Property tokenURI not found".into())547}548549fn get_token_permission<T: Config>(550	collection_id: CollectionId,551	key: &PropertyKey,552) -> Result<PropertyPermission> {553	let token_property_permissions = CollectionPropertyPermissions::<T>::try_get(collection_id)554		.map_err(|_| Error::Revert("No permissions for collection".into()))?;555	let a = token_property_permissions556		.get(key)557		.map(|p| p.clone())558		.ok_or_else(|| Error::Revert("No permission".into()))?;559	Ok(a)560}561562fn has_token_permission<T: Config>(collection_id: CollectionId, key: &PropertyKey) -> bool {563	if let Ok(token_property_permissions) =564		CollectionPropertyPermissions::<T>::try_get(collection_id)565	{566		return token_property_permissions.contains_key(key);567	}568569	false570}571572/// @title Unique extensions for ERC721.573#[solidity_interface(name = "ERC721UniqueExtensions")]574impl<T: Config> NonfungibleHandle<T> {575	/// @notice Transfer ownership of an NFT576	/// @dev Throws unless `msg.sender` is the current owner. Throws if `to`577	///  is the zero address. Throws if `tokenId` is not a valid NFT.578	/// @param to The new owner579	/// @param tokenId The NFT to transfer580	/// @param _value Not used for an NFT581	#[weight(<SelfWeightOf<T>>::transfer())]582	fn transfer(583		&mut self,584		caller: caller,585		to: address,586		token_id: uint256,587		_value: value,588	) -> Result<void> {589		let caller = T::CrossAccountId::from_eth(caller);590		let to = T::CrossAccountId::from_eth(to);591		let token = token_id.try_into()?;592		let budget = self593			.recorder594			.weight_calls_budget(<StructureWeight<T>>::find_parent());595596		<Pallet<T>>::transfer(self, &caller, &to, token, &budget).map_err(dispatch_to_evm::<T>)?;597		Ok(())598	}599600	/// @notice Burns a specific ERC721 token.601	/// @dev Throws unless `msg.sender` is the current owner or an authorized602	///  operator for this NFT. Throws if `from` is not the current owner. Throws603	///  if `to` is the zero address. Throws if `tokenId` is not a valid NFT.604	/// @param from The current owner of the NFT605	/// @param tokenId The NFT to transfer606	/// @param _value Not used for an NFT607	#[weight(<SelfWeightOf<T>>::burn_from())]608	fn burn_from(609		&mut self,610		caller: caller,611		from: address,612		token_id: uint256,613		_value: value,614	) -> Result<void> {615		let caller = T::CrossAccountId::from_eth(caller);616		let from = T::CrossAccountId::from_eth(from);617		let token = token_id.try_into()?;618		let budget = self619			.recorder620			.weight_calls_budget(<StructureWeight<T>>::find_parent());621622		<Pallet<T>>::burn_from(self, &caller, &from, token, &budget)623			.map_err(dispatch_to_evm::<T>)?;624		Ok(())625	}626627	/// @notice Returns next free NFT ID.628	fn next_token_id(&self) -> Result<uint256> {629		self.consume_store_reads(1)?;630		Ok(<TokensMinted<T>>::get(self.id)631			.checked_add(1)632			.ok_or("item id overflow")?633			.into())634	}635636	/// @notice Function to mint multiple tokens.637	/// @dev `tokenIds` should be an array of consecutive numbers and first number638	///  should be obtained with `nextTokenId` method639	/// @param to The new owner640	/// @param tokenIds IDs of the minted NFTs641	#[weight(<SelfWeightOf<T>>::create_multiple_items(token_ids.len() as u32))]642	fn mint_bulk(&mut self, caller: caller, to: address, token_ids: Vec<uint256>) -> Result<bool> {643		let caller = T::CrossAccountId::from_eth(caller);644		let to = T::CrossAccountId::from_eth(to);645		let mut expected_index = <TokensMinted<T>>::get(self.id)646			.checked_add(1)647			.ok_or("item id overflow")?;648		let budget = self649			.recorder650			.weight_calls_budget(<StructureWeight<T>>::find_parent());651652		let total_tokens = token_ids.len();653		for id in token_ids.into_iter() {654			let id: u32 = id.try_into().map_err(|_| "token id overflow")?;655			if id != expected_index {656				return Err("item id should be next".into());657			}658			expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;659		}660		let data = (0..total_tokens)661			.map(|_| CreateItemData::<T> {662				properties: BoundedVec::default(),663				owner: to.clone(),664			})665			.collect();666667		<Pallet<T>>::create_multiple_items(self, &caller, data, &budget)668			.map_err(dispatch_to_evm::<T>)?;669		Ok(true)670	}671672	/// @notice Function to mint multiple tokens with the given tokenUris.673	/// @dev `tokenIds` is array of pairs of token ID and token URI. Token IDs should be consecutive674	///  numbers and first number should be obtained with `nextTokenId` method675	/// @param to The new owner676	/// @param tokens array of pairs of token ID and token URI for minted tokens677	#[solidity(rename_selector = "mintBulkWithTokenURI")]678	#[weight(<SelfWeightOf<T>>::create_multiple_items(tokens.len() as u32))]679	fn mint_bulk_with_token_uri(680		&mut self,681		caller: caller,682		to: address,683		tokens: Vec<(uint256, string)>,684	) -> Result<bool> {685		let key = token_uri_key();686		let caller = T::CrossAccountId::from_eth(caller);687		let to = T::CrossAccountId::from_eth(to);688		let mut expected_index = <TokensMinted<T>>::get(self.id)689			.checked_add(1)690			.ok_or("item id overflow")?;691		let budget = self692			.recorder693			.weight_calls_budget(<StructureWeight<T>>::find_parent());694695		let mut data = Vec::with_capacity(tokens.len());696		for (id, token_uri) in tokens {697			let id: u32 = id.try_into().map_err(|_| "token id overflow")?;698			if id != expected_index {699				return Err("item id should be next".into());700			}701			expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;702703			let mut properties = CollectionPropertiesVec::default();704			properties705				.try_push(Property {706					key: key.clone(),707					value: token_uri708						.into_bytes()709						.try_into()710						.map_err(|_| "token uri is too long")?,711				})712				.map_err(|e| Error::Revert(alloc::format!("Can't add property: {:?}", e)))?;713714			data.push(CreateItemData::<T> {715				properties,716				owner: to.clone(),717			});718		}719720		<Pallet<T>>::create_multiple_items(self, &caller, data, &budget)721			.map_err(dispatch_to_evm::<T>)?;722		Ok(true)723	}724}725726#[solidity_interface(727	name = "UniqueNFT",728	is(729		ERC721,730		ERC721Metadata,731		ERC721Enumerable,732		ERC721UniqueExtensions,733		ERC721Mintable,734		ERC721Burnable,735		via("CollectionHandle<T>", common_mut, Collection),736		TokenProperties,737	)738)]739impl<T: Config> NonfungibleHandle<T> where T::AccountId: From<[u8; 32]> {}740741// Not a tests, but code generators742generate_stubgen!(gen_impl, UniqueNFTCall<()>, true);743generate_stubgen!(gen_iface, UniqueNFTCall<()>, false);744745impl<T: Config> CommonEvmHandler for NonfungibleHandle<T>746where747	T::AccountId: From<[u8; 32]>,748{749	const CODE: &'static [u8] = include_bytes!("./stubs/UniqueNFT.raw");750751	fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {752		call::<T, UniqueNFTCall<T>, _, _>(handle, self)753	}754}
after · pallets/nonfungible/src/erc.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//! # Nonfungible Pallet EVM API18//!19//! Provides ERC-721 standart support implementation and EVM API for unique extensions for Nonfungible Pallet.20//! Method implementations are mostly doing parameter conversion and calling Nonfungible Pallet methods.2122extern crate alloc;23use core::{24	char::{REPLACEMENT_CHARACTER, decode_utf16},25	convert::TryInto,26};27use evm_coder::{ToLog, execution::*, generate_stubgen, solidity, solidity_interface, types::*, weight};28use frame_support::BoundedVec;29use up_data_structs::{30	TokenId, PropertyPermission, PropertyKeyPermission, Property, CollectionId, PropertyKey,31	CollectionPropertiesVec,32};33use pallet_evm_coder_substrate::dispatch_to_evm;34use sp_std::vec::Vec;35use pallet_common::{36	erc::{CommonEvmHandler, PrecompileResult, CollectionCall, static_property_key_value::*},37	CollectionHandle, CollectionPropertyPermissions,38};39use pallet_evm::{account::CrossAccountId, PrecompileHandle};40use pallet_evm_coder_substrate::call;41use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};42use alloc::string::ToString;4344use crate::{45	AccountBalance, Config, CreateItemData, NonfungibleHandle, Pallet, TokenData, TokensMinted,46	SelfWeightOf, weights::WeightInfo, TokenProperties,47};4849/// @title A contract that allows to set and delete token properties and change token property permissions.50#[solidity_interface(name = "TokenProperties")]51impl<T: Config> NonfungibleHandle<T> {52	/// @notice Set permissions for token property.53	/// @dev Throws error if `msg.sender` is not admin or owner of the collection.54	/// @param key Property key.55	/// @param is_mutable Permission to mutate property.56	/// @param collection_admin Permission to mutate property by collection admin if property is mutable.57	/// @param token_owner Permission to mutate property by token owner if property is mutable.58	fn set_token_property_permission(59		&mut self,60		caller: caller,61		key: string,62		is_mutable: bool,63		collection_admin: bool,64		token_owner: bool,65	) -> Result<()> {66		let caller = T::CrossAccountId::from_eth(caller);67		<Pallet<T>>::set_property_permission(68			self,69			&caller,70			PropertyKeyPermission {71				key: <Vec<u8>>::from(key)72					.try_into()73					.map_err(|_| "too long key")?,74				permission: PropertyPermission {75					mutable: is_mutable,76					collection_admin,77					token_owner,78				},79			},80		)81		.map_err(dispatch_to_evm::<T>)82	}8384	/// @notice Set token property value.85	/// @dev Throws error if `msg.sender` has no permission to edit the property.86	/// @param tokenId ID of the token.87	/// @param key Property key.88	/// @param value Property value.89	fn set_property(90		&mut self,91		caller: caller,92		token_id: uint256,93		key: string,94		value: bytes,95	) -> Result<()> {96		let caller = T::CrossAccountId::from_eth(caller);97		let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;98		let key = <Vec<u8>>::from(key)99			.try_into()100			.map_err(|_| "key too long")?;101		let value = value.try_into().map_err(|_| "value too long")?;102103		let nesting_budget = self104			.recorder105			.weight_calls_budget(<StructureWeight<T>>::find_parent());106107		<Pallet<T>>::set_token_property(108			self,109			&caller,110			TokenId(token_id),111			Property { key, value },112			&nesting_budget,113		)114		.map_err(dispatch_to_evm::<T>)115	}116117	/// @notice Delete token property value.118	/// @dev Throws error if `msg.sender` has no permission to edit the property.119	/// @param tokenId ID of the token.120	/// @param key Property key.121	fn delete_property(&mut self, token_id: uint256, caller: caller, key: string) -> Result<()> {122		let caller = T::CrossAccountId::from_eth(caller);123		let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;124		let key = <Vec<u8>>::from(key)125			.try_into()126			.map_err(|_| "key too long")?;127128		let nesting_budget = self129			.recorder130			.weight_calls_budget(<StructureWeight<T>>::find_parent());131132		<Pallet<T>>::delete_token_property(self, &caller, TokenId(token_id), key, &nesting_budget)133			.map_err(dispatch_to_evm::<T>)134	}135136	/// @notice Get token property value.137	/// @dev Throws error if key not found138	/// @param tokenId ID of the token.139	/// @param key Property key.140	/// @return Property value bytes141	fn property(&self, token_id: uint256, key: string) -> Result<bytes> {142		let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;143		let key = <Vec<u8>>::from(key)144			.try_into()145			.map_err(|_| "key too long")?;146147		let props = <TokenProperties<T>>::get((self.id, token_id));148		let prop = props.get(&key).ok_or("key not found")?;149150		Ok(prop.to_vec())151	}152}153154#[derive(ToLog)]155pub enum ERC721Events {156	/// @dev This emits when ownership of any NFT changes by any mechanism.157	///  This event emits when NFTs are created (`from` == 0) and destroyed158	///  (`to` == 0). Exception: during contract creation, any number of NFTs159	///  may be created and assigned without emitting Transfer. At the time of160	///  any transfer, the approved address for that NFT (if any) is reset to none.161	Transfer {162		#[indexed]163		from: address,164		#[indexed]165		to: address,166		#[indexed]167		token_id: uint256,168	},169	/// @dev This emits when the approved address for an NFT is changed or170	///  reaffirmed. The zero address indicates there is no approved address.171	///  When a Transfer event emits, this also indicates that the approved172	///  address for that NFT (if any) is reset to none.173	Approval {174		#[indexed]175		owner: address,176		#[indexed]177		approved: address,178		#[indexed]179		token_id: uint256,180	},181	/// @dev This emits when an operator is enabled or disabled for an owner.182	///  The operator can manage all NFTs of the owner.183	#[allow(dead_code)]184	ApprovalForAll {185		#[indexed]186		owner: address,187		#[indexed]188		operator: address,189		approved: bool,190	},191}192193#[derive(ToLog)]194pub enum ERC721MintableEvents {195	#[allow(dead_code)]196	MintingFinished {},197}198199/// @title ERC-721 Non-Fungible Token Standard, optional metadata extension200/// @dev See https://eips.ethereum.org/EIPS/eip-721201#[solidity_interface(name = "ERC721Metadata")]202impl<T: Config> NonfungibleHandle<T> {203	/// @notice A descriptive name for a collection of NFTs in this contract204	fn name(&self) -> Result<string> {205		Ok(decode_utf16(self.name.iter().copied())206			.map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))207			.collect::<string>())208	}209210	/// @notice An abbreviated name for NFTs in this contract211	fn symbol(&self) -> Result<string> {212		Ok(string::from_utf8_lossy(&self.token_prefix).into())213	}214215	/// @notice A distinct Uniform Resource Identifier (URI) for a given asset.216	/// @dev Throws if `tokenId` is not a valid NFT. URIs are defined in RFC217	///  3986. The URI may point to a JSON file that conforms to the "ERC721218	///  Metadata JSON Schema".219	/// @return token's const_metadata220	#[solidity(rename_selector = "tokenURI")]221	fn token_uri(&self, token_id: uint256) -> Result<string> {222		let is_erc721 = || {223			if let Some(shema_name) = pallet_common::Pallet::<T>::get_collection_property(self.id, &schema_name_key()) {224				let shema_name = shema_name.into_inner();225				shema_name == b"ERC721"226			} else {227				false228			}229		};230231		let token_id_u32: u32 = token_id.try_into().map_err(|_| "token id overflow")?;232233		if let Ok(url) = get_token_property(self, token_id_u32, &u_key()) {234			if !url.is_empty() {235				return Ok(url);236			}237		} else if !is_erc721() {238			return Err("tokenURI not set".into());239		}240241		if let Some(base_uri) = pallet_common::Pallet::<T>::get_collection_property(self.id, &base_uri_key()) {242			if !base_uri.is_empty() {243				let base_uri = string::from_utf8(base_uri.into_inner()).map_err(|e| {244					Error::Revert(alloc::format!(245						"Can not convert value \"baseURI\" to string with error \"{}\"",246						e247					))248				})?;249				if let Ok(suffix) = get_token_property(self, token_id_u32, &s_key()) {250					if !suffix.is_empty() {251						return Ok(base_uri + suffix.as_str());252					}253				}254255				return Ok(base_uri + token_id.to_string().as_str());256			}257		}258259		Ok("".into())260	}261}262263/// @title ERC-721 Non-Fungible Token Standard, optional enumeration extension264/// @dev See https://eips.ethereum.org/EIPS/eip-721265#[solidity_interface(name = "ERC721Enumerable")]266impl<T: Config> NonfungibleHandle<T> {267	/// @notice Enumerate valid NFTs268	/// @param index A counter less than `totalSupply()`269	/// @return The token identifier for the `index`th NFT,270	///  (sort order not specified)271	fn token_by_index(&self, index: uint256) -> Result<uint256> {272		Ok(index)273	}274275	/// @dev Not implemented276	fn token_of_owner_by_index(&self, _owner: address, _index: uint256) -> Result<uint256> {277		// TODO: Not implemetable278		Err("not implemented".into())279	}280281	/// @notice Count NFTs tracked by this contract282	/// @return A count of valid NFTs tracked by this contract, where each one of283	///  them has an assigned and queryable owner not equal to the zero address284	fn total_supply(&self) -> Result<uint256> {285		self.consume_store_reads(1)?;286		Ok(<Pallet<T>>::total_supply(self).into())287	}288}289290/// @title ERC-721 Non-Fungible Token Standard291/// @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md292#[solidity_interface(name = "ERC721", events(ERC721Events))]293impl<T: Config> NonfungibleHandle<T> {294	/// @notice Count all NFTs assigned to an owner295	/// @dev NFTs assigned to the zero address are considered invalid, and this296	///  function throws for queries about the zero address.297	/// @param owner An address for whom to query the balance298	/// @return The number of NFTs owned by `owner`, possibly zero299	fn balance_of(&self, owner: address) -> Result<uint256> {300		self.consume_store_reads(1)?;301		let owner = T::CrossAccountId::from_eth(owner);302		let balance = <AccountBalance<T>>::get((self.id, owner));303		Ok(balance.into())304	}305	/// @notice Find the owner of an NFT306	/// @dev NFTs assigned to zero address are considered invalid, and queries307	///  about them do throw.308	/// @param tokenId The identifier for an NFT309	/// @return The address of the owner of the NFT310	fn owner_of(&self, token_id: uint256) -> Result<address> {311		self.consume_store_reads(1)?;312		let token: TokenId = token_id.try_into()?;313		Ok(*<TokenData<T>>::get((self.id, token))314			.ok_or("token not found")?315			.owner316			.as_eth())317	}318	/// @dev Not implemented319	fn safe_transfer_from_with_data(320		&mut self,321		_from: address,322		_to: address,323		_token_id: uint256,324		_data: bytes,325		_value: value,326	) -> Result<void> {327		// TODO: Not implemetable328		Err("not implemented".into())329	}330	/// @dev Not implemented331	fn safe_transfer_from(332		&mut self,333		_from: address,334		_to: address,335		_token_id: uint256,336		_value: value,337	) -> Result<void> {338		// TODO: Not implemetable339		Err("not implemented".into())340	}341342	/// @notice Transfer ownership of an NFT -- THE CALLER IS RESPONSIBLE343	///  TO CONFIRM THAT `to` IS CAPABLE OF RECEIVING NFTS OR ELSE344	///  THEY MAY BE PERMANENTLY LOST345	/// @dev Throws unless `msg.sender` is the current owner or an authorized346	///  operator for this NFT. Throws if `from` is not the current owner. Throws347	///  if `to` is the zero address. Throws if `tokenId` is not a valid NFT.348	/// @param from The current owner of the NFT349	/// @param to The new owner350	/// @param tokenId The NFT to transfer351	/// @param _value Not used for an NFT352	#[weight(<SelfWeightOf<T>>::transfer_from())]353	fn transfer_from(354		&mut self,355		caller: caller,356		from: address,357		to: address,358		token_id: uint256,359		_value: value,360	) -> Result<void> {361		let caller = T::CrossAccountId::from_eth(caller);362		let from = T::CrossAccountId::from_eth(from);363		let to = T::CrossAccountId::from_eth(to);364		let token = token_id.try_into()?;365		let budget = self366			.recorder367			.weight_calls_budget(<StructureWeight<T>>::find_parent());368369		<Pallet<T>>::transfer_from(self, &caller, &from, &to, token, &budget)370			.map_err(dispatch_to_evm::<T>)?;371		Ok(())372	}373374	/// @notice Set or reaffirm the approved address for an NFT375	/// @dev The zero address indicates there is no approved address.376	/// @dev Throws unless `msg.sender` is the current NFT owner, or an authorized377	///  operator of the current owner.378	/// @param approved The new approved NFT controller379	/// @param tokenId The NFT to approve380	#[weight(<SelfWeightOf<T>>::approve())]381	fn approve(382		&mut self,383		caller: caller,384		approved: address,385		token_id: uint256,386		_value: value,387	) -> Result<void> {388		let caller = T::CrossAccountId::from_eth(caller);389		let approved = T::CrossAccountId::from_eth(approved);390		let token = token_id.try_into()?;391392		<Pallet<T>>::set_allowance(self, &caller, token, Some(&approved))393			.map_err(dispatch_to_evm::<T>)?;394		Ok(())395	}396397	/// @dev Not implemented398	fn set_approval_for_all(399		&mut self,400		_caller: caller,401		_operator: address,402		_approved: bool,403	) -> Result<void> {404		// TODO: Not implemetable405		Err("not implemented".into())406	}407408	/// @dev Not implemented409	fn get_approved(&self, _token_id: uint256) -> Result<address> {410		// TODO: Not implemetable411		Err("not implemented".into())412	}413414	/// @dev Not implemented415	fn is_approved_for_all(&self, _owner: address, _operator: address) -> Result<address> {416		// TODO: Not implemetable417		Err("not implemented".into())418	}419}420421/// @title ERC721 Token that can be irreversibly burned (destroyed).422#[solidity_interface(name = "ERC721Burnable")]423impl<T: Config> NonfungibleHandle<T> {424	/// @notice Burns a specific ERC721 token.425	/// @dev Throws unless `msg.sender` is the current NFT owner, or an authorized426	///  operator of the current owner.427	/// @param tokenId The NFT to approve428	#[weight(<SelfWeightOf<T>>::burn_item())]429	fn burn(&mut self, caller: caller, token_id: uint256) -> Result<void> {430		let caller = T::CrossAccountId::from_eth(caller);431		let token = token_id.try_into()?;432433		<Pallet<T>>::burn(self, &caller, token).map_err(dispatch_to_evm::<T>)?;434		Ok(())435	}436}437438/// @title ERC721 minting logic.439#[solidity_interface(name = "ERC721Mintable", events(ERC721MintableEvents))]440impl<T: Config> NonfungibleHandle<T> {441	fn minting_finished(&self) -> Result<bool> {442		Ok(false)443	}444445	/// @notice Function to mint token.446	/// @dev `tokenId` should be obtained with `nextTokenId` method,447	///  unlike standard, you can't specify it manually448	/// @param to The new owner449	/// @param tokenId ID of the minted NFT450	#[weight(<SelfWeightOf<T>>::create_item())]451	fn mint(&mut self, caller: caller, to: address, token_id: uint256) -> Result<bool> {452		let caller = T::CrossAccountId::from_eth(caller);453		let to = T::CrossAccountId::from_eth(to);454		let token_id: u32 = token_id.try_into()?;455		let budget = self456			.recorder457			.weight_calls_budget(<StructureWeight<T>>::find_parent());458459		if <TokensMinted<T>>::get(self.id)460			.checked_add(1)461			.ok_or("item id overflow")?462			!= token_id463		{464			return Err("item id should be next".into());465		}466467		<Pallet<T>>::create_item(468			self,469			&caller,470			CreateItemData::<T> {471				properties: BoundedVec::default(),472				owner: to,473			},474			&budget,475		)476		.map_err(dispatch_to_evm::<T>)?;477478		Ok(true)479	}480481	/// @notice Function to mint token with the given tokenUri.482	/// @dev `tokenId` should be obtained with `nextTokenId` method,483	///  unlike standard, you can't specify it manually484	/// @param to The new owner485	/// @param tokenId ID of the minted NFT486	/// @param tokenUri Token URI that would be stored in the NFT properties487	#[solidity(rename_selector = "mintWithTokenURI")]488	#[weight(<SelfWeightOf<T>>::create_item())]489	fn mint_with_token_uri(490		&mut self,491		caller: caller,492		to: address,493		token_id: uint256,494		token_uri: string,495	) -> Result<bool> {496		let key = u_key();497		let permission = get_token_permission::<T>(self.id, &key)?;498		if !permission.collection_admin {499			return Err("Operation is not allowed".into());500		}501502		let caller = T::CrossAccountId::from_eth(caller);503		let to = T::CrossAccountId::from_eth(to);504		let token_id: u32 = token_id.try_into().map_err(|_| "amount overflow")?;505		let budget = self506			.recorder507			.weight_calls_budget(<StructureWeight<T>>::find_parent());508509		if <TokensMinted<T>>::get(self.id)510			.checked_add(1)511			.ok_or("item id overflow")?512			!= token_id513		{514			return Err("item id should be next".into());515		}516517		let mut properties = CollectionPropertiesVec::default();518		properties519			.try_push(Property {520				key,521				value: token_uri522					.into_bytes()523					.try_into()524					.map_err(|_| "token uri is too long")?,525			})526			.map_err(|e| Error::Revert(alloc::format!("Can't add property: {:?}", e)))?;527528		<Pallet<T>>::create_item(529			self,530			&caller,531			CreateItemData::<T> {532				properties,533				owner: to,534			},535			&budget,536		)537		.map_err(dispatch_to_evm::<T>)?;538		Ok(true)539	}540541	/// @dev Not implemented542	fn finish_minting(&mut self, _caller: caller) -> Result<bool> {543		Err("not implementable".into())544	}545}546547fn get_token_property<T: Config>(548	collection: &CollectionHandle<T>,549	token_id: u32,550	key: &up_data_structs::PropertyKey,551) -> Result<string> {552	collection.consume_store_reads(1)?;553	let properties = <TokenProperties<T>>::try_get((collection.id, token_id))554		.map_err(|_| Error::Revert("Token properties not found".into()))?;555	if let Some(property) = properties.get(key) {556		return Ok(string::from_utf8_lossy(property).into());557	}558559	Err("Property tokenURI not found".into())560}561562fn get_token_permission<T: Config>(563	collection_id: CollectionId,564	key: &PropertyKey,565) -> Result<PropertyPermission> {566	let token_property_permissions = CollectionPropertyPermissions::<T>::try_get(collection_id)567		.map_err(|_| Error::Revert("No permissions for collection".into()))?;568	let a = token_property_permissions569		.get(key)570		.map(Clone::clone)571		.ok_or_else(|| {572			let key = string::from_utf8(key.clone().into_inner()).unwrap_or_default();573			Error::Revert(alloc::format!("No permission for key {}", key))574		})?;575	Ok(a)576}577578fn has_token_permission<T: Config>(collection_id: CollectionId, key: &PropertyKey) -> bool {579	if let Ok(token_property_permissions) =580		CollectionPropertyPermissions::<T>::try_get(collection_id)581	{582		return token_property_permissions.contains_key(key);583	}584585	false586}587588/// @title Unique extensions for ERC721.589#[solidity_interface(name = "ERC721UniqueExtensions")]590impl<T: Config> NonfungibleHandle<T> {591	/// @notice Transfer ownership of an NFT592	/// @dev Throws unless `msg.sender` is the current owner. Throws if `to`593	///  is the zero address. Throws if `tokenId` is not a valid NFT.594	/// @param to The new owner595	/// @param tokenId The NFT to transfer596	/// @param _value Not used for an NFT597	#[weight(<SelfWeightOf<T>>::transfer())]598	fn transfer(599		&mut self,600		caller: caller,601		to: address,602		token_id: uint256,603		_value: value,604	) -> Result<void> {605		let caller = T::CrossAccountId::from_eth(caller);606		let to = T::CrossAccountId::from_eth(to);607		let token = token_id.try_into()?;608		let budget = self609			.recorder610			.weight_calls_budget(<StructureWeight<T>>::find_parent());611612		<Pallet<T>>::transfer(self, &caller, &to, token, &budget).map_err(dispatch_to_evm::<T>)?;613		Ok(())614	}615616	/// @notice Burns a specific ERC721 token.617	/// @dev Throws unless `msg.sender` is the current owner or an authorized618	///  operator for this NFT. Throws if `from` is not the current owner. Throws619	///  if `to` is the zero address. Throws if `tokenId` is not a valid NFT.620	/// @param from The current owner of the NFT621	/// @param tokenId The NFT to transfer622	/// @param _value Not used for an NFT623	#[weight(<SelfWeightOf<T>>::burn_from())]624	fn burn_from(625		&mut self,626		caller: caller,627		from: address,628		token_id: uint256,629		_value: value,630	) -> Result<void> {631		let caller = T::CrossAccountId::from_eth(caller);632		let from = T::CrossAccountId::from_eth(from);633		let token = token_id.try_into()?;634		let budget = self635			.recorder636			.weight_calls_budget(<StructureWeight<T>>::find_parent());637638		<Pallet<T>>::burn_from(self, &caller, &from, token, &budget)639			.map_err(dispatch_to_evm::<T>)?;640		Ok(())641	}642643	/// @notice Returns next free NFT ID.644	fn next_token_id(&self) -> Result<uint256> {645		self.consume_store_reads(1)?;646		Ok(<TokensMinted<T>>::get(self.id)647			.checked_add(1)648			.ok_or("item id overflow")?649			.into())650	}651652	/// @notice Function to mint multiple tokens.653	/// @dev `tokenIds` should be an array of consecutive numbers and first number654	///  should be obtained with `nextTokenId` method655	/// @param to The new owner656	/// @param tokenIds IDs of the minted NFTs657	#[weight(<SelfWeightOf<T>>::create_multiple_items(token_ids.len() as u32))]658	fn mint_bulk(&mut self, caller: caller, to: address, token_ids: Vec<uint256>) -> Result<bool> {659		let caller = T::CrossAccountId::from_eth(caller);660		let to = T::CrossAccountId::from_eth(to);661		let mut expected_index = <TokensMinted<T>>::get(self.id)662			.checked_add(1)663			.ok_or("item id overflow")?;664		let budget = self665			.recorder666			.weight_calls_budget(<StructureWeight<T>>::find_parent());667668		let total_tokens = token_ids.len();669		for id in token_ids.into_iter() {670			let id: u32 = id.try_into().map_err(|_| "token id overflow")?;671			if id != expected_index {672				return Err("item id should be next".into());673			}674			expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;675		}676		let data = (0..total_tokens)677			.map(|_| CreateItemData::<T> {678				properties: BoundedVec::default(),679				owner: to.clone(),680			})681			.collect();682683		<Pallet<T>>::create_multiple_items(self, &caller, data, &budget)684			.map_err(dispatch_to_evm::<T>)?;685		Ok(true)686	}687688	/// @notice Function to mint multiple tokens with the given tokenUris.689	/// @dev `tokenIds` is array of pairs of token ID and token URI. Token IDs should be consecutive690	///  numbers and first number should be obtained with `nextTokenId` method691	/// @param to The new owner692	/// @param tokens array of pairs of token ID and token URI for minted tokens693	#[solidity(rename_selector = "mintBulkWithTokenURI")]694	#[weight(<SelfWeightOf<T>>::create_multiple_items(tokens.len() as u32))]695	fn mint_bulk_with_token_uri(696		&mut self,697		caller: caller,698		to: address,699		tokens: Vec<(uint256, string)>,700	) -> Result<bool> {701		let key = token_uri_key();702		let caller = T::CrossAccountId::from_eth(caller);703		let to = T::CrossAccountId::from_eth(to);704		let mut expected_index = <TokensMinted<T>>::get(self.id)705			.checked_add(1)706			.ok_or("item id overflow")?;707		let budget = self708			.recorder709			.weight_calls_budget(<StructureWeight<T>>::find_parent());710711		let mut data = Vec::with_capacity(tokens.len());712		for (id, token_uri) in tokens {713			let id: u32 = id.try_into().map_err(|_| "token id overflow")?;714			if id != expected_index {715				return Err("item id should be next".into());716			}717			expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;718719			let mut properties = CollectionPropertiesVec::default();720			properties721				.try_push(Property {722					key: key.clone(),723					value: token_uri724						.into_bytes()725						.try_into()726						.map_err(|_| "token uri is too long")?,727				})728				.map_err(|e| Error::Revert(alloc::format!("Can't add property: {:?}", e)))?;729730			data.push(CreateItemData::<T> {731				properties,732				owner: to.clone(),733			});734		}735736		<Pallet<T>>::create_multiple_items(self, &caller, data, &budget)737			.map_err(dispatch_to_evm::<T>)?;738		Ok(true)739	}740}741742#[solidity_interface(743	name = "UniqueNFT",744	is(745		ERC721,746		ERC721Metadata,747		ERC721Enumerable,748		ERC721UniqueExtensions,749		ERC721Mintable,750		ERC721Burnable,751		via("CollectionHandle<T>", common_mut, Collection),752		TokenProperties,753	)754)]755impl<T: Config> NonfungibleHandle<T> where T::AccountId: From<[u8; 32]> {}756757// Not a tests, but code generators758generate_stubgen!(gen_impl, UniqueNFTCall<()>, true);759generate_stubgen!(gen_iface, UniqueNFTCall<()>, false);760761impl<T: Config> CommonEvmHandler for NonfungibleHandle<T>762where763	T::AccountId: From<[u8; 32]>,764{765	const CODE: &'static [u8] = include_bytes!("./stubs/UniqueNFT.raw");766767	fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {768		call::<T, UniqueNFTCall<T>, _, _>(handle, self)769	}770}
modifiedpallets/unique/src/eth/mod.rsdiffbeforeafterboth
--- a/pallets/unique/src/eth/mod.rs
+++ b/pallets/unique/src/eth/mod.rs
@@ -95,29 +95,18 @@
 	let mut token_property_permissions =
 		up_data_structs::CollectionPropertiesPermissionsVec::default();
 
-	if add_properties {
-		token_property_permissions
-			.try_push(up_data_structs::PropertyKeyPermission {
-				key: token_uri_key(),
-				permission: up_data_structs::PropertyPermission {
-					mutable: true,
-					collection_admin: true,
-					token_owner: false,
-				},
-			})
-			.map_err(|e| Error::Revert(format!("{:?}", e)))?;
-
-		token_property_permissions
-			.try_push(up_data_structs::PropertyKeyPermission {
-				key: u_key(),
-				permission: up_data_structs::PropertyPermission {
-					mutable: false,
-					collection_admin: true,
-					token_owner: false,
-				},
-			})
-			.map_err(|e| Error::Revert(format!("{:?}", e)))?;
+	token_property_permissions
+		.try_push(up_data_structs::PropertyKeyPermission {
+			key: u_key(),
+			permission: up_data_structs::PropertyPermission {
+				mutable: false,
+				collection_admin: true,
+				token_owner: false,
+			},
+		})
+		.map_err(|e| Error::Revert(format!("{:?}", e)))?;
 
+	if add_properties {
 		token_property_permissions
 			.try_push(up_data_structs::PropertyKeyPermission {
 				key: s_key(),
@@ -176,7 +165,14 @@
 	) -> Result<address> {
 		let (caller, name, description, token_prefix, _base_uri_value) =
 			convert_data::<T>(caller, name, description, token_prefix, "".into())?;
-		let data = make_data::<T>(name, CollectionMode::NFT, description, token_prefix, Default::default(), false)?;
+		let data = make_data::<T>(
+			name,
+			CollectionMode::NFT,
+			description,
+			token_prefix,
+			Default::default(),
+			false,
+		)?;
 		let collection_id =
 			<pallet_nonfungible::Pallet<T>>::init_collection(caller.clone(), data, false)
 				.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;
@@ -197,9 +193,16 @@
 	) -> Result<address> {
 		let (caller, name, description, token_prefix, base_uri_value) =
 			convert_data::<T>(caller, name, description, token_prefix, base_uri)?;
-		let data = make_data::<T>(name, CollectionMode::NFT, description, token_prefix, base_uri_value, true)?;
+		let data = make_data::<T>(
+			name,
+			CollectionMode::NFT,
+			description,
+			token_prefix,
+			base_uri_value,
+			true,
+		)?;
 		let collection_id =
-			<pallet_nonfungible::Pallet<T>>::init_collection(caller.clone(), data, false)
+			<pallet_nonfungible::Pallet<T>>::init_collection(caller.clone(), data, true)
 				.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;
 
 		let address = pallet_common::eth::collection_id_to_address(collection_id);