12345678910111213141516171819202122extern crate alloc;2324use core::{25 char::{REPLACEMENT_CHARACTER, decode_utf16},26 convert::TryInto,27};28use evm_coder::{ToLog, execution::*, generate_stubgen, solidity, solidity_interface, types::*, weight};29use frame_support::{BoundedBTreeMap, BoundedVec};30use pallet_common::{31 CollectionHandle, CollectionPropertyPermissions,32 erc::{CommonEvmHandler, CollectionCall, static_property::key, static_property::value},33};34use pallet_evm::{account::CrossAccountId, PrecompileHandle};35use pallet_evm_coder_substrate::{call, dispatch_to_evm};36use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};37use sp_core::H160;38use sp_std::{collections::btree_map::BTreeMap, vec::Vec, vec};39use up_data_structs::{40 CollectionId, CollectionPropertiesVec, mapping::TokenAddressMapping, Property, PropertyKey,41 PropertyKeyPermission, PropertyPermission, TokenId,42};4344use crate::{45 AccountBalance, Balance, Config, CreateItemData, Pallet, RefungibleHandle, SelfWeightOf,46 TokenProperties, TokensMinted, TotalSupply, weights::WeightInfo,47};4849pub const ADDRESS_FOR_PARTIALLY_OWNED_TOKENS: H160 = H160::repeat_byte(0xff);505152#[solidity_interface(name = TokenProperties)]53impl<T: Config> RefungibleHandle<T> {54 55 56 57 58 59 60 fn set_token_property_permission(61 &mut self,62 caller: caller,63 key: string,64 is_mutable: bool,65 collection_admin: bool,66 token_owner: bool,67 ) -> Result<()> {68 let caller = T::CrossAccountId::from_eth(caller);69 <Pallet<T>>::set_token_property_permissions(70 self,71 &caller,72 vec![PropertyKeyPermission {73 key: <Vec<u8>>::from(key)74 .try_into()75 .map_err(|_| "too long key")?,76 permission: PropertyPermission {77 mutable: is_mutable,78 collection_admin,79 token_owner,80 },81 }],82 )83 .map_err(dispatch_to_evm::<T>)84 }8586 87 88 89 90 91 fn set_property(92 &mut self,93 caller: caller,94 token_id: uint256,95 key: string,96 value: bytes,97 ) -> Result<()> {98 let caller = T::CrossAccountId::from_eth(caller);99 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;100 let key = <Vec<u8>>::from(key)101 .try_into()102 .map_err(|_| "key too long")?;103 let value = value.try_into().map_err(|_| "value too long")?;104105 let nesting_budget = self106 .recorder107 .weight_calls_budget(<StructureWeight<T>>::find_parent());108109 <Pallet<T>>::set_token_property(110 self,111 &caller,112 TokenId(token_id),113 Property { key, value },114 &nesting_budget,115 )116 .map_err(dispatch_to_evm::<T>)117 }118119 120 121 122 123 fn delete_property(&mut self, token_id: uint256, caller: caller, key: string) -> Result<()> {124 let caller = T::CrossAccountId::from_eth(caller);125 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;126 let key = <Vec<u8>>::from(key)127 .try_into()128 .map_err(|_| "key too long")?;129130 let nesting_budget = self131 .recorder132 .weight_calls_budget(<StructureWeight<T>>::find_parent());133134 <Pallet<T>>::delete_token_property(self, &caller, TokenId(token_id), key, &nesting_budget)135 .map_err(dispatch_to_evm::<T>)136 }137138 139 140 141 142 143 fn property(&self, token_id: uint256, key: string) -> Result<bytes> {144 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;145 let key = <Vec<u8>>::from(key)146 .try_into()147 .map_err(|_| "key too long")?;148149 let props = <TokenProperties<T>>::get((self.id, token_id));150 let prop = props.get(&key).ok_or("key not found")?;151152 Ok(prop.to_vec())153 }154}155156#[derive(ToLog)]157pub enum ERC721Events {158 159 160 161 Transfer {162 #[indexed]163 from: address,164 #[indexed]165 to: address,166 #[indexed]167 token_id: uint256,168 },169 170 Approval {171 #[indexed]172 owner: address,173 #[indexed]174 approved: address,175 #[indexed]176 token_id: uint256,177 },178 179 #[allow(dead_code)]180 ApprovalForAll {181 #[indexed]182 owner: address,183 #[indexed]184 operator: address,185 approved: bool,186 },187}188189#[derive(ToLog)]190pub enum ERC721MintableEvents {191 192 #[allow(dead_code)]193 MintingFinished {},194}195196#[solidity_interface(name = ERC721Metadata)]197impl<T: Config> RefungibleHandle<T> {198 199 fn name(&self) -> Result<string> {200 Ok(decode_utf16(self.name.iter().copied())201 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))202 .collect::<string>())203 }204205 206 fn symbol(&self) -> Result<string> {207 Ok(string::from_utf8_lossy(&self.token_prefix).into())208 }209210 211 212 213 214 215 216 217 218 219 #[solidity(rename_selector = "tokenURI")]220 fn token_uri(&self, token_id: uint256) -> Result<string> {221 let token_id_u32: u32 = token_id.try_into().map_err(|_| "token id overflow")?;222223 match get_token_property(self, token_id_u32, &key::url()).as_deref() {224 Err(_) | Ok("") => (),225 Ok(url) => {226 return Ok(url.into());227 }228 };229230 let base_uri =231 pallet_common::Pallet::<T>::get_collection_property(self.id, &key::base_uri())232 .map(BoundedVec::into_inner)233 .map(string::from_utf8)234 .transpose()235 .map_err(|e| {236 Error::Revert(alloc::format!(237 "Can not convert value \"baseURI\" to string with error \"{}\"",238 e239 ))240 })?;241242 let base_uri = match base_uri.as_deref() {243 None | Some("") => {244 return Ok("".into());245 }246 Some(base_uri) => base_uri.into(),247 };248249 Ok(250 match get_token_property(self, token_id_u32, &key::suffix()).as_deref() {251 Err(_) | Ok("") => base_uri,252 Ok(suffix) => base_uri + suffix,253 },254 )255 }256}257258259260#[solidity_interface(name = ERC721Enumerable)]261impl<T: Config> RefungibleHandle<T> {262 263 264 265 266 fn token_by_index(&self, index: uint256) -> Result<uint256> {267 Ok(index)268 }269270 271 fn token_of_owner_by_index(&self, _owner: address, _index: uint256) -> Result<uint256> {272 273 Err("not implemented".into())274 }275276 277 278 279 fn total_supply(&self) -> Result<uint256> {280 self.consume_store_reads(1)?;281 Ok(<Pallet<T>>::total_supply(self).into())282 }283}284285286287#[solidity_interface(name = ERC721, events(ERC721Events))]288impl<T: Config> RefungibleHandle<T> {289 290 291 292 293 294 fn balance_of(&self, owner: address) -> Result<uint256> {295 self.consume_store_reads(1)?;296 let owner = T::CrossAccountId::from_eth(owner);297 let balance = <AccountBalance<T>>::get((self.id, owner));298 Ok(balance.into())299 }300301 302 303 304 305 306 307 308 fn owner_of(&self, token_id: uint256) -> Result<address> {309 self.consume_store_reads(2)?;310 let token = token_id.try_into()?;311 let owner = <Pallet<T>>::token_owner(self.id, token);312 Ok(owner313 .map(|address| *address.as_eth())314 .unwrap_or_else(|| ADDRESS_FOR_PARTIALLY_OWNED_TOKENS))315 }316317 318 fn safe_transfer_from_with_data(319 &mut self,320 _from: address,321 _to: address,322 _token_id: uint256,323 _data: bytes,324 ) -> Result<void> {325 326 Err("not implemented".into())327 }328329 330 fn safe_transfer_from(331 &mut self,332 _from: address,333 _to: address,334 _token_id: uint256,335 ) -> Result<void> {336 337 Err("not implemented".into())338 }339340 341 342 343 344 345 346 347 348 349 350 #[weight(<SelfWeightOf<T>>::transfer_from_creating_removing())]351 fn transfer_from(352 &mut self,353 caller: caller,354 from: address,355 to: address,356 token_id: uint256,357 ) -> Result<void> {358 let caller = T::CrossAccountId::from_eth(caller);359 let from = T::CrossAccountId::from_eth(from);360 let to = T::CrossAccountId::from_eth(to);361 let token = token_id.try_into()?;362 let budget = self363 .recorder364 .weight_calls_budget(<StructureWeight<T>>::find_parent());365366 let balance = balance(&self, token, &from)?;367 ensure_single_owner(&self, token, balance)?;368369 <Pallet<T>>::transfer_from(self, &caller, &from, &to, token, balance, &budget)370 .map_err(dispatch_to_evm::<T>)?;371372 Ok(())373 }374375 376 fn approve(&mut self, _caller: caller, _approved: address, _token_id: uint256) -> Result<void> {377 Err("not implemented".into())378 }379380 381 fn set_approval_for_all(382 &mut self,383 _caller: caller,384 _operator: address,385 _approved: bool,386 ) -> Result<void> {387 388 Err("not implemented".into())389 }390391 392 fn get_approved(&self, _token_id: uint256) -> Result<address> {393 394 Err("not implemented".into())395 }396397 398 fn is_approved_for_all(&self, _owner: address, _operator: address) -> Result<address> {399 400 Err("not implemented".into())401 }402}403404405pub fn balance<T: Config>(406 collection: &RefungibleHandle<T>,407 token: TokenId,408 owner: &T::CrossAccountId,409) -> Result<u128> {410 collection.consume_store_reads(1)?;411 let balance = <Balance<T>>::get((collection.id, token, &owner));412 Ok(balance)413}414415416pub fn ensure_single_owner<T: Config>(417 collection: &RefungibleHandle<T>,418 token: TokenId,419 owner_balance: u128,420) -> Result<()> {421 collection.consume_store_reads(1)?;422 let total_supply = <TotalSupply<T>>::get((collection.id, token));423 if total_supply != owner_balance {424 return Err("token has multiple owners".into());425 }426 Ok(())427}428429430#[solidity_interface(name = ERC721Burnable)]431impl<T: Config> RefungibleHandle<T> {432 433 434 435 436 #[weight(<SelfWeightOf<T>>::burn_item_fully())]437 fn burn(&mut self, caller: caller, token_id: uint256) -> Result<void> {438 let caller = T::CrossAccountId::from_eth(caller);439 let token = token_id.try_into()?;440441 let balance = balance(&self, token, &caller)?;442 ensure_single_owner(&self, token, balance)?;443444 <Pallet<T>>::burn(self, &caller, token, balance).map_err(dispatch_to_evm::<T>)?;445 Ok(())446 }447}448449450#[solidity_interface(name = ERC721Mintable, events(ERC721MintableEvents))]451impl<T: Config> RefungibleHandle<T> {452 fn minting_finished(&self) -> Result<bool> {453 Ok(false)454 }455456 457 458 459 460 461 #[weight(<SelfWeightOf<T>>::create_item())]462 fn mint(&mut self, caller: caller, to: address, token_id: uint256) -> Result<bool> {463 let caller = T::CrossAccountId::from_eth(caller);464 let to = T::CrossAccountId::from_eth(to);465 let token_id: u32 = token_id.try_into()?;466 let budget = self467 .recorder468 .weight_calls_budget(<StructureWeight<T>>::find_parent());469470 if <TokensMinted<T>>::get(self.id)471 .checked_add(1)472 .ok_or("item id overflow")?473 != token_id474 {475 return Err("item id should be next".into());476 }477478 let users = [(to.clone(), 1)]479 .into_iter()480 .collect::<BTreeMap<_, _>>()481 .try_into()482 .unwrap();483 <Pallet<T>>::create_item(484 self,485 &caller,486 CreateItemData::<T::CrossAccountId> {487 users,488 properties: CollectionPropertiesVec::default(),489 },490 &budget,491 )492 .map_err(dispatch_to_evm::<T>)?;493494 Ok(true)495 }496497 498 499 500 501 502 503 #[solidity(rename_selector = "mintWithTokenURI")]504 #[weight(<SelfWeightOf<T>>::create_item())]505 fn mint_with_token_uri(506 &mut self,507 caller: caller,508 to: address,509 token_id: uint256,510 token_uri: string,511 ) -> Result<bool> {512 let key = key::url();513 let permission = get_token_permission::<T>(self.id, &key)?;514 if !permission.collection_admin {515 return Err("Operation is not allowed".into());516 }517518 let caller = T::CrossAccountId::from_eth(caller);519 let to = T::CrossAccountId::from_eth(to);520 let token_id: u32 = token_id.try_into().map_err(|_| "amount overflow")?;521 let budget = self522 .recorder523 .weight_calls_budget(<StructureWeight<T>>::find_parent());524525 if <TokensMinted<T>>::get(self.id)526 .checked_add(1)527 .ok_or("item id overflow")?528 != token_id529 {530 return Err("item id should be next".into());531 }532533 let mut properties = CollectionPropertiesVec::default();534 properties535 .try_push(Property {536 key,537 value: token_uri538 .into_bytes()539 .try_into()540 .map_err(|_| "token uri is too long")?,541 })542 .map_err(|e| Error::Revert(alloc::format!("Can't add property: {:?}", e)))?;543544 let users = [(to.clone(), 1)]545 .into_iter()546 .collect::<BTreeMap<_, _>>()547 .try_into()548 .unwrap();549 <Pallet<T>>::create_item(550 self,551 &caller,552 CreateItemData::<T::CrossAccountId> { users, properties },553 &budget,554 )555 .map_err(dispatch_to_evm::<T>)?;556 Ok(true)557 }558559 560 fn finish_minting(&mut self, _caller: caller) -> Result<bool> {561 Err("not implementable".into())562 }563}564565fn get_token_property<T: Config>(566 collection: &CollectionHandle<T>,567 token_id: u32,568 key: &up_data_structs::PropertyKey,569) -> Result<string> {570 collection.consume_store_reads(1)?;571 let properties = <TokenProperties<T>>::try_get((collection.id, token_id))572 .map_err(|_| Error::Revert("Token properties not found".into()))?;573 if let Some(property) = properties.get(key) {574 return Ok(string::from_utf8_lossy(property).into());575 }576577 Err("Property tokenURI not found".into())578}579580fn get_token_permission<T: Config>(581 collection_id: CollectionId,582 key: &PropertyKey,583) -> Result<PropertyPermission> {584 let token_property_permissions = CollectionPropertyPermissions::<T>::try_get(collection_id)585 .map_err(|_| Error::Revert("No permissions for collection".into()))?;586 let a = token_property_permissions587 .get(key)588 .map(Clone::clone)589 .ok_or_else(|| {590 let key = string::from_utf8(key.clone().into_inner()).unwrap_or_default();591 Error::Revert(alloc::format!("No permission for key {}", key))592 })?;593 Ok(a)594}595596597#[solidity_interface(name = ERC721UniqueExtensions)]598impl<T: Config> RefungibleHandle<T> {599 600 601 602 603 604 605 #[weight(<SelfWeightOf<T>>::transfer_creating_removing())]606 fn transfer(&mut self, caller: caller, to: address, token_id: uint256) -> Result<void> {607 let caller = T::CrossAccountId::from_eth(caller);608 let to = T::CrossAccountId::from_eth(to);609 let token = token_id.try_into()?;610 let budget = self611 .recorder612 .weight_calls_budget(<StructureWeight<T>>::find_parent());613614 let balance = balance(&self, token, &caller)?;615 ensure_single_owner(&self, token, balance)?;616617 <Pallet<T>>::transfer(self, &caller, &to, token, balance, &budget)618 .map_err(dispatch_to_evm::<T>)?;619 Ok(())620 }621622 623 624 625 626 627 628 629 #[weight(<SelfWeightOf<T>>::burn_from())]630 fn burn_from(&mut self, caller: caller, from: address, token_id: uint256) -> 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 let balance = balance(&self, token, &caller)?;639 ensure_single_owner(&self, token, balance)?;640641 <Pallet<T>>::burn_from(self, &caller, &from, token, balance, &budget)642 .map_err(dispatch_to_evm::<T>)?;643 Ok(())644 }645646 647 fn next_token_id(&self) -> Result<uint256> {648 self.consume_store_reads(1)?;649 Ok(<TokensMinted<T>>::get(self.id)650 .checked_add(1)651 .ok_or("item id overflow")?652 .into())653 }654655 656 657 658 659 660 #[weight(<SelfWeightOf<T>>::create_multiple_items(token_ids.len() as u32))]661 fn mint_bulk(&mut self, caller: caller, to: address, token_ids: Vec<uint256>) -> Result<bool> {662 let caller = T::CrossAccountId::from_eth(caller);663 let to = T::CrossAccountId::from_eth(to);664 let mut expected_index = <TokensMinted<T>>::get(self.id)665 .checked_add(1)666 .ok_or("item id overflow")?;667 let budget = self668 .recorder669 .weight_calls_budget(<StructureWeight<T>>::find_parent());670671 let total_tokens = token_ids.len();672 for id in token_ids.into_iter() {673 let id: u32 = id.try_into().map_err(|_| "token id overflow")?;674 if id != expected_index {675 return Err("item id should be next".into());676 }677 expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;678 }679 let users = [(to.clone(), 1)]680 .into_iter()681 .collect::<BTreeMap<_, _>>()682 .try_into()683 .unwrap();684 let create_item_data = CreateItemData::<T::CrossAccountId> {685 users,686 properties: CollectionPropertiesVec::default(),687 };688 let data = (0..total_tokens)689 .map(|_| create_item_data.clone())690 .collect();691692 <Pallet<T>>::create_multiple_items(self, &caller, data, &budget)693 .map_err(dispatch_to_evm::<T>)?;694 Ok(true)695 }696697 698 699 700 701 702 #[solidity(rename_selector = "mintBulkWithTokenURI")]703 #[weight(<SelfWeightOf<T>>::create_multiple_items(tokens.len() as u32))]704 fn mint_bulk_with_token_uri(705 &mut self,706 caller: caller,707 to: address,708 tokens: Vec<(uint256, string)>,709 ) -> Result<bool> {710 let key = key::url();711 let caller = T::CrossAccountId::from_eth(caller);712 let to = T::CrossAccountId::from_eth(to);713 let mut expected_index = <TokensMinted<T>>::get(self.id)714 .checked_add(1)715 .ok_or("item id overflow")?;716 let budget = self717 .recorder718 .weight_calls_budget(<StructureWeight<T>>::find_parent());719720 let mut data = Vec::with_capacity(tokens.len());721 let users: BoundedBTreeMap<_, _, _> = [(to.clone(), 1)]722 .into_iter()723 .collect::<BTreeMap<_, _>>()724 .try_into()725 .unwrap();726 for (id, token_uri) in tokens {727 let id: u32 = id.try_into().map_err(|_| "token id overflow")?;728 if id != expected_index {729 return Err("item id should be next".into());730 }731 expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;732733 let mut properties = CollectionPropertiesVec::default();734 properties735 .try_push(Property {736 key: key.clone(),737 value: token_uri738 .into_bytes()739 .try_into()740 .map_err(|_| "token uri is too long")?,741 })742 .map_err(|e| Error::Revert(alloc::format!("Can't add property: {:?}", e)))?;743744 let create_item_data = CreateItemData::<T::CrossAccountId> {745 users: users.clone(),746 properties,747 };748 data.push(create_item_data);749 }750751 <Pallet<T>>::create_multiple_items(self, &caller, data, &budget)752 .map_err(dispatch_to_evm::<T>)?;753 Ok(true)754 }755756 757 758 759 fn token_contract_address(&self, token: uint256) -> Result<address> {760 Ok(T::EvmTokenAddressMapping::token_to_address(761 self.id,762 token.try_into().map_err(|_| "token id overflow")?,763 ))764 }765}766767impl<T: Config> RefungibleHandle<T> {768 pub fn supports_metadata(&self) -> bool {769 let has_metadata_support_enabled = if let Some(erc721_metadata) =770 pallet_common::Pallet::<T>::get_collection_property(self.id, &key::erc721_metadata())771 {772 *erc721_metadata.into_inner() == *value::ERC721_METADATA_SUPPORTED773 } else {774 false775 };776777 let has_url_property_permissions = get_token_permission::<T>(self.id, &key::url()).is_ok();778779 has_metadata_support_enabled && has_url_property_permissions780 }781}782783#[solidity_interface(784 name = UniqueRefungible,785 is(786 ERC721,787 ERC721Enumerable,788 ERC721UniqueExtensions,789 ERC721Mintable,790 ERC721Burnable,791 Collection(via(common_mut returns CollectionHandle<T>)),792 TokenProperties,793 ERC721Metadata(if(this.supports_metadata())),794 )795)]796impl<T: Config> RefungibleHandle<T> where T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]> {}797798799generate_stubgen!(gen_impl, UniqueRefungibleCall<()>, true);800generate_stubgen!(gen_iface, UniqueRefungibleCall<()>, false);801802impl<T: Config> CommonEvmHandler for RefungibleHandle<T>803where804 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,805{806 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueRefungible.raw");807 fn call(808 self,809 handle: &mut impl PrecompileHandle,810 ) -> Option<pallet_common::erc::PrecompileResult> {811 call::<T, UniqueRefungibleCall<T>, _, _>(handle, self)812 }813}