12345678910111213141516171819202122extern crate alloc;23use core::{24 char::{REPLACEMENT_CHARACTER, decode_utf16},25 convert::TryInto,26};27use evm_coder::{28 abi::AbiType, ToLog, execution::*, generate_stubgen, solidity, solidity_interface, types::*,29 weight,30};31use frame_support::BoundedVec;32use up_data_structs::{33 TokenId, PropertyPermission, PropertyKeyPermission, Property, CollectionId, PropertyKey,34 CollectionPropertiesVec,35};36use pallet_evm_coder_substrate::dispatch_to_evm;37use sp_std::{vec::Vec, vec};38use pallet_common::{39 CollectionHandle, CollectionPropertyPermissions, CommonCollectionOperations,40 erc::{CommonEvmHandler, PrecompileResult, CollectionCall, static_property::key},41 eth::{self, TokenUri},42};43use pallet_evm::{account::CrossAccountId, PrecompileHandle};44use pallet_evm_coder_substrate::call;45use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};46use sp_core::{U256, Get};4748use crate::{49 AccountBalance, Config, CreateItemData, NonfungibleHandle, Pallet, TokenData, TokensMinted,50 SelfWeightOf, weights::WeightInfo, TokenProperties,51};525354#[solidity_interface(name = TokenProperties)]55impl<T: Config> NonfungibleHandle<T> {56 57 58 59 60 61 62 #[weight(<SelfWeightOf<T>>::set_token_property_permissions(1))]63 #[solidity(hide)]64 fn set_token_property_permission(65 &mut self,66 caller: Caller,67 key: String,68 is_mutable: bool,69 collection_admin: bool,70 token_owner: bool,71 ) -> Result<()> {72 let caller = T::CrossAccountId::from_eth(caller);73 <Pallet<T>>::set_token_property_permissions(74 self,75 &caller,76 vec![PropertyKeyPermission {77 key: <Vec<u8>>::from(key)78 .try_into()79 .map_err(|_| "too long key")?,80 permission: PropertyPermission {81 mutable: is_mutable,82 collection_admin,83 token_owner,84 },85 }],86 )87 .map_err(dispatch_to_evm::<T>)88 }8990 91 92 93 #[weight(<SelfWeightOf<T>>::set_token_property_permissions(permissions.len() as u32))]94 fn set_token_property_permissions(95 &mut self,96 caller: Caller,97 permissions: Vec<eth::TokenPropertyPermission>,98 ) -> Result<()> {99 let caller = T::CrossAccountId::from_eth(caller);100 let perms = eth::TokenPropertyPermission::into_property_key_permissions(permissions)?;101102 <Pallet<T>>::set_token_property_permissions(self, &caller, perms)103 .map_err(dispatch_to_evm::<T>)104 }105106 107 fn token_property_permissions(&self) -> Result<Vec<eth::TokenPropertyPermission>> {108 let perms = <Pallet<T>>::token_property_permission(self.id);109 Ok(perms110 .into_iter()111 .map(eth::TokenPropertyPermission::from)112 .collect())113 }114115 116 117 118 119 120 #[solidity(hide)]121 #[weight(<SelfWeightOf<T>>::set_token_properties(1))]122 fn set_property(123 &mut self,124 caller: Caller,125 token_id: U256,126 key: String,127 value: Bytes,128 ) -> Result<()> {129 let caller = T::CrossAccountId::from_eth(caller);130 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;131 let key = <Vec<u8>>::from(key)132 .try_into()133 .map_err(|_| "key too long")?;134 let value = value.0.try_into().map_err(|_| "value too long")?;135136 let nesting_budget = self137 .recorder138 .weight_calls_budget(<StructureWeight<T>>::find_parent());139140 <Pallet<T>>::set_token_property(141 self,142 &caller,143 TokenId(token_id),144 Property { key, value },145 &nesting_budget,146 )147 .map_err(dispatch_to_evm::<T>)148 }149150 151 152 153 154 #[weight(<SelfWeightOf<T>>::set_token_properties(properties.len() as u32))]155 fn set_properties(156 &mut self,157 caller: Caller,158 token_id: U256,159 properties: Vec<eth::Property>,160 ) -> Result<()> {161 let caller = T::CrossAccountId::from_eth(caller);162 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;163164 let nesting_budget = self165 .recorder166 .weight_calls_budget(<StructureWeight<T>>::find_parent());167168 let properties = properties169 .into_iter()170 .map(eth::Property::try_into)171 .collect::<Result<Vec<_>>>()?;172173 <Pallet<T>>::set_token_properties(174 self,175 &caller,176 TokenId(token_id),177 properties.into_iter(),178 false,179 &nesting_budget,180 )181 .map_err(dispatch_to_evm::<T>)182 }183184 185 186 187 188 #[solidity(hide)]189 #[weight(<SelfWeightOf<T>>::delete_token_properties(1))]190 fn delete_property(&mut self, token_id: U256, caller: Caller, key: String) -> Result<()> {191 let caller = T::CrossAccountId::from_eth(caller);192 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;193 let key = <Vec<u8>>::from(key)194 .try_into()195 .map_err(|_| "key too long")?;196197 let nesting_budget = self198 .recorder199 .weight_calls_budget(<StructureWeight<T>>::find_parent());200201 <Pallet<T>>::delete_token_property(self, &caller, TokenId(token_id), key, &nesting_budget)202 .map_err(dispatch_to_evm::<T>)203 }204205 206 207 208 209 #[weight(<SelfWeightOf<T>>::delete_token_properties(keys.len() as u32))]210 fn delete_properties(211 &mut self,212 token_id: U256,213 caller: Caller,214 keys: Vec<String>,215 ) -> Result<()> {216 let caller = T::CrossAccountId::from_eth(caller);217 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;218 let keys = keys219 .into_iter()220 .map(|k| Ok(<Vec<u8>>::from(k).try_into().map_err(|_| "key too long")?))221 .collect::<Result<Vec<_>>>()?;222223 let nesting_budget = self224 .recorder225 .weight_calls_budget(<StructureWeight<T>>::find_parent());226227 <Pallet<T>>::delete_token_properties(228 self,229 &caller,230 TokenId(token_id),231 keys.into_iter(),232 &nesting_budget,233 )234 .map_err(dispatch_to_evm::<T>)235 }236237 238 239 240 241 242 fn property(&self, token_id: U256, key: String) -> Result<Bytes> {243 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;244 let key = <Vec<u8>>::from(key)245 .try_into()246 .map_err(|_| "key too long")?;247248 let props = <TokenProperties<T>>::get((self.id, token_id));249 let prop = props.get(&key).ok_or("key not found")?;250251 Ok(prop.to_vec().into())252 }253}254255#[derive(ToLog)]256pub enum ERC721Events {257 258 259 260 261 262 Transfer {263 #[indexed]264 from: Address,265 #[indexed]266 to: Address,267 #[indexed]268 token_id: U256,269 },270 271 272 273 274 Approval {275 #[indexed]276 owner: Address,277 #[indexed]278 approved: Address,279 #[indexed]280 token_id: U256,281 },282 283 284 #[allow(dead_code)]285 ApprovalForAll {286 #[indexed]287 owner: Address,288 #[indexed]289 operator: Address,290 approved: bool,291 },292}293294295296#[solidity_interface(name = ERC721Metadata, expect_selector = 0x5b5e139f)]297impl<T: Config> NonfungibleHandle<T>298where299 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,300{301 302 303 #[solidity(hide, rename_selector = "name")]304 fn name_proxy(&self) -> Result<String> {305 self.name()306 }307308 309 310 #[solidity(hide, rename_selector = "symbol")]311 fn symbol_proxy(&self) -> Result<String> {312 self.symbol()313 }314315 316 317 318 319 320 321 322 323 324 #[solidity(rename_selector = "tokenURI")]325 fn token_uri(&self, token_id: U256) -> Result<String> {326 let token_id_u32: u32 = token_id.try_into().map_err(|_| "token id overflow")?;327328 match get_token_property(self, token_id_u32, &key::url()).as_deref() {329 Err(_) | Ok("") => (),330 Ok(url) => {331 return Ok(url.into());332 }333 };334335 let base_uri =336 pallet_common::Pallet::<T>::get_collection_property(self.id, &key::base_uri())337 .map(BoundedVec::into_inner)338 .map(String::from_utf8)339 .transpose()340 .map_err(|e| {341 Error::Revert(alloc::format!(342 "Can not convert value \"baseURI\" to string with error \"{}\"",343 e344 ))345 })?;346347 let base_uri = match base_uri.as_deref() {348 None | Some("") => {349 return Ok("".into());350 }351 Some(base_uri) => base_uri.into(),352 };353354 Ok(355 match get_token_property(self, token_id_u32, &key::suffix()).as_deref() {356 Err(_) | Ok("") => base_uri,357 Ok(suffix) => base_uri + suffix,358 },359 )360 }361}362363364365#[solidity_interface(name = ERC721Enumerable, expect_selector = 0x780e9d63)]366impl<T: Config> NonfungibleHandle<T> {367 368 369 370 371 fn token_by_index(&self, index: U256) -> Result<U256> {372 Ok(index)373 }374375 376 fn token_of_owner_by_index(&self, _owner: Address, _index: U256) -> Result<U256> {377 378 Err("not implemented".into())379 }380381 382 383 384 fn total_supply(&self) -> Result<U256> {385 self.consume_store_reads(1)?;386 Ok(<Pallet<T>>::total_supply(self).into())387 }388}389390391392#[solidity_interface(name = ERC721, events(ERC721Events), expect_selector = 0x80ac58cd)]393impl<T: Config> NonfungibleHandle<T> {394 395 396 397 398 399 fn balance_of(&self, owner: Address) -> Result<U256> {400 self.consume_store_reads(1)?;401 let owner = T::CrossAccountId::from_eth(owner);402 let balance = <AccountBalance<T>>::get((self.id, owner));403 Ok(balance.into())404 }405 406 407 408 409 410 fn owner_of(&self, token_id: U256) -> Result<Address> {411 self.consume_store_reads(1)?;412 let token: TokenId = token_id.try_into()?;413 Ok(*<TokenData<T>>::get((self.id, token))414 .ok_or("token not found")?415 .owner416 .as_eth())417 }418 419 #[solidity(rename_selector = "safeTransferFrom")]420 fn safe_transfer_from_with_data(421 &mut self,422 _from: Address,423 _to: Address,424 _token_id: U256,425 _data: Bytes,426 ) -> Result<()> {427 428 Err("not implemented".into())429 }430 431 fn safe_transfer_from(&mut self, _from: Address, _to: Address, _token_id: U256) -> Result<()> {432 433 Err("not implemented".into())434 }435436 437 438 439 440 441 442 443 444 445 #[weight(<SelfWeightOf<T>>::transfer_from())]446 fn transfer_from(447 &mut self,448 caller: Caller,449 from: Address,450 to: Address,451 token_id: U256,452 ) -> Result<()> {453 let caller = T::CrossAccountId::from_eth(caller);454 let from = T::CrossAccountId::from_eth(from);455 let to = T::CrossAccountId::from_eth(to);456 let token = token_id.try_into()?;457 let budget = self458 .recorder459 .weight_calls_budget(<StructureWeight<T>>::find_parent());460461 <Pallet<T>>::transfer_from(self, &caller, &from, &to, token, &budget)462 .map_err(dispatch_to_evm::<T>)?;463 Ok(())464 }465466 467 468 469 470 471 472 #[weight(<SelfWeightOf<T>>::approve())]473 fn approve(&mut self, caller: Caller, approved: Address, token_id: U256) -> Result<()> {474 let caller = T::CrossAccountId::from_eth(caller);475 let approved = T::CrossAccountId::from_eth(approved);476 let token = token_id.try_into()?;477478 <Pallet<T>>::set_allowance(self, &caller, token, Some(&approved))479 .map_err(dispatch_to_evm::<T>)?;480 Ok(())481 }482483 484 485 486 487 #[weight(<SelfWeightOf<T>>::set_allowance_for_all())]488 fn set_approval_for_all(489 &mut self,490 caller: Caller,491 operator: Address,492 approved: bool,493 ) -> Result<()> {494 let caller = T::CrossAccountId::from_eth(caller);495 let operator = T::CrossAccountId::from_eth(operator);496497 <Pallet<T>>::set_allowance_for_all(self, &caller, &operator, approved)498 .map_err(dispatch_to_evm::<T>)?;499 Ok(())500 }501502 503 fn get_approved(&self, _token_id: U256) -> Result<Address> {504 505 Err("not implemented".into())506 }507508 509 #[weight(<SelfWeightOf<T>>::allowance_for_all())]510 fn is_approved_for_all(&self, owner: Address, operator: Address) -> Result<bool> {511 let owner = T::CrossAccountId::from_eth(owner);512 let operator = T::CrossAccountId::from_eth(operator);513514 Ok(<Pallet<T>>::allowance_for_all(self, &owner, &operator))515 }516}517518519#[solidity_interface(name = ERC721Burnable)]520impl<T: Config> NonfungibleHandle<T> {521 522 523 524 525 #[weight(<SelfWeightOf<T>>::burn_item())]526 fn burn(&mut self, caller: Caller, token_id: U256) -> Result<()> {527 let caller = T::CrossAccountId::from_eth(caller);528 let token = token_id.try_into()?;529530 <Pallet<T>>::burn(self, &caller, token).map_err(dispatch_to_evm::<T>)?;531 Ok(())532 }533}534535536#[solidity_interface(name = ERC721UniqueMintable)]537impl<T: Config> NonfungibleHandle<T> {538 539 540 541 #[weight(<SelfWeightOf<T>>::create_item())]542 fn mint(&mut self, caller: Caller, to: Address) -> Result<U256> {543 let token_id: U256 = <TokensMinted<T>>::get(self.id)544 .checked_add(1)545 .ok_or("item id overflow")?546 .into();547 self.mint_check_id(caller, to, token_id)?;548 Ok(token_id)549 }550551 552 553 554 555 556 #[solidity(hide, rename_selector = "mint")]557 #[weight(<SelfWeightOf<T>>::create_item())]558 fn mint_check_id(&mut self, caller: Caller, to: Address, token_id: U256) -> Result<bool> {559 let caller = T::CrossAccountId::from_eth(caller);560 let to = T::CrossAccountId::from_eth(to);561 let token_id: u32 = token_id.try_into()?;562 let budget = self563 .recorder564 .weight_calls_budget(<StructureWeight<T>>::find_parent());565566 if <TokensMinted<T>>::get(self.id)567 .checked_add(1)568 .ok_or("item id overflow")?569 != token_id570 {571 return Err("item id should be next".into());572 }573574 <Pallet<T>>::create_item(575 self,576 &caller,577 CreateItemData::<T> {578 properties: BoundedVec::default(),579 owner: to,580 },581 &budget,582 )583 .map_err(dispatch_to_evm::<T>)?;584585 Ok(true)586 }587588 589 590 591 592 #[solidity(rename_selector = "mintWithTokenURI")]593 #[weight(<SelfWeightOf<T>>::create_item() + <SelfWeightOf<T>>::set_token_properties(1))]594 fn mint_with_token_uri(595 &mut self,596 caller: Caller,597 to: Address,598 token_uri: String,599 ) -> Result<U256> {600 let token_id: U256 = <TokensMinted<T>>::get(self.id)601 .checked_add(1)602 .ok_or("item id overflow")?603 .into();604 self.mint_with_token_uri_check_id(caller, to, token_id, token_uri)?;605 Ok(token_id)606 }607608 609 610 611 612 613 614 #[solidity(hide, rename_selector = "mintWithTokenURI")]615 #[weight(<SelfWeightOf<T>>::create_item() + <SelfWeightOf<T>>::set_token_properties(1))]616 fn mint_with_token_uri_check_id(617 &mut self,618 caller: Caller,619 to: Address,620 token_id: U256,621 token_uri: String,622 ) -> Result<bool> {623 let key = key::url();624 let permission = get_token_permission::<T>(self.id, &key)?;625 if !permission.collection_admin {626 return Err("Operation is not allowed".into());627 }628629 let caller = T::CrossAccountId::from_eth(caller);630 let to = T::CrossAccountId::from_eth(to);631 let token_id: u32 = token_id.try_into().map_err(|_| "amount overflow")?;632 let budget = self633 .recorder634 .weight_calls_budget(<StructureWeight<T>>::find_parent());635636 if <TokensMinted<T>>::get(self.id)637 .checked_add(1)638 .ok_or("item id overflow")?639 != token_id640 {641 return Err("item id should be next".into());642 }643644 let mut properties = CollectionPropertiesVec::default();645 properties646 .try_push(Property {647 key,648 value: token_uri649 .into_bytes()650 .try_into()651 .map_err(|_| "token uri is too long")?,652 })653 .map_err(|e| Error::Revert(alloc::format!("Can't add property: {:?}", e)))?;654655 <Pallet<T>>::create_item(656 self,657 &caller,658 CreateItemData::<T> {659 properties,660 owner: to,661 },662 &budget,663 )664 .map_err(dispatch_to_evm::<T>)?;665 Ok(true)666 }667}668669fn get_token_property<T: Config>(670 collection: &CollectionHandle<T>,671 token_id: u32,672 key: &up_data_structs::PropertyKey,673) -> Result<String> {674 collection.consume_store_reads(1)?;675 let properties = <TokenProperties<T>>::try_get((collection.id, token_id))676 .map_err(|_| Error::Revert("Token properties not found".into()))?;677 if let Some(property) = properties.get(key) {678 return Ok(String::from_utf8_lossy(property).into());679 }680681 Err("Property tokenURI not found".into())682}683684fn get_token_permission<T: Config>(685 collection_id: CollectionId,686 key: &PropertyKey,687) -> Result<PropertyPermission> {688 let token_property_permissions = CollectionPropertyPermissions::<T>::try_get(collection_id)689 .map_err(|_| Error::Revert("No permissions for collection".into()))?;690 let a = token_property_permissions691 .get(key)692 .map(Clone::clone)693 .ok_or_else(|| {694 let key = String::from_utf8(key.clone().into_inner()).unwrap_or_default();695 Error::Revert(alloc::format!("No permission for key {}", key))696 })?;697 Ok(a)698}699700701#[solidity_interface(name = ERC721UniqueExtensions)]702impl<T: Config> NonfungibleHandle<T>703where704 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,705{706 707 fn name(&self) -> Result<String> {708 Ok(decode_utf16(self.name.iter().copied())709 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))710 .collect::<String>())711 }712713 714 fn symbol(&self) -> Result<String> {715 Ok(String::from_utf8_lossy(&self.token_prefix).into())716 }717718 719 fn description(&self) -> Result<String> {720 Ok(decode_utf16(self.description.iter().copied())721 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))722 .collect::<String>())723 }724725 726 727 728 fn cross_owner_of(&self, token_id: U256) -> Result<eth::CrossAddress> {729 Self::token_owner(&self, token_id.try_into()?)730 .map(|o| eth::CrossAddress::from_sub_cross_account::<T>(&o))731 .map_err(|_| Error::Revert("token not found".into()))732 }733734 735 736 737 738 739 fn properties(&self, token_id: U256, keys: Vec<String>) -> Result<Vec<eth::Property>> {740 let keys = keys741 .into_iter()742 .map(|key| {743 <Vec<u8>>::from(key)744 .try_into()745 .map_err(|_| Error::Revert("key too large".into()))746 })747 .collect::<Result<Vec<_>>>()?;748749 <Self as CommonCollectionOperations<T>>::token_properties(750 &self,751 token_id.try_into()?,752 if keys.is_empty() { None } else { Some(keys) },753 )754 .into_iter()755 .map(eth::Property::try_from)756 .collect::<Result<Vec<_>>>()757 }758759 760 761 762 763 764 765 #[weight(<SelfWeightOf<T>>::approve())]766 fn approve_cross(767 &mut self,768 caller: Caller,769 approved: eth::CrossAddress,770 token_id: U256,771 ) -> Result<()> {772 let caller = T::CrossAccountId::from_eth(caller);773 let approved = approved.into_sub_cross_account::<T>()?;774 let token = token_id.try_into()?;775776 <Pallet<T>>::set_allowance(self, &caller, token, Some(&approved))777 .map_err(dispatch_to_evm::<T>)?;778 Ok(())779 }780781 782 783 784 785 786 #[weight(<SelfWeightOf<T>>::transfer())]787 fn transfer(&mut self, caller: Caller, to: Address, token_id: U256) -> Result<()> {788 let caller = T::CrossAccountId::from_eth(caller);789 let to = T::CrossAccountId::from_eth(to);790 let token = token_id.try_into()?;791 let budget = self792 .recorder793 .weight_calls_budget(<StructureWeight<T>>::find_parent());794795 <Pallet<T>>::transfer(self, &caller, &to, token, &budget).map_err(dispatch_to_evm::<T>)?;796 Ok(())797 }798799 800 801 802 803 804 #[weight(<SelfWeightOf<T>>::transfer())]805 fn transfer_cross(806 &mut self,807 caller: Caller,808 to: eth::CrossAddress,809 token_id: U256,810 ) -> Result<()> {811 let caller = T::CrossAccountId::from_eth(caller);812 let to = to.into_sub_cross_account::<T>()?;813 let token = token_id.try_into()?;814 let budget = self815 .recorder816 .weight_calls_budget(<StructureWeight<T>>::find_parent());817818 <Pallet<T>>::transfer(self, &caller, &to, token, &budget).map_err(dispatch_to_evm::<T>)?;819 Ok(())820 }821822 823 824 825 826 827 828 #[weight(<SelfWeightOf<T>>::transfer())]829 fn transfer_from_cross(830 &mut self,831 caller: Caller,832 from: eth::CrossAddress,833 to: eth::CrossAddress,834 token_id: U256,835 ) -> Result<()> {836 let caller = T::CrossAccountId::from_eth(caller);837 let from = from.into_sub_cross_account::<T>()?;838 let to = to.into_sub_cross_account::<T>()?;839 let token_id = token_id.try_into()?;840 let budget = self841 .recorder842 .weight_calls_budget(<StructureWeight<T>>::find_parent());843 Pallet::<T>::transfer_from(self, &caller, &from, &to, token_id, &budget)844 .map_err(dispatch_to_evm::<T>)?;845 Ok(())846 }847848 849 850 851 852 853 854 #[solidity(hide)]855 #[weight(<SelfWeightOf<T>>::burn_from())]856 fn burn_from(&mut self, caller: Caller, from: Address, token_id: U256) -> Result<()> {857 let caller = T::CrossAccountId::from_eth(caller);858 let from = T::CrossAccountId::from_eth(from);859 let token = token_id.try_into()?;860 let budget = self861 .recorder862 .weight_calls_budget(<StructureWeight<T>>::find_parent());863864 <Pallet<T>>::burn_from(self, &caller, &from, token, &budget)865 .map_err(dispatch_to_evm::<T>)?;866 Ok(())867 }868869 870 871 872 873 874 875 #[weight(<SelfWeightOf<T>>::burn_from())]876 fn burn_from_cross(877 &mut self,878 caller: Caller,879 from: eth::CrossAddress,880 token_id: U256,881 ) -> Result<()> {882 let caller = T::CrossAccountId::from_eth(caller);883 let from = from.into_sub_cross_account::<T>()?;884 let token = token_id.try_into()?;885 let budget = self886 .recorder887 .weight_calls_budget(<StructureWeight<T>>::find_parent());888889 <Pallet<T>>::burn_from(self, &caller, &from, token, &budget)890 .map_err(dispatch_to_evm::<T>)?;891 Ok(())892 }893894 895 fn next_token_id(&self) -> Result<U256> {896 self.consume_store_reads(1)?;897 Ok(<TokensMinted<T>>::get(self.id)898 .checked_add(1)899 .ok_or("item id overflow")?900 .into())901 }902903 904 905 906 907 908 #[solidity(hide)]909 #[weight(<SelfWeightOf<T>>::create_multiple_items(token_ids.len() as u32))]910 fn mint_bulk(&mut self, caller: Caller, to: Address, token_ids: Vec<U256>) -> Result<bool> {911 let caller = T::CrossAccountId::from_eth(caller);912 let to = T::CrossAccountId::from_eth(to);913 let mut expected_index = <TokensMinted<T>>::get(self.id)914 .checked_add(1)915 .ok_or("item id overflow")?;916 let budget = self917 .recorder918 .weight_calls_budget(<StructureWeight<T>>::find_parent());919920 let total_tokens = token_ids.len();921 for id in token_ids.into_iter() {922 let id: u32 = id.try_into().map_err(|_| "token id overflow")?;923 if id != expected_index {924 return Err("item id should be next".into());925 }926 expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;927 }928 let data = (0..total_tokens)929 .map(|_| CreateItemData::<T> {930 properties: BoundedVec::default(),931 owner: to.clone(),932 })933 .collect();934935 <Pallet<T>>::create_multiple_items(self, &caller, data, &budget)936 .map_err(dispatch_to_evm::<T>)?;937 Ok(true)938 }939940 941 942 943 944 945 #[solidity(hide, rename_selector = "mintBulkWithTokenURI")]946 #[weight(<SelfWeightOf<T>>::create_multiple_items(tokens.len() as u32) + <SelfWeightOf<T>>::set_token_properties(tokens.len() as u32))]947 fn mint_bulk_with_token_uri(948 &mut self,949 caller: Caller,950 to: Address,951 tokens: Vec<TokenUri>,952 ) -> Result<bool> {953 let key = key::url();954 let caller = T::CrossAccountId::from_eth(caller);955 let to = T::CrossAccountId::from_eth(to);956 let mut expected_index = <TokensMinted<T>>::get(self.id)957 .checked_add(1)958 .ok_or("item id overflow")?;959 let budget = self960 .recorder961 .weight_calls_budget(<StructureWeight<T>>::find_parent());962963 let mut data = Vec::with_capacity(tokens.len());964 for TokenUri { id, uri } in tokens {965 let id: u32 = id.try_into().map_err(|_| "token id overflow")?;966 if id != expected_index {967 return Err("item id should be next".into());968 }969 expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;970971 let mut properties = CollectionPropertiesVec::default();972 properties973 .try_push(Property {974 key: key.clone(),975 value: uri976 .into_bytes()977 .try_into()978 .map_err(|_| "token uri is too long")?,979 })980 .map_err(|e| Error::Revert(alloc::format!("Can't add property: {:?}", e)))?;981982 data.push(CreateItemData::<T> {983 properties,984 owner: to.clone(),985 });986 }987988 <Pallet<T>>::create_multiple_items(self, &caller, data, &budget)989 .map_err(dispatch_to_evm::<T>)?;990 Ok(true)991 }992993 994 995 996 997 #[weight(<SelfWeightOf<T>>::create_item() + <SelfWeightOf<T>>::set_token_properties(properties.len() as u32))]998 fn mint_cross(999 &mut self,1000 caller: Caller,1001 to: eth::CrossAddress,1002 properties: Vec<eth::Property>,1003 ) -> Result<U256> {1004 let token_id = <TokensMinted<T>>::get(self.id)1005 .checked_add(1)1006 .ok_or("item id overflow")?;10071008 let to = to.into_sub_cross_account::<T>()?;10091010 let properties = properties1011 .into_iter()1012 .map(eth::Property::try_into)1013 .collect::<Result<Vec<_>>>()?1014 .try_into()1015 .map_err(|_| Error::Revert(alloc::format!("too many properties")))?;10161017 let caller = T::CrossAccountId::from_eth(caller);10181019 let budget = self1020 .recorder1021 .weight_calls_budget(<StructureWeight<T>>::find_parent());10221023 <Pallet<T>>::create_item(1024 self,1025 &caller,1026 CreateItemData::<T> {1027 properties,1028 owner: to,1029 },1030 &budget,1031 )1032 .map_err(dispatch_to_evm::<T>)?;10331034 Ok(token_id.into())1035 }10361037 1038 fn collection_helper_address(&self) -> Result<Address> {1039 Ok(T::ContractAddress::get())1040 }1041}10421043#[solidity_interface(1044 name = UniqueNFT,1045 is(1046 ERC721,1047 ERC721Enumerable,1048 ERC721UniqueExtensions,1049 ERC721UniqueMintable,1050 ERC721Burnable,1051 ERC721Metadata(if(this.flags.erc721metadata)),1052 Collection(via(common_mut returns CollectionHandle<T>)),1053 TokenProperties,1054 )1055)]1056impl<T: Config> NonfungibleHandle<T> where T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]> {}105710581059generate_stubgen!(gen_impl, UniqueNFTCall<()>, true);1060generate_stubgen!(gen_iface, UniqueNFTCall<()>, false);10611062impl<T: Config> CommonEvmHandler for NonfungibleHandle<T>1063where1064 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,1065{1066 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueNFT.raw");10671068 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {1069 call::<T, UniqueNFTCall<T>, _, _>(handle, self)1070 }1071}