12345678910111213141516171819202122extern crate alloc;23use core::{24 char::{REPLACEMENT_CHARACTER, decode_utf16},25 convert::TryInto,26};27use evm_coder::{abi::AbiType, ToLog, generate_stubgen, solidity_interface, types::*};28use frame_support::BoundedVec;29use up_data_structs::{30 TokenId, PropertyPermission, PropertyKeyPermission, Property, CollectionId, PropertyKey,31 CollectionPropertiesVec,32};33use pallet_evm_coder_substrate::{34 dispatch_to_evm, frontier_contract,35 execution::{Result, PreDispatch, Error},36};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 TokenProperties, SelfWeightOf, weights::WeightInfo,51};5253frontier_contract! {54 macro_rules! NonfungibleHandle_result {...}55 impl<T: Config> Contract for NonfungibleHandle<T> {...}56}575859#[solidity_interface(name = TokenProperties, enum(derive(PreDispatch)), enum_attr(weight))]60impl<T: Config> NonfungibleHandle<T> {61 62 63 64 65 66 67 #[solidity(hide)]68 #[weight(<SelfWeightOf<T>>::set_token_property_permissions(1))]69 fn set_token_property_permission(70 &mut self,71 caller: Caller,72 key: String,73 is_mutable: bool,74 collection_admin: bool,75 token_owner: bool,76 ) -> Result<()> {77 let caller = T::CrossAccountId::from_eth(caller);78 <Pallet<T>>::set_token_property_permissions(79 self,80 &caller,81 vec![PropertyKeyPermission {82 key: <Vec<u8>>::from(key)83 .try_into()84 .map_err(|_| "too long key")?,85 permission: PropertyPermission {86 mutable: is_mutable,87 collection_admin,88 token_owner,89 },90 }],91 )92 .map_err(dispatch_to_evm::<T>)93 }9495 96 97 98 #[weight(<SelfWeightOf<T>>::set_token_property_permissions(permissions.len() as u32))]99 fn set_token_property_permissions(100 &mut self,101 caller: Caller,102 permissions: Vec<eth::TokenPropertyPermission>,103 ) -> Result<()> {104 let caller = T::CrossAccountId::from_eth(caller);105 let perms = eth::TokenPropertyPermission::into_property_key_permissions(permissions)?;106107 <Pallet<T>>::set_token_property_permissions(self, &caller, perms)108 .map_err(dispatch_to_evm::<T>)109 }110111 112 fn token_property_permissions(&self) -> Result<Vec<eth::TokenPropertyPermission>> {113 let perms = <Pallet<T>>::token_property_permission(self.id);114 Ok(perms115 .into_iter()116 .map(eth::TokenPropertyPermission::from)117 .collect())118 }119120 121 122 123 124 125 #[solidity(hide)]126 #[weight(<SelfWeightOf<T>>::set_token_properties(1))]127 fn set_property(128 &mut self,129 caller: Caller,130 token_id: U256,131 key: String,132 value: Bytes,133 ) -> Result<()> {134 let caller = T::CrossAccountId::from_eth(caller);135 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;136 let key = <Vec<u8>>::from(key)137 .try_into()138 .map_err(|_| "key too long")?;139 let value = value.0.try_into().map_err(|_| "value too long")?;140141 let nesting_budget = self142 .recorder143 .weight_calls_budget(<StructureWeight<T>>::find_parent());144145 <Pallet<T>>::set_token_property(146 self,147 &caller,148 TokenId(token_id),149 Property { key, value },150 &nesting_budget,151 )152 .map_err(dispatch_to_evm::<T>)153 }154155 156 157 158 159 #[weight(<SelfWeightOf<T>>::set_token_properties(properties.len() as u32))]160 fn set_properties(161 &mut self,162 caller: Caller,163 token_id: U256,164 properties: Vec<eth::Property>,165 ) -> Result<()> {166 let caller = T::CrossAccountId::from_eth(caller);167 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;168169 let nesting_budget = self170 .recorder171 .weight_calls_budget(<StructureWeight<T>>::find_parent());172173 let properties = properties174 .into_iter()175 .map(eth::Property::try_into)176 .collect::<Result<Vec<_>>>()?;177178 <Pallet<T>>::set_token_properties(179 self,180 &caller,181 TokenId(token_id),182 properties.into_iter(),183 false,184 &nesting_budget,185 )186 .map_err(dispatch_to_evm::<T>)187 }188189 190 191 192 193 #[solidity(hide)]194 #[weight(<SelfWeightOf<T>>::delete_token_properties(1))]195 fn delete_property(&mut self, token_id: U256, caller: Caller, key: String) -> Result<()> {196 let caller = T::CrossAccountId::from_eth(caller);197 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;198 let key = <Vec<u8>>::from(key)199 .try_into()200 .map_err(|_| "key too long")?;201202 let nesting_budget = self203 .recorder204 .weight_calls_budget(<StructureWeight<T>>::find_parent());205206 <Pallet<T>>::delete_token_property(self, &caller, TokenId(token_id), key, &nesting_budget)207 .map_err(dispatch_to_evm::<T>)208 }209210 211 212 213 214 #[weight(<SelfWeightOf<T>>::delete_token_properties(keys.len() as u32))]215 fn delete_properties(216 &mut self,217 token_id: U256,218 caller: Caller,219 keys: Vec<String>,220 ) -> Result<()> {221 let caller = T::CrossAccountId::from_eth(caller);222 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;223 let keys = keys224 .into_iter()225 .map(|k| Ok(<Vec<u8>>::from(k).try_into().map_err(|_| "key too long")?))226 .collect::<Result<Vec<_>>>()?;227228 let nesting_budget = self229 .recorder230 .weight_calls_budget(<StructureWeight<T>>::find_parent());231232 <Pallet<T>>::delete_token_properties(233 self,234 &caller,235 TokenId(token_id),236 keys.into_iter(),237 &nesting_budget,238 )239 .map_err(dispatch_to_evm::<T>)240 }241242 243 244 245 246 247 fn property(&self, token_id: U256, key: String) -> Result<Bytes> {248 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;249 let key = <Vec<u8>>::from(key)250 .try_into()251 .map_err(|_| "key too long")?;252253 let props = <TokenProperties<T>>::get((self.id, token_id));254 let prop = props.get(&key).ok_or("key not found")?;255256 Ok(prop.to_vec().into())257 }258}259260#[derive(ToLog)]261pub enum ERC721Events {262 263 264 265 266 267 Transfer {268 #[indexed]269 from: Address,270 #[indexed]271 to: Address,272 #[indexed]273 token_id: U256,274 },275 276 277 278 279 Approval {280 #[indexed]281 owner: Address,282 #[indexed]283 approved: Address,284 #[indexed]285 token_id: U256,286 },287 288 289 #[allow(dead_code)]290 ApprovalForAll {291 #[indexed]292 owner: Address,293 #[indexed]294 operator: Address,295 approved: bool,296 },297}298299300301#[solidity_interface(name = ERC721Metadata, expect_selector = 0x5b5e139f, enum(derive(PreDispatch)), enum_attr(weight))]302impl<T: Config> NonfungibleHandle<T>303where304 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,305{306 307 308 #[solidity(hide, rename_selector = "name")]309 fn name_proxy(&self) -> String {310 self.name()311 }312313 314 315 #[solidity(hide, rename_selector = "symbol")]316 fn symbol_proxy(&self) -> String {317 self.symbol()318 }319320 321 322 323 324 325 326 327 328 329 #[solidity(rename_selector = "tokenURI")]330 fn token_uri(&self, token_id: U256) -> Result<String> {331 let token_id_u32: u32 = token_id.try_into().map_err(|_| "token id overflow")?;332333 match get_token_property(self, token_id_u32, &key::url()).as_deref() {334 Err(_) | Ok("") => (),335 Ok(url) => {336 return Ok(url.into());337 }338 };339340 let base_uri =341 pallet_common::Pallet::<T>::get_collection_property(self.id, &key::base_uri())342 .map(BoundedVec::into_inner)343 .map(String::from_utf8)344 .transpose()345 .map_err(|e| {346 Error::Revert(alloc::format!(347 "Can not convert value \"baseURI\" to string with error \"{}\"",348 e349 ))350 })?;351352 let base_uri = match base_uri.as_deref() {353 None | Some("") => {354 return Ok("".into());355 }356 Some(base_uri) => base_uri.into(),357 };358359 Ok(360 match get_token_property(self, token_id_u32, &key::suffix()).as_deref() {361 Err(_) | Ok("") => base_uri,362 Ok(suffix) => base_uri + suffix,363 },364 )365 }366}367368369370#[solidity_interface(name = ERC721Enumerable, expect_selector = 0x780e9d63, enum(derive(PreDispatch)), enum_attr(weight))]371impl<T: Config> NonfungibleHandle<T> {372 373 374 375 376 fn token_by_index(&self, index: U256) -> U256 {377 index378 }379380 381 fn token_of_owner_by_index(&self, _owner: Address, _index: U256) -> Result<U256> {382 383 Err("not implemented".into())384 }385386 387 388 389 fn total_supply(&self) -> Result<U256> {390 self.consume_store_reads(1)?;391 Ok(<Pallet<T>>::total_supply(self).into())392 }393}394395396397#[solidity_interface(name = ERC721, events(ERC721Events), enum(derive(PreDispatch)), enum_attr(weight), expect_selector = 0x80ac58cd)]398impl<T: Config> NonfungibleHandle<T> {399 400 401 402 403 404 fn balance_of(&self, owner: Address) -> Result<U256> {405 self.consume_store_reads(1)?;406 let owner = T::CrossAccountId::from_eth(owner);407 let balance = <AccountBalance<T>>::get((self.id, owner));408 Ok(balance.into())409 }410 411 412 413 414 415 fn owner_of(&self, token_id: U256) -> Result<Address> {416 self.consume_store_reads(1)?;417 let token: TokenId = token_id.try_into()?;418 Ok(*<TokenData<T>>::get((self.id, token))419 .ok_or("token not found")?420 .owner421 .as_eth())422 }423 424 #[solidity(rename_selector = "safeTransferFrom")]425 fn safe_transfer_from_with_data(426 &mut self,427 _from: Address,428 _to: Address,429 _token_id: U256,430 _data: Bytes,431 ) -> Result<()> {432 433 Err("not implemented".into())434 }435 436 fn safe_transfer_from(&mut self, _from: Address, _to: Address, _token_id: U256) -> Result<()> {437 438 Err("not implemented".into())439 }440441 442 443 444 445 446 447 448 449 450 #[weight(<SelfWeightOf<T>>::transfer_from())]451 fn transfer_from(452 &mut self,453 caller: Caller,454 from: Address,455 to: Address,456 token_id: U256,457 ) -> Result<()> {458 let caller = T::CrossAccountId::from_eth(caller);459 let from = T::CrossAccountId::from_eth(from);460 let to = T::CrossAccountId::from_eth(to);461 let token = token_id.try_into()?;462 let budget = self463 .recorder464 .weight_calls_budget(<StructureWeight<T>>::find_parent());465466 <Pallet<T>>::transfer_from(self, &caller, &from, &to, token, &budget)467 .map_err(dispatch_to_evm::<T>)?;468 Ok(())469 }470471 472 473 474 475 476 477 #[weight(<SelfWeightOf<T>>::approve())]478 fn approve(&mut self, caller: Caller, approved: Address, token_id: U256) -> Result<()> {479 let caller = T::CrossAccountId::from_eth(caller);480 let approved = T::CrossAccountId::from_eth(approved);481 let token = token_id.try_into()?;482483 <Pallet<T>>::set_allowance(self, &caller, token, Some(&approved))484 .map_err(dispatch_to_evm::<T>)?;485 Ok(())486 }487488 489 490 491 492 #[weight(<SelfWeightOf<T>>::set_allowance_for_all())]493 fn set_approval_for_all(494 &mut self,495 caller: Caller,496 operator: Address,497 approved: bool,498 ) -> Result<()> {499 let caller = T::CrossAccountId::from_eth(caller);500 let operator = T::CrossAccountId::from_eth(operator);501502 <Pallet<T>>::set_allowance_for_all(self, &caller, &operator, approved)503 .map_err(dispatch_to_evm::<T>)?;504 Ok(())505 }506507 508 fn get_approved(&self, _token_id: U256) -> Result<Address> {509 510 Err("not implemented".into())511 }512513 514 #[weight(<SelfWeightOf<T>>::allowance_for_all())]515 fn is_approved_for_all(&self, owner: Address, operator: Address) -> Result<bool> {516 let owner = T::CrossAccountId::from_eth(owner);517 let operator = T::CrossAccountId::from_eth(operator);518519 Ok(<Pallet<T>>::allowance_for_all(self, &owner, &operator))520 }521}522523524#[solidity_interface(name = ERC721Burnable, enum(derive(PreDispatch)), enum_attr(weight))]525impl<T: Config> NonfungibleHandle<T> {526 527 528 529 530 #[weight(<SelfWeightOf<T>>::burn_item())]531 fn burn(&mut self, caller: Caller, token_id: U256) -> Result<()> {532 let caller = T::CrossAccountId::from_eth(caller);533 let token = token_id.try_into()?;534535 <Pallet<T>>::burn(self, &caller, token).map_err(dispatch_to_evm::<T>)?;536 Ok(())537 }538}539540541#[solidity_interface(name = ERC721UniqueMintable, enum(derive(PreDispatch)), enum_attr(weight))]542impl<T: Config> NonfungibleHandle<T> {543 544 545 546 #[weight(<SelfWeightOf<T>>::create_item())]547 fn mint(&mut self, caller: Caller, to: Address) -> Result<U256> {548 let token_id: U256 = <TokensMinted<T>>::get(self.id)549 .checked_add(1)550 .ok_or("item id overflow")?551 .into();552 self.mint_check_id(caller, to, token_id)?;553 Ok(token_id)554 }555556 557 558 559 560 561 #[solidity(hide, rename_selector = "mint")]562 #[weight(<SelfWeightOf<T>>::create_item())]563 fn mint_check_id(&mut self, caller: Caller, to: Address, token_id: U256) -> Result<bool> {564 let caller = T::CrossAccountId::from_eth(caller);565 let to = T::CrossAccountId::from_eth(to);566 let token_id: u32 = token_id.try_into()?;567 let budget = self568 .recorder569 .weight_calls_budget(<StructureWeight<T>>::find_parent());570571 if <TokensMinted<T>>::get(self.id)572 .checked_add(1)573 .ok_or("item id overflow")?574 != token_id575 {576 return Err("item id should be next".into());577 }578579 <Pallet<T>>::create_item(580 self,581 &caller,582 CreateItemData::<T> {583 properties: BoundedVec::default(),584 owner: to,585 },586 &budget,587 )588 .map_err(dispatch_to_evm::<T>)?;589590 Ok(true)591 }592593 594 595 596 597 #[solidity(rename_selector = "mintWithTokenURI")]598 #[weight(<SelfWeightOf<T>>::create_item() + <SelfWeightOf<T>>::set_token_properties(1))]599 fn mint_with_token_uri(600 &mut self,601 caller: Caller,602 to: Address,603 token_uri: String,604 ) -> Result<U256> {605 let token_id: U256 = <TokensMinted<T>>::get(self.id)606 .checked_add(1)607 .ok_or("item id overflow")?608 .into();609 self.mint_with_token_uri_check_id(caller, to, token_id, token_uri)?;610 Ok(token_id)611 }612613 614 615 616 617 618 619 #[solidity(hide, rename_selector = "mintWithTokenURI")]620 #[weight(<SelfWeightOf<T>>::create_item() + <SelfWeightOf<T>>::set_token_properties(1))]621 fn mint_with_token_uri_check_id(622 &mut self,623 caller: Caller,624 to: Address,625 token_id: U256,626 token_uri: String,627 ) -> Result<bool> {628 let key = key::url();629 let permission = get_token_permission::<T>(self.id, &key)?;630 if !permission.collection_admin {631 return Err("Operation is not allowed".into());632 }633634 let caller = T::CrossAccountId::from_eth(caller);635 let to = T::CrossAccountId::from_eth(to);636 let token_id: u32 = token_id.try_into().map_err(|_| "amount overflow")?;637 let budget = self638 .recorder639 .weight_calls_budget(<StructureWeight<T>>::find_parent());640641 if <TokensMinted<T>>::get(self.id)642 .checked_add(1)643 .ok_or("item id overflow")?644 != token_id645 {646 return Err("item id should be next".into());647 }648649 let mut properties = CollectionPropertiesVec::default();650 properties651 .try_push(Property {652 key,653 value: token_uri654 .into_bytes()655 .try_into()656 .map_err(|_| "token uri is too long")?,657 })658 .map_err(|e| Error::Revert(alloc::format!("Can't add property: {:?}", e)))?;659660 <Pallet<T>>::create_item(661 self,662 &caller,663 CreateItemData::<T> {664 properties,665 owner: to,666 },667 &budget,668 )669 .map_err(dispatch_to_evm::<T>)?;670 Ok(true)671 }672}673674fn get_token_property<T: Config>(675 collection: &CollectionHandle<T>,676 token_id: u32,677 key: &up_data_structs::PropertyKey,678) -> Result<String> {679 collection.consume_store_reads(1)?;680 let properties = <TokenProperties<T>>::try_get((collection.id, token_id))681 .map_err(|_| Error::Revert("Token properties not found".into()))?;682 if let Some(property) = properties.get(key) {683 return Ok(String::from_utf8_lossy(property).into());684 }685686 Err("Property tokenURI not found".into())687}688689fn get_token_permission<T: Config>(690 collection_id: CollectionId,691 key: &PropertyKey,692) -> Result<PropertyPermission> {693 let token_property_permissions = CollectionPropertyPermissions::<T>::try_get(collection_id)694 .map_err(|_| Error::Revert("No permissions for collection".into()))?;695 let a = token_property_permissions696 .get(key)697 .map(Clone::clone)698 .ok_or_else(|| {699 let key = String::from_utf8(key.clone().into_inner()).unwrap_or_default();700 Error::Revert(alloc::format!("No permission for key {}", key))701 })?;702 Ok(a)703}704705706#[solidity_interface(name = ERC721UniqueExtensions, enum(derive(PreDispatch)), enum_attr(weight))]707impl<T: Config> NonfungibleHandle<T>708where709 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,710{711 712 fn name(&self) -> String {713 decode_utf16(self.name.iter().copied())714 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))715 .collect::<String>()716 }717718 719 fn symbol(&self) -> String {720 String::from_utf8_lossy(&self.token_prefix).into()721 }722723 724 fn description(&self) -> String {725 decode_utf16(self.description.iter().copied())726 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))727 .collect::<String>()728 }729730 731 732 733 fn cross_owner_of(&self, token_id: U256) -> Result<eth::CrossAddress> {734 Self::token_owner(&self, token_id.try_into()?)735 .map(|o| eth::CrossAddress::from_sub_cross_account::<T>(&o))736 .map_err(|_| Error::Revert("token not found".into()))737 }738739 740 741 742 743 744 fn properties(&self, token_id: U256, keys: Vec<String>) -> Result<Vec<eth::Property>> {745 let keys = keys746 .into_iter()747 .map(|key| {748 <Vec<u8>>::from(key)749 .try_into()750 .map_err(|_| Error::Revert("key too large".into()))751 })752 .collect::<Result<Vec<_>>>()?;753754 <Self as CommonCollectionOperations<T>>::token_properties(755 &self,756 token_id.try_into()?,757 if keys.is_empty() { None } else { Some(keys) },758 )759 .into_iter()760 .map(eth::Property::try_from)761 .collect::<Result<Vec<_>>>()762 }763764 765 766 767 768 769 770 #[weight(<SelfWeightOf<T>>::approve())]771 fn approve_cross(772 &mut self,773 caller: Caller,774 approved: eth::CrossAddress,775 token_id: U256,776 ) -> Result<()> {777 let caller = T::CrossAccountId::from_eth(caller);778 let approved = approved.into_sub_cross_account::<T>()?;779 let token = token_id.try_into()?;780781 <Pallet<T>>::set_allowance(self, &caller, token, Some(&approved))782 .map_err(dispatch_to_evm::<T>)?;783 Ok(())784 }785786 787 788 789 790 791 #[weight(<SelfWeightOf<T>>::transfer())]792 fn transfer(&mut self, caller: Caller, to: Address, token_id: U256) -> Result<()> {793 let caller = T::CrossAccountId::from_eth(caller);794 let to = T::CrossAccountId::from_eth(to);795 let token = token_id.try_into()?;796 let budget = self797 .recorder798 .weight_calls_budget(<StructureWeight<T>>::find_parent());799800 <Pallet<T>>::transfer(self, &caller, &to, token, &budget).map_err(dispatch_to_evm::<T>)?;801 Ok(())802 }803804 805 806 807 808 809 #[weight(<SelfWeightOf<T>>::transfer())]810 fn transfer_cross(811 &mut self,812 caller: Caller,813 to: eth::CrossAddress,814 token_id: U256,815 ) -> Result<()> {816 let caller = T::CrossAccountId::from_eth(caller);817 let to = to.into_sub_cross_account::<T>()?;818 let token = token_id.try_into()?;819 let budget = self820 .recorder821 .weight_calls_budget(<StructureWeight<T>>::find_parent());822823 <Pallet<T>>::transfer(self, &caller, &to, token, &budget).map_err(dispatch_to_evm::<T>)?;824 Ok(())825 }826827 828 829 830 831 832 833 #[weight(<SelfWeightOf<T>>::transfer())]834 fn transfer_from_cross(835 &mut self,836 caller: Caller,837 from: eth::CrossAddress,838 to: eth::CrossAddress,839 token_id: U256,840 ) -> Result<()> {841 let caller = T::CrossAccountId::from_eth(caller);842 let from = from.into_sub_cross_account::<T>()?;843 let to = to.into_sub_cross_account::<T>()?;844 let token_id = token_id.try_into()?;845 let budget = self846 .recorder847 .weight_calls_budget(<StructureWeight<T>>::find_parent());848 Pallet::<T>::transfer_from(self, &caller, &from, &to, token_id, &budget)849 .map_err(dispatch_to_evm::<T>)?;850 Ok(())851 }852853 854 855 856 857 858 859 #[solidity(hide)]860 #[weight(<SelfWeightOf<T>>::burn_from())]861 fn burn_from(&mut self, caller: Caller, from: Address, token_id: U256) -> Result<()> {862 let caller = T::CrossAccountId::from_eth(caller);863 let from = T::CrossAccountId::from_eth(from);864 let token = token_id.try_into()?;865 let budget = self866 .recorder867 .weight_calls_budget(<StructureWeight<T>>::find_parent());868869 <Pallet<T>>::burn_from(self, &caller, &from, token, &budget)870 .map_err(dispatch_to_evm::<T>)?;871 Ok(())872 }873874 875 876 877 878 879 880 #[weight(<SelfWeightOf<T>>::burn_from())]881 fn burn_from_cross(882 &mut self,883 caller: Caller,884 from: eth::CrossAddress,885 token_id: U256,886 ) -> Result<()> {887 let caller = T::CrossAccountId::from_eth(caller);888 let from = from.into_sub_cross_account::<T>()?;889 let token = token_id.try_into()?;890 let budget = self891 .recorder892 .weight_calls_budget(<StructureWeight<T>>::find_parent());893894 <Pallet<T>>::burn_from(self, &caller, &from, token, &budget)895 .map_err(dispatch_to_evm::<T>)?;896 Ok(())897 }898899 900 fn next_token_id(&self) -> Result<U256> {901 self.consume_store_reads(1)?;902 Ok(<TokensMinted<T>>::get(self.id)903 .checked_add(1)904 .ok_or("item id overflow")?905 .into())906 }907908 909 910 911 912 913 #[solidity(hide)]914 #[weight(<SelfWeightOf<T>>::create_multiple_items(token_ids.len() as u32))]915 fn mint_bulk(&mut self, caller: Caller, to: Address, token_ids: Vec<U256>) -> Result<bool> {916 let caller = T::CrossAccountId::from_eth(caller);917 let to = T::CrossAccountId::from_eth(to);918 let mut expected_index = <TokensMinted<T>>::get(self.id)919 .checked_add(1)920 .ok_or("item id overflow")?;921 let budget = self922 .recorder923 .weight_calls_budget(<StructureWeight<T>>::find_parent());924925 let total_tokens = token_ids.len();926 for id in token_ids.into_iter() {927 let id: u32 = id.try_into().map_err(|_| "token id overflow")?;928 if id != expected_index {929 return Err("item id should be next".into());930 }931 expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;932 }933 let data = (0..total_tokens)934 .map(|_| CreateItemData::<T> {935 properties: BoundedVec::default(),936 owner: to.clone(),937 })938 .collect();939940 <Pallet<T>>::create_multiple_items(self, &caller, data, &budget)941 .map_err(dispatch_to_evm::<T>)?;942 Ok(true)943 }944945 946 947 948 949 950 #[solidity(hide, rename_selector = "mintBulkWithTokenURI")]951 #[weight(<SelfWeightOf<T>>::create_multiple_items(tokens.len() as u32) + <SelfWeightOf<T>>::set_token_properties(tokens.len() as u32))]952 fn mint_bulk_with_token_uri(953 &mut self,954 caller: Caller,955 to: Address,956 tokens: Vec<TokenUri>,957 ) -> Result<bool> {958 let key = key::url();959 let caller = T::CrossAccountId::from_eth(caller);960 let to = T::CrossAccountId::from_eth(to);961 let mut expected_index = <TokensMinted<T>>::get(self.id)962 .checked_add(1)963 .ok_or("item id overflow")?;964 let budget = self965 .recorder966 .weight_calls_budget(<StructureWeight<T>>::find_parent());967968 let mut data = Vec::with_capacity(tokens.len());969 for TokenUri { id, uri } in tokens {970 let id: u32 = id.try_into().map_err(|_| "token id overflow")?;971 if id != expected_index {972 return Err("item id should be next".into());973 }974 expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;975976 let mut properties = CollectionPropertiesVec::default();977 properties978 .try_push(Property {979 key: key.clone(),980 value: uri981 .into_bytes()982 .try_into()983 .map_err(|_| "token uri is too long")?,984 })985 .map_err(|e| Error::Revert(alloc::format!("Can't add property: {:?}", e)))?;986987 data.push(CreateItemData::<T> {988 properties,989 owner: to.clone(),990 });991 }992993 <Pallet<T>>::create_multiple_items(self, &caller, data, &budget)994 .map_err(dispatch_to_evm::<T>)?;995 Ok(true)996 }997998 999 1000 1001 1002 #[weight(<SelfWeightOf<T>>::create_item() + <SelfWeightOf<T>>::set_token_properties(properties.len() as u32))]1003 fn mint_cross(1004 &mut self,1005 caller: Caller,1006 to: eth::CrossAddress,1007 properties: Vec<eth::Property>,1008 ) -> Result<U256> {1009 let token_id = <TokensMinted<T>>::get(self.id)1010 .checked_add(1)1011 .ok_or("item id overflow")?;10121013 let to = to.into_sub_cross_account::<T>()?;10141015 let properties = properties1016 .into_iter()1017 .map(eth::Property::try_into)1018 .collect::<Result<Vec<_>>>()?1019 .try_into()1020 .map_err(|_| Error::Revert(alloc::format!("too many properties")))?;10211022 let caller = T::CrossAccountId::from_eth(caller);10231024 let budget = self1025 .recorder1026 .weight_calls_budget(<StructureWeight<T>>::find_parent());10271028 <Pallet<T>>::create_item(1029 self,1030 &caller,1031 CreateItemData::<T> {1032 properties,1033 owner: to,1034 },1035 &budget,1036 )1037 .map_err(dispatch_to_evm::<T>)?;10381039 Ok(token_id.into())1040 }10411042 1043 fn collection_helper_address(&self) -> Address {1044 T::ContractAddress::get()1045 }1046}10471048#[solidity_interface(1049 name = UniqueNFT,1050 is(1051 ERC721,1052 ERC721Enumerable,1053 ERC721UniqueExtensions,1054 ERC721UniqueMintable,1055 ERC721Burnable,1056 ERC721Metadata(if(this.flags.erc721metadata)),1057 Collection(via(common_mut returns CollectionHandle<T>)),1058 TokenProperties,1059 ),1060 enum(derive(PreDispatch)),1061)]1062impl<T: Config> NonfungibleHandle<T> where T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]> {}106310641065generate_stubgen!(gen_impl, UniqueNFTCall<()>, true);1066generate_stubgen!(gen_iface, UniqueNFTCall<()>, false);10671068impl<T: Config> CommonEvmHandler for NonfungibleHandle<T>1069where1070 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,1071{1072 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueNFT.raw");10731074 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {1075 call::<T, UniqueNFTCall<T>, _, _>(handle, self)1076 }1077}